您可以在管道的顶部/尾部仅使用一个入站和出站异常处理程序。如果你想捕获所有异常,你可以这样做(我假设这是 Netty 4.0):
import io.netty.channel.*;
import java.net.SocketAddress;
public class ExceptionHandler extends ChannelDuplexHandler {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Uncaught exceptions from inbound handlers will propagate up to this handler
}
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
ctx.connect(remoteAddress, localAddress, promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
// Handle connect exception here...
Throwable failureCause = future.cause();
}
}
}));
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
ctx.write(msg, promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
// Handle write exception here...
Throwable failureCause = future.cause();
}
}
}));
}
// ... override more outbound methods to handle their exceptions as well
}
入站处理程序引发的任何异常都将“向上”传播管道并调用此处理程序的 exceptionCaught() 方法,假设下面的处理程序不使用它们。
对于像write() 和connect() 这样的出站操作,您需要添加一个ChannelFutureListener 来捕获它们的异常。 exceptionCaught() 方法仅针对来自入站事件的异常调用,例如 channelRead()、channelActive() 等。
在管道的“顶部”使用这个处理程序,我们可以从下面的所有出站处理程序中捕获异常。假设您的一个出站处理程序正在执行一些编码,但由于异常而失败,这将由我们添加到 write() 操作承诺的通道未来侦听器处理。
如果这个异常处理程序像你最初建议的那样安装在管道的“底部”/头部,那么它不会看到来自它上面的处理程序的异常,因为如果写入失败,它的 write() 方法将永远不会被调用以前的处理程序。这就是为什么这个处理程序必须在顶部。
为了避免混淆管道的顶部/底部,我将如何配置您的示例管道:
pipeline.addLast(outboundHandler2) // bottom
.addLast(outboundHandler1)
.addLast(inboundHandler2)
.addLast(inboundHandler1)
.addLast(new ExceptionHandler()); // top