0

我的 Netty 服务器从 ChannelHandler 上的 exceptionCaught 方法向客户端发回错误响应。如果客户端消失了,这将导致无限递归。这是堆栈跟踪片段:

at org.jboss.netty.channel.socket.nio.AbstractNioWorker.cleanUpWriteBuffer(AbstractNioWorker.java:778) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromUserCode(AbstractNioWorker.java:501) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:129) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:66) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.Channels.write(Channels.java:733) [netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.Channels.write(Channels.java:694) [netty-3.5.7.Final.jar:na]
at com.inversoft.cleanspeak.store.content.server.netty.ContentStoreChannelHandler.exceptionCaught(ContentStoreChannelHandler.java:68) ~[cleanspeak-content-store-2.3.jar:2.3]
at org.jboss.netty.handler.codec.frame.FrameDecoder.exceptionCaught(FrameDecoder.java:378) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.Channels.fireExceptionCaught(Channels.java:533) [netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.cleanUpWriteBuffer(AbstractNioWorker.java:790) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromUserCode(AbstractNioWorker.java:501) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:129) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:66) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.Channels.write(Channels.java:733) [netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.Channels.write(Channels.java:694) [netty-3.5.7.Final.jar:na]
at com.inversoft.cleanspeak.store.content.server.netty.ContentStoreChannelHandler.exceptionCaught(ContentStoreChannelHandler.java:68) ~[cleanspeak-content-store-2.3.jar:2.3]
at org.jboss.netty.handler.codec.frame.FrameDecoder.exceptionCaught(FrameDecoder.java:378) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.Channels.fireExceptionCaught(Channels.java:533) [netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.cleanUpWriteBuffer(AbstractNioWorker.java:790) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromUserCode(AbstractNioWorker.java:501) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:129) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:66) ~[netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.Channels.write(Channels.java:733) [netty-3.5.7.Final.jar:na]
at org.jboss.netty.channel.Channels.write(Channels.java:694) [netty-3.5.7.Final.jar:na]

我的问题是,对我来说测试客户端是否仍然存在于我的 exceptionCaught 方法中的最佳方法是什么?e.getChannel().isOpen() 有效吗?

4

1 回答 1

1

您可以使用 channel.isConnected() 进行检查,但这可能无法解决,因为写入是异步的,因此当实际写入发生时它可能不再正确。我大部分时间都是这样的:

Channel channel = ...
channel.write(data).addListener(ChannelFutureListener.CLOSE);

这将确保在写入尝试后关闭通道。

于 2012-10-16T04:46:44.587 回答