【发布时间】:2014-11-24 15:57:08
【问题描述】:
我正在尝试关闭来自 netty 服务器的空闲连接,在空闲超时时间之后,使用 IdleStateHandler,类似于http://netty.io/3.9/api/org/jboss/netty/handler/timeout/IdleStateHandler.html。 有些对我不起作用。调试问题并发现Timer实际上是在超时后触发“IdleStateEvent”事件,它在添加到队列“AbstractNioSelector.taskQueue”后从“IdleStateHandler.fireChannelIdle”方法中丢失。它是在 3.5.9.Final 中引入的,但如果我使用 netty 3.5.8.Final 版本,它可以正常工作。
我绑定的是netty 3.9.x版本,现在用的是3.9.4.Final。
受影响的版本:3.5.9.Final 到 3.9.5.Final
如何重现这个。 以下代码在 1 分钟后关闭空闲连接,如果我使用 netty 3.5.8.Final,但任何更高版本的 3.x 库都不能这样做。
public class PipelineFactory implements ChannelPipelineFactory {
...
public PipelineFactory(Timer timer) {
this.timer = timer;
this.chHandler = new EchoServerHandler();
}
@Override
public ChannelPipeline getPipeline() throws Exception {
final ChannelPipeline pipeline = Channels.pipeline(new IdleStateHandler(this.timer, 0, 0, 60),
new MyIdleStateAwareHandler());
pipeline.addLast("handler", chHandler);
return pipeline;
}
}
class MyIdleStateAwareHandler extends IdleStateAwareChannelHandler {
...
@Override
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) {
if (e.getState() == IdleState.ALL_IDLE) {
logger.info("Closed idle socket.");
e.getChannel().close();
}
}
}
class EchoServerHandler extends SimpleChannelHandler {
...
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
try {
Thread.sleep(600000);
}
catch(InterruptedException iex) {
logger.error("interrupted " + iex.getMessage());
}
ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
while(buffer.readable()) {
System.out.print((char) buffer.readByte());
System.out.flush();
}
}
}
非常感谢解决此问题的任何帮助或指示。
【问题讨论】:
标签: netty