ChannelInitializer在Netty中是一个很重要的东西。也是4.x版本中用户接触比较多的一个类

它本身是继承ChannelInboundHandlerAdapter的。实现ChannelInboundHandler类


【推荐1】Netty4 ChannelPipeLine分析  ★★★★☆

【推荐2】java netty之ChannelPipeline  ★★★☆☆

【推荐3】netty源码分析之FrameDecoder(LengthFieldBasedFrameDecoder)  ★★★☆☆

ChannelPipeline 中的中的事件流程

  • I/O Request
    via Channel or
    ChannelHandlerContext
    |
    +---------------------------------------------------+---------------+
    | ChannelPipeline | |
    | \|/ |
    | +---------------------+ +-----------+----------+ |
    | | Inbound Handler N | | Outbound Handler 1 | |
    | +----------+----------+ +-----------+----------+ |
    | /|\ | |
    | | \|/ |
    | +----------+----------+ +-----------+----------+ |
    | | Inbound Handler N-1 | | Outbound Handler 2 | |
    | +----------+----------+ +-----------+----------+ |
    | /|\ . |
    | . . |
    | ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
    | [ method call] [method call] |
    | . . |
    | . \|/ |
    | +----------+----------+ +-----------+----------+ |
    | | Inbound Handler 2 | | Outbound Handler M-1 | |
    | +----------+----------+ +-----------+----------+ |
    | /|\ | |
    | | \|/ |
    | +----------+----------+ +-----------+----------+ |
    | | Inbound Handler 1 | | Outbound Handler M | |
    | +----------+----------+ +-----------+----------+ |
    | /|\ | |
    +---------------+-----------------------------------+---------------+
    | \|/
    +---------------+-----------------------------------+---------------+
    | | | |
    | [ Socket.read() ] [ Socket.write() ] |
    | |
    | Netty Internal I/O Threads (Transport Implementation) |
    +-------------------------------------------------------------------+

    这个图是joc里面的ChannelPipeline文档里面的一张示意图。图示的内容是。
    当有IO请求
    ChannelInboundHandler 是从上之下搜索,相应的实现。一次执行。
    4.x版本中的Netty。启用了一个新的东西用于初始ChannelPipeline -> ChannelInitializer<Channel>
    ChannelInitializer有一个抽象未实现的方法:initChannel(C ch) 用于开发者自己定义相关的Handler添加到信道中。原理及详细分析请参考上面推荐的两篇文章。可能版本还不是最新的。但是4.x版本之后的一些大致的思路都是没有什么变化。
    
    下面把自己写的小东西贴出来。
    
     package com.engine.netty.server.initializer;
    
     import io.netty.channel.ChannelInitializer;
    import io.netty.channel.ChannelPipeline;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
    import io.netty.handler.codec.LengthFieldPrepender;
    import io.netty.handler.timeout.IdleStateHandler; import com.engine.netty.server.handler.IdleStateCheckHandler;
    import com.engine.netty.server.handler.codec.DecoderAMF3;
    import com.engine.netty.server.handler.codec.EncoderAMF3;
    import com.engine.netty.socket.ChatServerHandler; /**
    * Creates a newly configured {@link ChannelPipeline} for a new channel.
    */
    public class NettyServerInitializer extends ChannelInitializer<SocketChannel> { @Override
    public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline(); // 编码或者解码 拆包
    /*
    * 需要和客户端订立 相关通信协议底层规则 - 【消息长度(2 : short表示 2个字节)】【消息内容】
    * lengthFieldLength => 是整数 - 占四个字节 所以同时偏移四个字节 => (0, 4, 0, 4)
    *
    * */
    pipeline.addLast("framer", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
    pipeline.addLast("prepender", new LengthFieldPrepender(4, false)); // 空闲状态检查 间隔无消息,断开连接
    pipeline.addLast("idleStateCheck", new IdleStateHandler(0, 0, 600));
    pipeline.addLast("idleCheckHandler", new IdleStateCheckHandler()); // AMF3 编码或者解码
    pipeline.addLast("decoder", new DecoderAMF3());
    pipeline.addLast("encoder", new EncoderAMF3()); // 序列化对象object编码和解码 【用于Java Object】
    //pipeline.addLast("objectEncoder", new ObjectEncoder());
    //pipeline.addLast("objectDecoder", new ObjectDecoder(ClassResolvers.cacheDisabled(null))); /*// 以UTF-8格式编码和解码 数据 【用于字符串】
    pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
    pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));*/ // and then business logic.
    pipeline.addLast("handler", new ChatServerHandler());
    }
    }

    NettyServerInitializer

    发出去AMF3的东西是上一篇记录里面写的之外。其他两种编码和解码都是官网有相对应例子的。编码和解码说白其实就是对字节数据的翻译。在Netty中数据已ByteBuf保存。ByteBuf提供了很多的方法读取相对应的数据。在这里就不详细介绍了。有兴趣的可以去看看官网的示例。或者是API 文档。文档中有一些类。官方都提供了相对应的代码范例。
    【空闲状态检查】是官网API文档中找到的。
    io.netty.handler.timeout 这个包中就是Netty提供的一些关于读超时,写超时,读写超时。空闲时间等的东西。 LengthFieldBasedFrameDecoder 和 LengthFieldPrepender
    这两个编码解码 相当于是对数据的处理。
    先讲一下
    LengthFieldBasedFrameDecoder :
    直接通过命名大家很明显的可以理解这个类 一个根据帧长度的解码类。到底是怎么样子的呢?其实很简单。 LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip)
    参数:
    maxFrameLength: the maximum length of the frame. If the length of the frame is greater than this value, TooLongFrameException will be thrown. (最大帧长)
    lengthFieldOffset: the offset of the length field (偏移量)
    lengthFieldLength: the length of the length field
    lengthAdjustment: the compensation value to add to the value of the length field
    initialBytesToStrip: the number of first bytes to strip out from the decoded frame
    它有5个参数 (详细的解释可以参考推荐文章3).
    说简单点就是第一个是字节消息的最大长度。第二个参数是消息长度这个字节的偏移量。第三个参数是描述字节消息长度的那部分的长度。第四个不解释。第五个是跳过首部的字节长度
    我这边写的0202其实意思就是 我的消息长度是有一个short类型来描述的。short类型的数据占2个字节。第二个2是说在返回最终解码的数据的时候跳过这个描述长度的字节数。
    API文档中的:
    2 bytes length field at offset 0, strip header :
    Because we can get the length
    of the content by calling ByteBuf.readableBytes(), you might want to strip the
    length field by specifying initialBytesToStrip. In this example, we specified 2,
    that is same with the length of the length field, to strip the first two bytes.

    lengthFieldOffset = 0
    lengthFieldLength = 2
    lengthAdjustment = 0

    initialBytesToStrip = 2 (= the length of the Length field) BEFORE
    DECODE (14 bytes) AFTER DECODE (12 bytes)
    +--------+----------------+ +----------------+
    | Length | Actual Content |----->| Actual Content |
    | 0x000C | "HELLO, WORLD" | | "HELLO, WORLD" |
    +--------+----------------+ +----------------+  最后的ChatServerHandler是我自己写的逻辑的Handler
    
    
    貌似就写到这边了。O(∩_∩)O哈哈~
    
    

