Netty学习——源码篇8 Outbound/Inbound传播方式 备份

接上篇:Netty学习——源码篇7 Pipeline的事件传播机制

1 Outbound事件传播方式

        Outbound事件都是请求事件(Request Event),即请求某件事情的发生,然后通过Outbound事件进行通知。

        Outbound事件的传播方向是从Tail到customContext再到Head。下面以Connect事件为例,分析一下Outbound事件的传播机制。

        首先,当用户调用了Bootstrap的connect方法时,就会触发一个Connect请求事件,此调用会触发调用链,如下图所示。

fc9bce2ba489418d81e6d6f2fe56b78e.jpeg

        继续跟踪,发现AbstractChannel的connect方法又调用了DefaultChannelPipeline的connect 方法,代码如下:

    public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
        return pipeline.connect(remoteAddress, localAddress, promise);
    }

         而pipeline.connect()方法的代码如下:

    public final ChannelFuture connect(
            SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
        return tail.connect(remoteAddress, localAddress, promise);
    }

        可以看到,当Outbound事件(这里是Connect事件)传递到Pipeline后,其实是以Tail为起点开始传播的。

        而tail.connect调用的是AbstractChannelHandlerContext的connect方法。

 public ChannelFuture connect(
            final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {

        if (remoteAddress == null) {
            throw new NullPointerException("remoteAddress");
        }
        if (!validatePromise(promise, false)) {
            // cancelled
            return promise;
        }

        final AbstractChannelHandlerContext next = findContextOutbound();
        EventExecutor executor = next.executor();
        if (executor.inEventLoop()) {
            next.invokeConnect(remoteAddress, localAddress, promise);
        } else {
            safeExecute(executor, new Runnable() {
                @Override
                public void run() {
                    next.invokeConnect(remoteAddress, localAddress, promise);
                }
            }, promise, null);
        }
        return promise;
    }

        顾名思义,findContextOutbound 方法的作用是以当前Context为起点,向Pipeline中Context双向链表的前段寻找第一个Outbound属性为true的Context(即关

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geminigoth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值