【发布时间】:2017-07-11 03:28:09
【问题描述】:
我正在使用 Netty 4.1.12.Final 编写一个 HTTP 客户端,并且我有单元测试来模拟 HTTP 服务器的崩溃,以便能够处理它。 我注意到,当它发生时,我的入站处理程序的 exceptionCaught 回调方法被调用:
java.io.IOException: Une connexion existante a dû être fermée par l’hôte distant
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(Unknown Source)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.read(Unknown Source)
at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100)
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:372)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:123)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:644)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:579)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:496)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:458)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138)
异常消息的英文等价物很可能是:
java.io.IOException: An existing connection was forcibly closed by the remote host
由于当我的入站处理程序的 channelRead0 方法引发异常时也会调用此回调方法,所以我要问几个问题:
- 我是否应该始终将 exceptionCaught 回调中“收到”的 IOException 视为没有继续使用通道的指示?
- 既然 channelRead0 被声明为抛出异常,我是否应该在其中捕获所有 IOException 以确保在 exceptionCaught 回调中“接收”一个 IOException 时,它与 Channel 有关?
- 有没有办法知道在 exceptionCaught 回调中“接收到”的异常是否与 I/O 操作或处理程序操作有关?
感谢您的任何提示!
【问题讨论】:
-
我忘了说当我收到 IOException 时:ctx.channel().isActive()、isOpen()、isRegistered() 和 .isWritable() 都是“真”