在我的渠道管道中,有许多处理程序.
据我所知,如果我不覆盖他们的exceptionCaught(ChannelHandlerContext ctx, Throwable cause)方法,默认行为是cause将被抛出到管道,这样的东西将由管道记录在WARN级别:
An exception was thrown by a user handler's exceptionCaught() method while handling the following exception: ...
Run Code Online (Sandbox Code Playgroud)
我想覆盖上面的管道行为来添加一些特定的逻辑(例如:如果cause是java.io.IOException: Connection reset by peer,不记录任何东西以避免太多"不太有用"的WARN级别的日志).
我该怎么办?
经过一番调查,我找到了这个源代码:https: //github.com/netty/netty/blob/4.0/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java
private void invokeExceptionCaught(final Throwable cause) {
try {
handler.exceptionCaught(this, cause);
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn(
"An exception was thrown by a user handler's " +
"exceptionCaught() method while handling the following exception:", cause);
}
}
}
Run Code Online (Sandbox Code Playgroud)
因为它是private,我不认为我可以轻易地覆盖它而不使用反射.有没有更好的办法?
您可以通过定义a ExceptionHandler然后将此处理程序放在管道的尾部来完成此操作.
ChannelPipeline p = ch.pipeline();
p.addLast("business", new SomeBusinessHandler());
p.addLast...
p.addLast("exception", new ExceptionHandler());//Make sure this is the last line when init the pipeline.
Run Code Online (Sandbox Code Playgroud)
并在exceptionCaught方法中编写您的特定逻辑.但永远不要重新抛出异常,因为这是管道的终结.
| 归档时间: |
|
| 查看次数: |
3136 次 |
| 最近记录: |