0

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

更详细一点:

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初始化从更改NioEventLoopGroupOioEventLoopGroup并将引导通道设置从bootstrap.channel(NioSocketChannel.class)更改为bootstrap.channel(OioSocketChannel.class)而不触及其他任何东西,则ReadTimeoutHandler停止抛出ReadTimeoutExceptions.

4

1 回答 1

0

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

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

于 2013-07-30T07:06:28.270 回答