刚刚从mina中转向netty,链路检测报文怎么配都不好使,google之,终于在jboss的官网上发现已经报告此bug,并在netty4.0修复,给提供了patch包,修复patch之后,重新打包,发现还是没有解决。查看源码之后发现netty将超时的时间从options中设置。于是将超时时间重新设置:
重新启动,解决。
ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool())
);
ChannelPipelineFactory pipelineFactory = new MyPipelineFactory(clientSessionHandler ,decoder,encoder);
bootstrap.setPipelineFactory( pipelineFactory );
bootstrap.setOption("allIdleTime","5"); //这里,很重要
public class ClientIdleHandler extends IdleStateAwareChannelHandler {
final Logger logger = LoggerFactory.getLogger(ClientIdleHandler.class);
@Override
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
if( e.getState() == IdleState.ALL_IDLE){
logger.debug("链路空闲!发送心跳!S:{} - C:{} idleState:{}", new Object[]{ctx.getChannel().getRemoteAddress(), ctx.getChannel().getLocalAddress() , e.getState()});
e.getChannel().write(MessageHelper.buildMessageEcho());
super.channelIdle(ctx, e);
}
}
}
public class MyPipelineFactory implements ChannelPipelineFactory{
private ETMessageDecoder decoder;
private ETMessageEncoder encoder;
private ChannelHandler clientHandler;
public MyPipelineFactory(ChannelHandler clientHandler,ETMessageDecoder decoder,ETMessageEncoder encoder){
this.clientHandler = clientHandler;
this.decoder = decoder;
this.encoder = encoder;
}
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
Timer timer = new HashedWheelTimer();
pipeline.addLast("encoder",encoder);
pipeline.addLast("decoder" , decoder);
pipeline.addLast("handler" , clientHandler);
pipeline.addLast("timeout", new IdleStateHandler(timer, 0, 0, 10));
pipeline.addLast("idleHandler", new ClientIdleHandler());
return pipeline;
}
}
重新启动,解决。