當我從客戶端異步發送消息並關閉通道時,Netty將拋出以下異常。發送異步消息並關閉通道後發生ClosedChannelException
java.nio.channels.ClosedChannelException
at sun.nio.ch.SocketChannelImpl.ensureWriteOpen(SocketChannelImpl.java:133)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:324)
at org.jboss.netty.channel.socket.nio.SocketSendBufferPool$PooledSendBuffer.transferTo(SocketSendBufferPool.java:239)
at org.jboss.netty.channel.socket.nio.NioWorker.write0(NioWorker.java:469)
at org.jboss.netty.channel.socket.nio.NioWorker.writeFromTaskLoop(NioWorker.java:392)
at org.jboss.netty.channel.socket.nio.NioSocketChannel$WriteTask.run(NioSocketChannel.java:276)
at org.jboss.netty.channel.socket.nio.NioWorker.processWriteTaskQueue(NioWorker.java:268)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:199)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
我發送代碼如下:
Channels.write(clientChannel, messageObject);
我身邊的代碼如下:
ChannelGroupFuture future = ALL_CHANNELS.close();
future.awaitUninterruptibly();
if (null != clientBootstrap) {
clientBootstrap.releaseExternalResources();
}
有什麼我需要做收盤前沖洗通道?
非常好。這讓我煩惱了好幾天。我找到的Netty例子都沒有這樣做。 – DarVar 2011-12-20 11:38:42
沒有說話很快。這沒有奏效。由於通道關閉,後續消息發送失敗。 – DarVar 2011-12-20 11:56:35
我認爲您只需要在完成發送/接收後即關閉頻道時 - 即在最後一次寫入之後。 – Veebs 2011-12-20 20:39:05