netty异常处理源码解析
netty 3.2.5: 事件处理
前段时间因为dubbo线程池的问题,着手研究了一下netty异常的处理,下面是我的分析,并结合dubbo2.5.3版本说明dubbo服务应用这个异常的方式。
说明: netty版本 3.2.5 ,netty4的代码有很大的变动,慢慢分析原理差不多的,懂NIO的一看就懂。
nettyServer初始化时,默认ChannelPipe : org.jboss.netty.channel.DefaultChannelPipeline
该类的方法:
public void sendUpstream(ChannelEvent e) {
DefaultChannelHandlerContext head = getActualUpstreamContext(this.head); 获取到 head 为空时结束
if (head == null) {
logger.warn(
"The pipeline contains no upstream handlers; discarding: " + e);
return;
}
sendUpstream(head, e);
}
void sendUpstream(DefaultChannelHandlerContext ctx, ChannelEvent e) {
try {
((ChannelUpstreamHandler) ctx.getHandler()).handleUpstream(ctx, e);
} catch (Throwable t) {//调用next handler如果出现问题,执行notifyHandlerException方法
notifyHandlerException(e, t);
}
}
protected void notifyHandlerException(ChannelEvent e, Throwable t) {
if (e instanceof ExceptionEvent) {//如果检测到事件类型为ExceptionEvent,进行日志打印,return,程序返回
logger.warn(
"An exception was thrown by a user handler " +
"while handling an exception eve