【Netty专题】Netty 中channelRead 和 channelRead0 有什么区别?

最近做项目发现Netty中的channel有channelRead 方法也有channelRead0 方法,
然后思考下这两个有什么区别呢?
我们来直接看源码

channelRead

@Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        boolean release = true;
        try {
            if (acceptInboundMessage(msg)) {
                @SuppressWarnings("unchecked")
                I imsg = (I) msg;
                channelRead0(ctx, imsg);
            } else {
                release = false;
                ctx.fireChannelRead(msg);
            }
        } finally {
            if (autoRelease && release) {
                ReferenceCountUtil.release(msg);
            }
        }
    }

channelRead0

protected abstract void channelRead0(ChannelHandlerContext ctx, I msg) throws Exception;
  • 可以很明显的看到,channelRead 是public 类型,可以被外部访问;而channelRead0是protected类型,只能被当前类及其子类访问。
  • channelRead中调用了channelRead0,那么channelRead又额外多做了什么呢?
/**
     * Returns {@code true} if the given message should be handled. If {@code false} it will be passed to the next
     * {@link ChannelInboundHandler} in the {@link ChannelPipeline}.
     */
    public boolean acceptInboundMessage(Object msg) throws Exception {
        return matcher.match(msg);
    }
  • 很明显做了一个消息类型检查,判断是否会传递到下一个handler

总结:
channelRead 中调用了 channelRead0,其会先做消息类型检查,判断当前message 是否需要传递到下一个handler。

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值