ChannelPipeline

pipeline中维护入站和出站链路,两条链路的执行顺序。

handler只负责处理自身的业务逻辑,对通道而言,它是无状态的。通道的信息会保存到handlerContext处理器上下文中,它是连接pipeline和handler之间的中间角色。

pipeline管理的是由handlerContext包裹的handler,也就是说,当添加handler时,先将其转为handlerContext,然后添加到pipeline的双向链表中。头结点叫做HeadContext,尾节点叫做TailContext。

 ch.pipeline().addLast(new NettyServerHandler());[DefaultChannelPipeline]----------------------------------------------------------------public final ChannelPipeline addLast(ChannelHandler... handlers) {return addLast(null, handlers);}public final ChannelPipeline addLast(EventExecutorGroup executor, ChannelHandler... handlers) {ObjectUtil.checkNotNull(handlers, "handlers");for (ChannelHandler h: handlers) {if (h == null) {break;}addLast(executor, null, h);}return this;}// 关键逻辑public final ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler) {final AbstractChannelHandlerContext newCtx;synchronized (this) {// 检查当前handler是否支持共享,如果不支持,又被添加到其他pipeline中,会报错checkMultiplicity(handler);// 将handler封装为contextnewCtx = newContext(group, filterName(name, handler), handler);// 将context添加到链表尾部addLast0(newCtx);// If the registered is false it means that the channel was not registered on an eventLoop yet.// In this case we add the context to the pipeline and add a task that will call// ChannelHandler.handlerAdded(...) once the channel is registered.// 判断当前通道的注册状态,如果是未注册,执行此逻辑if (!registered) {// 添加一个任务,当通道被注册后,能够回调handlerAdded方法newCtx.setAddPending();callHandlerCallbackLater(newCtx, true);return this;}// 如果已被注册  执行调用handlerAdded方法EventExecutor executor = newCtx.executor();if (!executor.inEventLoop()) {callHandlerAddedInEventLoop(newCtx, executor);return this;}}callHandlerAdded0(newCtx);return this;}private static void checkMultiplicity(ChannelHandler handler) {if (handler instanceof ChannelHandlerAdapter) {ChannelHandlerAdapter h = (ChannelHandlerAdapter) handler;if (!h.isSharable() && h.added) {throw new ChannelPipelineException(h.getClass().getName() +" is not a @Sharable handler, so can't be added or removed multiple times.");}h.added = true;}}private AbstractChannelHandlerContext newContext(EventExecutorGroup group, String name, ChannelHandler handler) {return new DefaultChannelHandlerContext(this, childExecutor(group), name, handler);}// 尾节点会提前声明并创建final AbstractChannelHandlerContext tail;//  prev -> tail   在其中插入newctx//  prev -> newctx -> tail   放到倒数第二个位置中  tail节点是保持不变的//  依次更改 新节点的前后指针   以及prev节点的后置指针和tail节点的前置指针private void addLast0(AbstractChannelHandlerContext newCtx) {AbstractChannelHandlerContext prev = tail.prev;newCtx.prev = prev;newCtx.next = tail;prev.next = newCtx;tail.prev = newCtx;}// 构造器中已经提前创建了头尾节点protected DefaultChannelPipeline(Channel channel) {this.channel = ObjectUtil.checkNotNull(channel, "channel");succeededFuture = new SucceededChannelFuture(channel, null);voidPromise =  new VoidChannelPromise(channel, true);tail = new TailContext(this);head = new HeadContext(this);head.next = tail;tail.prev = head;}

使用pipeline模式的优点:
A) 解耦,让处理器逻辑独立,可以被多个channel共享
B) channel相关信息,交给context维护
C) 具有极大的灵活性,使用处理器可以方便的添加或删除,或者更改它们的顺序

