【发布时间】: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