【问题标题】:Netty 4 ReadTimeoutHandler not throwing in OioEventLoopGroupNetty 4 ReadTimeoutHandler 没有抛出 OioEventLoopGroup
【发布时间】:2013-07-29 08:30:21
【问题描述】:

我是 netty 的新手。这是预期的行为吗?

再详细一点:

public class Test {
  public static void connect(){
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    Bootstrap bs = new Bootstrap();
    bs.group(workerGroup);
    bs.channel(NioSocketChannel.class);
    bs.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
    bs.handler( new ChannelInitializer<SocketChannel>(){
      @Override
      protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pl = ch.pipeline();
        pl.addLast("readTimeoutHandler", new ReadTimeoutHandler(1000,
          TimeUnit.MILLISECONDS));
          pl.addLast("framer", new DelimiterBasedFrameDecoder(
            16384, Delimiters.lineDelimiter()));
          pl.addLast("string-decoder", new StringDecoder());
          pl.addLast("handler", 
            new SimpleChannelInboundHandler<String> (String.class){
              @Override
              protected void channelRead0(ChannelHandlerContext ctx,
                String msg) throws Exception {
                System.out.println(msg);
              }
              @Override
              protected void exceptionCaught(ChannelHandlerContext ctx, 
                Throwable cause) throws Exception {
                if(cause instanceof ReadTimeoutException){
                  System.out.println("Timed out.");
                }
                ctx.close();
              }
          });
      }
    });
    bs.connect("127.0.0.1", 45001);
  }
}

这只是测试用例,所以可能有点不正确,不过管道与我的实际管道非常接近。

基本上,如果我将 EventLoopGroup 初始化从 NioEventLoopGroup 更改为 OioEventLoopGroup 并将引导通道设置从 bootstrap.channel(NioSocketChannel.class) 更改为 bootstrap.channel(OioSocketChannel.class) 而不触及任何其他内容,ReadTimeoutHandler 将停止抛出 ReadTimeoutExceptions

【问题讨论】:

    标签: java netty socketchannel


    【解决方案1】:

    这已在 Netty 4.0.4.Final 中修复。请升级,参见 [1]。

    [1]https://github.com/netty/netty/issues/1614

    【讨论】:

    • 哎呀没注意到那个。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多