ChannelHandler中异常的获取与处理是通过继承重写exceptionCaught方法来实现的,本篇文章我们对ChannelPipeline中exceptionCaught异常事件的传播进行梳理分析

1、出站事件的传播示例

首先我们继续在之前的代码上进行改造,模拟异常事件的传播

public class ServerApp {
public static void main(String[] args) {
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup work = new NioEventLoopGroup(2);
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.childOption(ChannelOption.SO_SNDBUF,2);
bootstrap.group(boss, work).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
// p.addLast(new LoggingHandler(LogLevel.INFO));
// 向ChannelPipeline中添加自定义channelHandler
p.addLast(new OutHandlerA());
p.addLast(new ServerHandlerA());
p.addLast(new ServerHandlerB());
p.addLast(new ServerHandlerC());
p.addLast(new OutHandlerB());
p.addLast(new OutHandlerC()); }
});
bootstrap.bind(8050).sync(); } catch (Exception e) {
// TODO: handle exception
} } } public class OutHandlerA extends ChannelOutboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
System.err.println(this.getClass().getName()+"---"+cause.getMessage());
ctx.fireExceptionCaught(cause);
}
} public class OutHandlerB extends ChannelOutboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
System.err.println(this.getClass().getName()+"---"+cause.getMessage());
ctx.fireExceptionCaught(cause);
}
} public class OutHandlerC extends ChannelOutboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
System.err.println(this.getClass().getName()+"---"+cause.getMessage());
ctx.fireExceptionCaught(cause);
}
} public class ServerHandlerB extends ChannelInboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
System.err.println(this.getClass().getName()+"---"+cause.getMessage());
ctx.fireExceptionCaught(cause);
}
} public class ServerHandlerC extends ChannelInboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
System.err.println(this.getClass().getName()+"---"+cause.getMessage());
ctx.fireExceptionCaught(cause);
}
}

然后我们在ServerHandlerA的channelRead方法中执行ctx的write方法,模拟异常事件的发生。

    @Override
public void channelRead(ChannelHandlerContext ctx, Object object) {
ctx.fireExceptionCaught(new Throwable("出现异常"));
//ctx.pipeline().fireExceptionCaught(new Throwable("出现异常")); }

我们首先看下运行结果

ctx.fireExceptionCaught

io.netty.example.echo.my.ServerHandlerB---出现异常
io.netty.example.echo.my.ServerHandlerC---出现异常
io.netty.example.echo.my.OutHandlerB---出现异常
io.netty.example.echo.my.OutHandlerC---出现异常
18:34:17.147 [nioEventLoopGroup-3-1] WARN i.n.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.lang.Throwable: 出现异常
at io.netty.example.echo.my.ServerHandlerA.channelRead(ServerHandlerA.java:39)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:338)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1424)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:944)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:709)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:639)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:553)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:510)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:912)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)

ctx.pipeline().fireExceptionCaught

io.netty.example.echo.my.OutHandlerA---出现异常
io.netty.example.echo.my.ServerHandlerA---出现异常
io.netty.example.echo.my.ServerHandlerB---出现异常
io.netty.example.echo.my.ServerHandlerC---出现异常
io.netty.example.echo.my.OutHandlerB---出现异常
io.netty.example.echo.my.OutHandlerC---出现异常
20:08:53.723 [nioEventLoopGroup-3-1] WARN i.n.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.lang.Throwable: 出现异常
at io.netty.example.echo.my.ServerHandlerA.channelRead(ServerHandlerA.java:40)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:338)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1424)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:944)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:709)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:639)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:553)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:510)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:912)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)

根据输出结果可以看出ctx.fireExceptionCaught 会从异常产生的ChannelHandler一直往后传播到tail尾节点,ctx.pipeline().fireExceptionCaught会从管道中第一个节点一直往后传播到tail尾节点,而上面结果中打印的异常信息则是在TailContext尾节点中统一处理的。

2、异常事件传播的分析

ctx.pipeline().fireExceptionCaught与ctx.fireExceptionCaught两种传播异常方法

前者调用的是DefaultChannelPipeline 的 fireExceptionCaught方法

    @Override
public final ChannelPipeline fireExceptionCaught(Throwable cause) {
AbstractChannelHandlerContext.invokeExceptionCaught(head, cause);
return this;
}

