netty 事件驱动(一)

本篇文章着重于浅析一下Netty的事件处理流程,Netty版本为netty-3.6.6.Final。

Netty定义了非常丰富的事件类型,代表了网络交互的各个阶段。并且当各个阶段发生时,触发相应的事件交给pipeline中定义的handler处理。

举个例子,如下一段简单的代码:

复制代码
ChannelFactory factory =
            new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
        ServerBootstrap bootstrap = new ServerBootstrap(factory);

        bootstrap.setPipelineFactory(new PipelineFactory());

        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.keepAlive", true);

        bootstrap.bind(new InetSocketAddress(7080));
复制代码

Netty中触发事件几乎都是靠Channels类中的几个静态fire函数,因此通过在这些函数中加上Sysout方法,就可以看出这一个简单的bind方法触发了多少事件,如下:

fireChannelOpen(final Channel channel) upstream
bind(final Channel channel, final SocketAddress localAddress) downstream
fireChannelBound(final Channel channel, final SocketAddress localAddress) upstream

由此可见由这几个函数触发了OPEN、BOUND和BIND事件。

 

Netty中的事件大致可以分为upstream事件和downstream事件。简单的说,upstream事件是内获取外资源时触发的事件如messageReceived等等,而downstream事件则是内向外发送资源时触发的事件如write、connect等等。

与之相对应的,处理upstream事件的是upstreamhandler,处理downstream事件的是downstreamhandler,也有可以处理两类事件的channelhandler。我们可以通过继承handler来实现自己的业务逻辑。

Upstream事件的典型是messageReceived,在Netty中抽象为MessageEvent,即接收到了消息。而downstream事件的典型是write,在Netty中也抽象为MessageEvent,即发送消息。一个比较完整的事件表如下:

upstream事件包括:

downstream事件包括

 

Netty通过pipeline来存放upstreamhandler和downstreamhandler,在pipeline中添加handler的源代码如下:

复制代码
public class PipelineFactory implements ChannelPipelineFactory
{
    public ChannelPipeline getPipeline()
        throws Exception
    {
        ChannelPipeline pipeline = Channels.pipeline();

        //并不具体处理事件,只是输出相关事件的string
        pipeline.addLast("1", new UpStreamHandler1());
        //单纯的丢弃事件
        pipeline.addLast("2", new DiscardServer());
        return pipeline;
    }
}
复制代码

在Netty中,upstreamhandler的处理顺序是从前向后,而downstreamhandler的顺序是从后往前。根本原因是pipeline中维护了一个双向链表,handler的处理顺序不同是因为upstream是从head->tail遍历,而downstream事件是从tail->head遍历。

以DefaultChannelPipeline为例,以下分别是添加handler至链表的代码和访问upstreamhandler的代码

复制代码
    public synchronized void addLast(String name, ChannelHandler handler) {
        if (name2ctx.isEmpty()) {
            init(name, handler);
        } else {
            checkDuplicateName(name);
            //一段典型的插入到链表尾部并更新尾指针的代码
            DefaultChannelHandlerContext oldTail = tail;
            DefaultChannelHandlerContext newTail = new DefaultChannelHandlerContext(oldTail, null, name, handler);

            callBeforeAdd(newTail);

            oldTail.next = newTail;
            tail = newTail;
            name2ctx.put(name, newTail);

            callAfterAdd(newTail);
        }
    }
复制代码
复制代码
    public void sendUpstream(ChannelEvent e) {
        //从头部开始遍历,相对的是,downstream则是从尾部开始遍历
        DefaultChannelHandlerContext head = getActualUpstreamContext(this.head);
        if (head == null) {
            if (logger.isWarnEnabled()) {
                logger.warn(
                        "The pipeline contains no upstream handlers; discarding: " + e);
            }

            return;
        }

        sendUpstream(head, e);
    }
复制代码

 

前文已经说过,Netty中通过Channels中的静态方法来触发事件,这些静态函数列举如下:

 1.fireChannelOpen;2.fireChannelBound;3.fireChannelConnected等等。

直接来看fireChannelOpen的源码,看看Netty到底是怎么做的。

复制代码
    public static void fireChannelOpen(final Channel channel) {
        // Notify the parent handler.
        if (channel.getParent() != null) {
            fireChildChannelStateChanged(channel.getParent(), channel);
        }
        channel.getPipeline().sendUpstream(
                new UpstreamChannelStateEvent(
                        channel, ChannelState.OPEN, Boolean.TRUE));
    }
复制代码

这个sendUpstream到底是干嘛的了?

复制代码
    void sendUpstream(final DefaultChannelHandlerContext ctx, final ChannelEvent e) {
        try {
            //从链表头部开始,取出每个节点中的handler直接对channelevent进行处理
            ((ChannelUpstreamHandler) ctx.getHandler()).handleUpstream(ctx, e);
        } catch (Throwable t) {
            notifyHandlerException(e, t);
        }
    }
复制代码

然后具体到handler又是怎么处理各个事件的了?以SimpleChannelUpstreamHandler为例,如下:

复制代码
    public void handleUpstream(
            final ChannelHandlerContext ctx, final ChannelEvent e) throws Exception {
        //根据事件类型进行不同的处理
        if (e instanceof MessageEvent) {
            messageReceived(ctx, (MessageEvent) e);
        } else if (e instanceof WriteCompletionEvent) {
            WriteCompletionEvent evt = (WriteCompletionEvent) e;
            writeComplete(ctx, evt);
        } else if (e instanceof ChildChannelStateEvent) {
            ......
        }
    }
   
    public void messageReceived(
            final ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
        //直接将事件传至下一个handler进行处理
        ctx.sendUpstream(e);
    }
复制代码

源码看到现在已经很明显了,在Netty里,pipeline中维护了一个handler的链表。每当事件触发时,就会从双向链表的头部(对于downstream事件则是尾部)开始遍历,这样每个handler都会对事件进行处理。在handler里,可以根据事件类型做相应的处理后传至下一个handler继续处理(甚至可以截断处理链)。

需要注意的是,单次流程是在一个线程中实现的,是串行的。因此如果其中一个handler是阻塞的,就会影响整体的效果。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

life1024

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

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

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

打赏作者

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

抵扣说明:

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

余额充值