【Netty学习】 ChannelInitializer 学习的更多相关文章

  1. 【Netty源码学习】ServerBootStrap

    上一篇博客[Netty源码学习]BootStrap中我们介绍了客户端使用的启动服务,接下来我们介绍一下服务端使用的启动服务. 总体来说ServerBootStrap有两个主要功能: (1)调用父类Ab ...

  2. Netty 源码学习——客户端流程分析

    Netty 源码学习--客户端流程分析 友情提醒: 需要观看者具备一些 NIO 的知识,否则看起来有的地方可能会不明白. 使用版本依赖 <dependency> <groupId&g ...

  3. netty权威指南学习笔记八——编解码技术之JBoss Marshalling

    JBoss Marshalling 是一个java序列化包,对JDK默认的序列化框架进行了优化,但又保持跟java.io.Serializable接口的兼容,同时增加了一些可调参数和附加特性,这些参数 ...

  4. netty权威指南学习笔记五——分隔符和定长解码器的应用

    TCP以流的方式进行数据传输,上层应用协议为了对消息进行区分,通常采用以下4中方式: 消息长度固定,累计读取到长度综合为定长LEN的报文后,就认为读取到了一个完整的消息,将计数器置位,重新开始读取下一 ...

  5. netty权威指南学习笔记三——TCP粘包/拆包之粘包现象

    TCP是个流协议,流没有一定界限.TCP底层不了解业务,他会根据TCP缓冲区的实际情况进行包划分,在业务上,一个业务完整的包,可能会被TCP底层拆分为多个包进行发送,也可能多个小包组合成一个大的数据包 ...

  6. Netty源码学习系列之4-ServerBootstrap的bind方法

    前言 今天研究ServerBootstrap的bind方法,该方法可以说是netty的重中之重.核心中的核心.前两节的NioEventLoopGroup和ServerBootstrap的初始化就是为b ...

  7. 【Netty源码学习】DefaultChannelPipeline(三)

    上一篇博客中[Netty源码学习]ChannelPipeline(二)我们介绍了接口ChannelPipeline的提供的方法,接下来我们分析一下其实现类DefaultChannelPipeline具 ...

  8. 【Netty源码学习】ChannelPipeline(一)

    ChannelPipeline类似于一个管道,管道中存放的是一系列对读取数据进行业务操作的ChannelHandler. 1.ChannelPipeline的结构图: 在之前的博客[Netty源码学习 ...

  9. Netty 源码学习——EventLoop

    Netty 源码学习--EventLoop 在前面 Netty 源码学习--客户端流程分析中我们已经知道了一个 EventLoop 大概的流程,这一章我们来详细的看一看. NioEventLoopGr ...

随机推荐

  1. IIS 301 跳转

    IIS设置301重定向 IIS服务器下做301永久重定向设置方法. IIS6设置301重定向: 1.新建一个站点,对应目录如E:\wwwroot\301web.该目录下只需要1个文件,即index.h ...

  2. 注意自己的dns设置 - 阿权的书房

    一般而言,随便找个合适的dns服务器作为自己的dns解析服务器即可,但如果选择不当,可能就会导致网络选择并不是最优的.这个情况一般发生在电信网通优化的域名上. 检查方法(域名有所替换): [root@ ...

  3. 使用Eclipse自带的Axis1插件生成WSDL文件

    首先创建一个web工程,创建过程如下: 如果选择Apache Tomcat v5.5,Dynamic web module version最高只能选择2.4,填写完成后点击“下一步”: 填写默认输出文 ...

  4. thrift

    环境准备: 1.下载window版本的thrift编译器 2.下载idea的thirft插件 3.设置thrift编译工具为:步骤1下载的编译器 4.编写thrift文件 namespace java ...

  5. twitter storm源码走读之4 -- worker进程中线程的分类及用途

    欢迎转载,转载请注明出版,徽沪一郎. 本文重点分析storm的worker进程在正常启动之后有哪些类型的线程,针对每种类型的线程,剖析其用途及消息的接收与发送流程. 概述 worker进程启动过程中最 ...

  6. Nginx 笔记与总结(4)配置 server 访问日志

    打开 nginx.conf: [root@localhost ~]# cd /usr/local/nginx/conf [root@localhost conf]# vim nginx.conf 在默 ...

  7. VIM常用快捷键~网页上查找

    转自~木枫林 转自~鸟哥的私房菜 第十章.vim 程序编辑器 第十章.vim 程序编辑器 最近更新日期:2009/08/20 2. vi 的使用 2.1 简易执行范例 2.2 按键说明 2.3 一个案 ...

  8. C与C++连续赋值的区别

    int a,b,c,d; a = b = ; // ( a!=b?a:b) = 1000;//如果a不等于b 那么a = 100;这句话执行完 a还是等于5,b= 100: printf(" ...

  9. wordpress 常用函数 checked(),selected(),disabled()

    checked().selected().disabled(),这三个函数是主题设计和插件设计中添加后台设置比较常用到的函数. 例如自定义一个widget,这个widget有一个字段为文章排列方式.文 ...

  10. Mysql 定时备份操作

    1.创建保存备份文件的路径/mysqldata #mkdir /bak/mysqlbak 2.创建/usr/sbin/bakmysql文件 #vi /usr/sbin/bakmysql.sh 3.写入 ...