Netty:空闲状态处理程序没有显示频道是否闲置



我的要求:
我想检测到频道是否闲着一段时间来阅读,并希望基于此时间超时。我的Netty客户端将请求发送到1000台服务器。

问题:我的Netty客户端永远不会表明,即使我使用的一些IPS始终会超时,我的Netty客户端也不会显示一段时间的闲置通道。我怀疑我没有正确实施idlestatehandler。我尝试减少iDlestateHandler的阅读超时,但没有运气。我花了几个小时来解决这个问题。任何帮助都将不胜感激。
因此,下面是我的代码:
我的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());
                    }
                });
    }

我的Netty Channel Intalizer:

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());
    }
}

我的Netty Channel处理程序

 @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();
             }
         }
     }
}

一些不匹配的.jar库导致这种错误。您是否检查了版本和兼容性。

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

https://github.com/netty/netty

- &gt;测试 - &gt;检查问题可能是您在声明之前声明的问题?

最新更新