在我的渠道管道中,有許多處理程序。在Netty 4中,我如何捕獲所有處理程序的未處理的exceptionCaught?
按我的理解,如果我不覆蓋其exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
方法,默認行爲是cause
會拋出的管道,並通過管道WARN級別這樣的事情會被記錄:
An exception was thrown by a user handler's exceptionCaught() method while handling the following exception: ...
我想覆蓋上面的管道行爲來添加一些特定的邏輯(例如:如果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);
}
}
}
因爲它是private
,我不認爲我可以很容易地覆蓋它,而無需使用反射。有沒有更好的辦法?
我已檢查過。這個解決方案有效謝謝! –