Netty:空闲状态处理程序未显示通道是否空闲

时间:2018-04-29 20:44:08

标签: java netty

我的要求:
我想检测通道是否处于空闲状态以便读取一段时间,并希望根据该时间超时。 My Netty客户端正在向1000台服务器发送请求。

问题:即使我正在使用一些总是超时的ips,我的网络客户端也永远不会显示有时候是否存在任何空闲信道。 我怀疑我没有正确实现IdleStateHandler。我试过减少IdleStateHandler的读取超时但没有运气。我花了好几个小时搞清楚这一点。任何帮助将非常感谢 所以,下面是我的代码:
My Netty客户端:

public void connect(final InetAddress remoteAddress){
        new Bootstrap()
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .group(eventLoopGroup)
            .channel(NioSocketChannel.class)
            .handler(httpNettyClientChannelInitializer)
            .connect(remoteAddress, serverPort)
            .addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) {
                        future.cancel(!future.isSuccess());
                    }
                });
    }

My Netty Channel Initalizer:

public class HttpNettyClientChannelInitializer extends ChannelInitializer<SocketChannel> {

    private final Provider<HttpNettyClientChannelHandler> handlerProvider;
    private final int timeout;
    private int maxContentLength;

    @Inject
    public HttpNettyClientChannelInitializer(Provider<HttpNettyClientChannelHandler> handlerProvider,
            @Named("readResponseTimeout") int timeout, @Named("maxContentLength") int maxContentLength) {
        this.handlerProvider = handlerProvider;
        this.timeout = timeout;
        this.maxContentLength = maxContentLength;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) {
        ChannelPipeline pipelineNettyClientChannel = socketChannel.pipeline();
        pipelineNettyClientChannel.addLast("codec", new HttpClientCodec());
        pipelineNettyClientChannel.addLast("idleStateHandler", new IdleStateHandler(timeout,0,0, TimeUnit.MILLISECONDS));
        pipelineNettyClientChannel.addLast("aggregator", new HttpObjectAggregator(maxContentLength));
        //handlerProvider.get() will provide new instance of channel handler
        pipelineNettyClientChannel.addLast("handler", handlerProvider.get());
    }

}

My Netty频道处理程序

 @ChannelHandler.Sharable
    public class HttpNettyClientChannelHandler extends SimpleChannelInboundHandler<HttpObject> {
      @Override
      public void channelActive(ChannelHandlerContext channelHandlerContext){...}

    @Override    
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject){...}

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
             if (evt instanceof IdleStateEvent) {
                 IdleStateEvent e = (IdleStateEvent) evt;
                 if (e.state() == IdleState.READER_IDLE) {
                     System.out.println("Idle channel");
                     ctx.close();
             }
         }
     }
}

1 个答案:

答案 0 :(得分:0)

某些不匹配的 .jar 库会导致此类错误。你检查过版本和兼容性了吗。

  • 版本
  • 依赖项
  • 完成单元测试

https://github.com/netty/netty

-> 测试 -> 检查问题可能是您之前声明的问题吗?