【问题标题】:How to make the distinction between different exceptions in Netty's onChannelFailure callbackNetty的onChannelFailure回调中如何区分不同的异常
【发布时间】: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() 都是“真”

标签: java netty


【解决方案1】:

1) 如果我们谈论的是 TCP 连接,那么是的,每个 IOException 都会导致连接被 netty 自动关闭,因为无法恢复。

2)我想我不完全理解这个问题,因为通过exceptionCaught(...)方法传递的每个异常都与ctx.channel()可以获取的通道有关

3) 不,一般来说没有办法。也就是说,如果它是一个 TCP 连接并且它是一个 IOException 并且它是由实际传输触发的,我们将关闭该连接。

【讨论】:

  • 谢谢。是的,我说的是 TCP 连接。那么因为ctx.channel().isActive()、isOpen()、isRegistered()和.isWritable()在exceptionCaught内部都是“true”,是不是表示调用了exceptionCaught后channel实际上是关闭的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 2016-03-20
  • 2018-12-07
  • 1970-01-01
相关资源
最近更新 更多