Netty 4 ReadTimeoutHandler没有投入OioEventLoopGroup

时间:2013-07-29 08:30:21

标签: java netty socketchannel

我是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)而不触及任何其他内容,{{1} }停止投掷ReadTimeoutHandler

1 个答案:

答案 0 :(得分:0)

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

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