Netty原理:pipeline相关推荐

  1. Netty原理-Pipeline

    Pipeline初始化 在Netty中一个Channel对应一个Pipeline,在AbstractChannel的构造函数中会进行Pipeline的构造, 默认会创建两个Context,一个是Hea ...

  2. 精尽 Netty 原理与源码专栏( 已经完成 61+ 篇,预计总共 70+ 篇 )

    只更新在笔者的知识星球,欢迎加入一起讨论 Netty 源码与实现. 目前已经有 1000+ 位球友加入- 进度:已经完成 60+ 篇,预计总共 70+ 篇,完成度 90% . 对应 Netty 版本号 ...

  3. (高级)Dubbo 第五章 Dubbo及RocketMQ底层-Netty原理

    Netty原理 Netty 是一个高性能.异步事件驱动的NIO 框架,基于JAVA NIO 提供的API 实现.它提供了对TCP.UDP 和文件传输的支持,作为一个异步NIO 框架,Netty 的所有 ...

  4. netty 进程挂起_这可能是目前最透彻的Netty原理架构解析

    本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件.整体架构,知其然且知其所以然,希望给大家在实际开发实践.学习开源项目方面提供参考. Netty 是一个异步事件驱动的网络应用程序 ...

  5. Netty原理和使用

    Netty是一个高性能 事件驱动的异步的非堵塞的IO(NIO)框架,用于建立TCP等底层的连接,基于Netty可以建立高性能的Http服务器.支持HTTP. WebSocket .Protobuf. ...

  6. Netty原理架构解析

    本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件.整体架构,知其然且知其所以然,希望给大家在实际开发实践.学习开源项目方面提供参考. Netty 是一个异步事件驱动的网络应用程序 ...

  7. 这可能是目前最透彻的Netty原理架构解析

    转载自:https://www.toutiao.com/i6620280257846968840/?tt_from=weixin&utm_campaign=client_share&w ...

  8. Netty系列二、Netty原理篇

    文章目录 一.Netty概述 二.Netty整体架构设计 1.Reactor模型 2.Reactor模型分类 2.1 单Reactor单线程 2.2 单Reactor多线程 2.3 多Reactor多 ...

  9. 【Netty】Netty 核心组件 ( Pipeline | ChannelPipeline )

    文章目录 一. Pipeline / ChannelPipeline 管道组件 二. Pipeline / ChannelPipeline 管道组件元素解析 一. Pipeline / Channel ...

  10. Netty原理五:ChannelFuture、DefaultChannelPromise对象解析

    文章目录 1. 前言 2. 原理解析 2.1 ChannelFuture 调用 sync() 的作用 2.2 Channel 调用的 closeFuture() 是什么 1. 前言 学习Netty的时 ...

最新文章

  1. Libevent实现TCP服务循环监听
  2. Delphi中取得和设置硬盘上文件的创建日期、修改日期、访问日期、文件属性
  3. 7.Verilog 条件语句的应用
  4. 【转:SAP学习篇】Fiori 的基本架构
  5. boost log 能不能循环覆盖_记一次for循环中let是声明还是赋值
  6. SpringMVC异常处理之异常处理代码编写
  7. SLAM: Inverse Depth Parametrization for Monocular SALM
  8. 滑动切换activity
  9. slowfast 跑多卡的时候遇到问题
  10. ad18修改过孔和走线间距_PCB设计之“过孔”
  11. Linux C语言解析 yaml,c – 用yaml cpp解析yaml
  12. 失焦事件触发_JavaScript event 事件详解
  13. JS中一些常用的函数(持续更新)
  14. 微信小程序地图定位当前位置
  15. 女生学前端适合么?新人应该怎么学习?
  16. DDG-1000下水
  17. wshop微信商城数据库结构简要解析
  18. 产品经理培训课程:产品经理从专业走向管理
  19. JavaScript进阶-编程思想、构造函数的原型对象、对象原型、原型继承以及原型链
  20. gCastle | 华为诺亚方舟实验室自研的因果结构学习工具链

热门文章

  1. OpenRefine安装使用
  2. 国产服务器虚拟化产品,国内主流虚拟化厂商之间比较.doc
  3. hs8346v5联通 说明书_中兴Memo V5S说明书
  4. 文献阅读|Nomograms列线图在肿瘤中的应用
  5. EPS FB信令流程
  6. flashtool线刷工具
  7. 【源码部署】Linux系统部署suricata
  8. 剧情插件Cutscene Creator uSequencer 1.3.7.1使用说明一
  9. 微弱信号检测技术在机械早期故障探查中的应用研究
  10. 1481c语言合法标识符,c语言试题答案集