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. 为全局变量赋值_Postman全局变量设置和运用
  2. 一文弄懂用户画像以及如何召回用户
  3. java+spring+mysql配置_用spring的beans配置mysql数据库
  4. sklearn中模型的选择和各个模型的比较
  5. php继承和重载区别,php继承中方法重载(覆盖)的应用场合
  6. 第二节:ES6新增了let关键字,干嘛用的?
  7. Dockerfile文件详解
  8. Linux下Centos7以rpm方式离线安装MySQL5.7教程以及部分报错解决方案
  9. 8. 吴恩达机器学习课程-作业8-异常检测和推荐系统
  10. 【汇编语言】指令寻址
  11. 编译原理第三版课后习题
  12. html提示更新浏览器的代码,IE9及以下浏览器升级提示
  13. 网站端服务器返回错误8001,云服务器 http server
  14. 《疯狂java讲义》学习(19):枚举类
  15. 会员等级进度功能前端实现
  16. 松鼠Ai辅助公校教育,开启智慧教育3.0
  17. idea 远程debug调试
  18. 使用vagrant搭建三台虚拟机环境
  19. python处理PPOCRLabel标注的数据用于LPRNet与Yolo的训练
  20. 我是如何在 16 岁时成为全栈开发者的?

热门文章

  1. 车辆调度(科大讯飞杯)
  2. OSC802 USB虚拟示波器开箱与测评
  3. ImageJ工具使用简介
  4. MDK(Keil) 自动生成bin文件、汇编文件或者HEX文件、ASM文件
  5. IT负载率与数据中心规模——孙长青
  6. 移动网优大神VoLTE学习笔记(五):被叫信令流程
  7. 串口485接法图_485串口接线
  8. opnet共享代码开发
  9. c语言 文学研究助手 源程序,数据结构文学研究助手
  10. 个人博客前后台整站开发——模板免费下载