后者调用的是AbstractChannelHandlerContext 的 fireExceptionCaught方法

    @Override
public ChannelHandlerContext fireExceptionCaught(final Throwable cause) {
invokeExceptionCaught(next, cause);
return this;
}

可以看到DefaultChannelPipeline的fireExceptionCaught方法中默认传入了head头部节点,所以ctx.pipeline().fireExceptionCaught会从管道中第一个节点开始向后传播。

我们进入invokeExceptionCaught方法内部看下具体实现

    static void invokeExceptionCaught(final AbstractChannelHandlerContext next, final Throwable cause) {
ObjectUtil.checkNotNull(cause, "cause");//检查异常是否为空
EventExecutor executor = next.executor();
if (executor.inEventLoop()) {//判断是否与当前线程一直
next.invokeExceptionCaught(cause);//触发回调,触发下一个AbstractChannelHandlerContext节点中handler的异常处理事件
} else {
try {
executor.execute(new Runnable() {//如果线程不一致,由其绑定的executor执行
@Override
public void run() {
next.invokeExceptionCaught(cause);
}
});
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn("Failed to submit an exceptionCaught() event.", t);
logger.warn("The exceptionCaught() event that was failed to submit was:", cause);
}
}
}

invokeExceptionCaught方法内部实现

    private void invokeExceptionCaught(final Throwable cause) {
if (invokeHandler()) {//判断当前handler的状态
try {
handler().exceptionCaught(this, cause);//调用exceptionCaught方法实现
} catch (Throwable error) {
if (logger.isDebugEnabled()) {
logger.debug(
"An exception {}" +
"was thrown by a user handler's exceptionCaught() " +
"method while handling the following exception:",
ThrowableUtil.stackTraceToString(error), cause);
} else if (logger.isWarnEnabled()) {
logger.warn(
"An exception '{}' [enable DEBUG level for full stacktrace] " +
"was thrown by a user handler's exceptionCaught() " +
"method while handling the following exception:", error, cause);
}
}
} else {
fireExceptionCaught(cause);
}
}

3、异常处理机制的设计

通过上面的分析我们可以看到如果通过ctx.fireExceptionCaught一直向后传递异常事件,最终会触发尾节点的exceptionCaught事件打印异常日志;

        @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
onUnhandledInboundException(cause);
}
    protected void onUnhandledInboundException(Throwable cause) {
try {
logger.warn(
"An exceptionCaught() event was fired, and it reached at the tail of the pipeline. " +
"It usually means the last handler in the pipeline did not handle the exception.",
cause);
} finally {
ReferenceCountUtil.release(cause);
}
}

在实际项目中我们可以在ChannelPipeline尾部增加一个异常处理handle用来统一处理异常信息;

        public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
// p.addLast(new LoggingHandler(LogLevel.INFO));
// 向ChannelPipeline中添加自定义channelHandler
p.addLast(new OutHandlerA());
p.addLast(new ServerHandlerA());
p.addLast(new ServerHandlerB());
p.addLast(new ServerHandlerC());
p.addLast(new OutHandlerB());
p.addLast(new OutHandlerC());
p.addLast(new ExceptionHandler()); }

通过以上三点内容我们对异常信息在ChannelPipeline中的传播进行了模拟,梳理事件的传播流程以及应该怎样统一处理异常信息,其中如有不足与不正确的地方还望指出与海涵。

关注微信公众号,查看更多技术文章。

