深入了解Netty之ChannelHandler与Pipeline(六)

本文深入探讨了Netty中异常的传播机制,指出异常在发生后会沿着ChannelHandler的Pipeline,不论入站还是出站处理器,一直向下一个节点传播,直到被处理或到达tail节点。通过示例分析了异常从抛出到传播的详细过程,强调了在Pipeline中定义专门的异常处理器的重要性。
摘要由CSDN通过智能技术生成

2021SC@SDUSC


异常的传播

在Netty中如果发生了异常,那么异常事件的传播和当前的节点是 入站处理器还是出站处理器是无关的,异常会一直往下一个节点传播,如果一直没有handler处理异常,最后会由tail节点处理

为了说明这一点,看一个例子:

public class InBoundHandlerA extends ChannelInboundHandlerAdapter {
   
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
   
        System.out.println("InBoundHandlerA.exceptionCaught()");
        ctx.fireExceptionCaught(cause);
    }
}
public class InBoundHandlerB extends ChannelInboundHandlerAdapter {
   
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{
   
        throw new Exception("from inboundHandlerB");
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
   
        System.out.println("InBoundHandlerB.exceptionCaught()");
        ctx.fireExceptionCaught(cause);
    }
}
public class InBoundHandlerC extends ChannelInboundHandlerAdapter {
   
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
   
        System.out.println("InBoundHandlerC.exceptionCaught()");
        ctx.fireExceptionCaught(cause);
    }
}
public class OutBoundHandlerA extends ChannelOutboundHandlerAdapter {
   
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
   
        System.out.println("OutBoundHandlerA.exceptionCaught()");
        ctx.fireExceptionCaught(cause);
    }
}
public class OutBoundHandlerB extends ChannelOutboundHandlerAdapter {
   
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
   
        System.out.println("OutBoundHandlerB.exceptionCaught()");
        ctx.fireExceptionCaught(cause);
    }
}
public class OutBoundHandlerC extends 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值