先看channelRead方法,直接上源码
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
boolean release = true;
try {
if (this.acceptInboundMessage(msg)) {
this.channelRead0(ctx, msg);
} else {
release = false;
ctx.fireChannelRead(msg);
}
} finally {
if (this.autoRelease && release) {
ReferenceCountUtil.release(msg);
}
}
}
再看channelRead0方法,只有一行:
protected abstract void channelRead0(ChannelHandlerContext var1, I var2) throws Exception;
可以很明显的看到,channelRead 是public 类型,可以被外部访问;而channelRead0是protected类型,只能被当前类及其子类访问。
channelRead中调用了channelRead0,那么channelRead又额外多做了什么呢?
很明显做了一个消息类型检查,判断是否会传递到下一个handler