【发布时间】:2012-03-23 07:48:51
【问题描述】:
Netty Channel.close() 偶尔会挂起。在我们的特定用例中,我们有一个通道池,我们的测试检查对网络故障的容忍度。因此,在我们的测试中,我们尝试关闭一个频道。
在下面的代码 sn-p 中,我们在调用 Channel.close() 之前、Channel.close() 之后和 ChannelFuture.await() 之后打印调试语句。为了确保线程没有被中断,我们检查 InterruptedException。
Channel c = partitionChannelMap.get(partition);
if (c != null) {
for (int retries = 0; retries < numRetries; retries++) {
try {
logger.debug("Attempt {}: Closing channel to partition {}", retries + 1, partition);
logger.debug("Channel Properties - isBound() isConnected() isOpen() " + c.isBound() + " "
+ c.isConnected() + " " + c.isOpen());
ChannelFuture closeFuture = c.close();
logger.debug("About to wait");
closeFuture.await(nettyTimeout);
if (closeFuture.isSuccess()) {
logger.debug("Attempt {}: CLOSED channel to partition {}", retries + 1, partition);
partitionChannelMap.remove(partition);
break;
} else {
logger.error("Attempt {}: FAILED to close partition {}", retries + 1, partition);
continue;
}
} catch (InterruptedException e) {
logger.error("Attempt {}: FAILED to close partition {}", retries + 1, partition);
e.printStackTrace();
continue;
}
}
}
}
在某些运行(错误的)中,Channel.close() 之前的调试语句会被执行,而紧随其后的则不会。由于 Channel.close() 是异步的,我们希望它立即返回。在这些情况下,调用 Channel.close() 后执行会挂起。
我在这里假设或做错了什么?
错误执行的示例输出 -
15:12:32.497 [Thread-7] DEBUG org.apache.s4.comm.tcp.TCPEmitter - Attempt 1: Closing channel to partition 0
15:12:32.497 [Thread-7] DEBUG org.apache.s4.comm.tcp.TCPEmitter - Channel Properties - isBound() isConnected() isOpen() true true true
我非常感谢您对此的任何帮助。
谢谢
【问题讨论】:
标签: netty