我是网络新手。这是预期的行为吗?
更详细一点:
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
.