Netty原理:pipeline
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相关推荐
- Netty原理-Pipeline
Pipeline初始化 在Netty中一个Channel对应一个Pipeline,在AbstractChannel的构造函数中会进行Pipeline的构造, 默认会创建两个Context,一个是Hea ...
- 精尽 Netty 原理与源码专栏( 已经完成 61+ 篇,预计总共 70+ 篇 )
只更新在笔者的知识星球,欢迎加入一起讨论 Netty 源码与实现. 目前已经有 1000+ 位球友加入- 进度:已经完成 60+ 篇,预计总共 70+ 篇,完成度 90% . 对应 Netty 版本号 ...
- (高级)Dubbo 第五章 Dubbo及RocketMQ底层-Netty原理
Netty原理 Netty 是一个高性能.异步事件驱动的NIO 框架,基于JAVA NIO 提供的API 实现.它提供了对TCP.UDP 和文件传输的支持,作为一个异步NIO 框架,Netty 的所有 ...
- netty 进程挂起_这可能是目前最透彻的Netty原理架构解析
本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件.整体架构,知其然且知其所以然,希望给大家在实际开发实践.学习开源项目方面提供参考. Netty 是一个异步事件驱动的网络应用程序 ...
- Netty原理和使用
Netty是一个高性能 事件驱动的异步的非堵塞的IO(NIO)框架,用于建立TCP等底层的连接,基于Netty可以建立高性能的Http服务器.支持HTTP. WebSocket .Protobuf. ...
- Netty原理架构解析
本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件.整体架构,知其然且知其所以然,希望给大家在实际开发实践.学习开源项目方面提供参考. Netty 是一个异步事件驱动的网络应用程序 ...
- 这可能是目前最透彻的Netty原理架构解析
转载自:https://www.toutiao.com/i6620280257846968840/?tt_from=weixin&utm_campaign=client_share&w ...
- Netty系列二、Netty原理篇
文章目录 一.Netty概述 二.Netty整体架构设计 1.Reactor模型 2.Reactor模型分类 2.1 单Reactor单线程 2.2 单Reactor多线程 2.3 多Reactor多 ...
- 【Netty】Netty 核心组件 ( Pipeline | ChannelPipeline )
文章目录 一. Pipeline / ChannelPipeline 管道组件 二. Pipeline / ChannelPipeline 管道组件元素解析 一. Pipeline / Channel ...
- Netty原理五:ChannelFuture、DefaultChannelPromise对象解析
文章目录 1. 前言 2. 原理解析 2.1 ChannelFuture 调用 sync() 的作用 2.2 Channel 调用的 closeFuture() 是什么 1. 前言 学习Netty的时 ...
最新文章
- 为全局变量赋值_Postman全局变量设置和运用
- 一文弄懂用户画像以及如何召回用户
- java+spring+mysql配置_用spring的beans配置mysql数据库
- sklearn中模型的选择和各个模型的比较
- php继承和重载区别,php继承中方法重载(覆盖)的应用场合
- 第二节:ES6新增了let关键字,干嘛用的?
- Dockerfile文件详解
- Linux下Centos7以rpm方式离线安装MySQL5.7教程以及部分报错解决方案
- 8. 吴恩达机器学习课程-作业8-异常检测和推荐系统
- 【汇编语言】指令寻址
- 编译原理第三版课后习题
- html提示更新浏览器的代码,IE9及以下浏览器升级提示
- 网站端服务器返回错误8001,云服务器 http server
- 《疯狂java讲义》学习(19):枚举类
- 会员等级进度功能前端实现
- 松鼠Ai辅助公校教育,开启智慧教育3.0
- idea 远程debug调试
- 使用vagrant搭建三台虚拟机环境
- python处理PPOCRLabel标注的数据用于LPRNet与Yolo的训练
- 我是如何在 16 岁时成为全栈开发者的?