Netty源码分析之ChannelPipeline—异常事件的传播的更多相关文章

  1. 【Netty源码分析】ChannelPipeline(二)

    在上一篇博客[Netty源码学习]ChannelPipeline(一)中我们只是大体介绍了ChannelPipeline相关的知识,其实介绍的并不详细,接下来我们详细介绍一下ChannelPipeli ...

  2. Netty源码分析之ChannelPipeline—入站事件的传播

    之前的文章中我们说过ChannelPipeline作为Netty中的数据管道,负责传递Channel中消息的事件传播,事件的传播分为入站和出站两个方向,分别通知ChannelInboundHandle ...

  3. Netty源码分析之ChannelPipeline—出站事件的传播

    上篇文章中我们梳理了ChannelPipeline中入站事件的传播,这篇文章中我们看下出站事件的传播,也就是ChannelOutboundHandler接口的实现. 1.出站事件的传播示例 我们对上篇 ...

  4. Netty源码分析之ChannelPipeline(二)—ChannelHandler的添加与删除

    上篇文章中,我们对Netty中ChannelPipeline的构造与初始化进行了分析与总结,本篇文章我们将对ChannelHandler的添加与删除操作进行具体的的代码分析: 一.ChannelHan ...

  5. [编织消息框架][netty源码分析]6 ChannelPipeline 实现类DefaultChannelPipeline职责与实现

    ChannelPipeline 负责channel数据进出处理,如数据编解码等.采用拦截思想设计,经过A handler处理后接着交给next handler ChannelPipeline 并不是直 ...

  6. Netty源码分析之ChannelPipeline(一)—ChannelPipeline的构造与初始化

    Netty中ChannelPipeline实际上类似与一条数据管道,负责传递Channel中读取的消息,它本质上是基于责任链模式的设计与实现,无论是IO事件的拦截器,还是用户自定义的ChannelHa ...

  7. netty源码分析系列文章

    netty源码分析系列文章 nettynetty源码阅读netty源码分析  想在年终之际将对netty研究的笔记记录下来,先看netty3,然后有时间了再写netty4的,希望对大家有所帮助,这个是 ...

  8. Netty源码分析第4章(pipeline)---->第6节: 传播异常事件

    Netty源码分析第四章: pipeline 第6节: 传播异常事件 讲完了inbound事件和outbound事件的传输流程, 这一小节剖析异常事件的传输流程 首先我们看一个最最简单的异常处理的场景 ...

  9. Netty源码分析第4章(pipeline)---->第4节: 传播inbound事件

    Netty源码分析第四章: pipeline 第四节: 传播inbound事件 有关于inbound事件, 在概述中做过简单的介绍, 就是以自己为基准, 流向自己的事件, 比如最常见的channelR ...

随机推荐

  1. coding++:idea提交SVN或GIT时,忽略某些文件

    设置步骤:Settings→Editor→File Types在窗口最下方“Ignore files and folders”一栏中添加如下忽略: *.iml;*.idea;*.gitignore;* ...

  2. Mysql 随笔记录

    Soundex 声音相似的 select * from demos where Soundex('title') = Soundex('标示'); Concat 拼接语句 select concat( ...

  3. Java构造器(构造方法/constructor)

    我们先来看一下什么是构造器: 1.构造器也叫构造方法或构造函数,分为有参构造器和无参构造器: 2.构造器也是一种方法,只不过是一种特殊的方法,它会在对象创建的时候被调用: 3.构造器最大的作用就是在创 ...

  4. ThreadAbortException是可以传递的

    今天在写线程Aborted代码时,发现嵌套的try catch中的ThreadAbortException错误是可以从内部传递到外部的,想想这也是必然的,在内部该线程已经中断了,外部必然是中断了,再仔 ...

  5. Java 判断 循环

    一.优先级 1.1 先判断5>3,true 6>4 true;然后true==true ,最后是true; 1.2 6>5,true;而true和4无法比较.所以该判断出错: 1.3 ...

  6. STM32F103ZET6的中断管理

    1.STM32的中断 STM32的中断管理是属于内核部分的,所以中断管理的寄存器也是属于内核组,不属于芯片外设,在查看相关资料的时候,需要查看相对应的内核手册. STM32F103ZET6是Corte ...

  7. PTA | 1009说反话(20分)

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有 ...

  8. Docker+Jmeter+InfluxDB+Grafana搭建性能测试监控平台

    搭建需求? jmeter自身的聚合测试报告可视化效果极差,为更加形象的.动态的展示测试过程,需要一个具有时序性的可视区来展示给我们的测试者, 这时候就需要用到后端监控,下面我们来开始搭建符合这种测试需 ...

  9. Python爬虫系列(四):Beautiful Soup解析HTML之把HTML转成Python对象

    在前几篇文章,我们学会了如何获取html文档内容,就是从url下载网页.今天开始,我们将讨论如何将html转成python对象,用python代码对文档进行分析. (牛小妹在学校折腾了好几天,也没把h ...

  10. Java课程设计之——Web前端

    主要使用的技术 Javascript/JQuery html css Jsp 前期调查 能看到的网页大致可以分为两个部分,一个是搜索的首页index.html,一个是搜索结果页/s index.htm ...