在使用Netty进行网络编程的时候,通常需要在网络连接的不同阶段进行相应的操作,比如在连接建立时,客户端向服务端发起认证,在接收到数据时对数据内容进行解析等等。那么,连接的不同阶段在netty中如何表示呢? 这便是本文讨论的内容,Netty中ChannelHandller的生命周期。

首先我们先分析小网络连接的生命周期,连接建立 ---> 数据交互 ---> 连接断开,在数据交互阶段,包括从连接中读取数据和向连接中写入数据。知道了连接的生命周期,就可以按图索骥的在各个阶段进行想要的操作。而在Netty中,网络连接的不同生命周期都可以通过回调的方式来绑定相应的逻辑,这个回调接口就是ChannelHandler,这里主要我们以ChannelInboundHandler为例进行分析。在ChannelInboundHandler中定义了如下和生命周期相关的接口:

  • channelRegistered
  • channelUnregistered
  • channelActive
  • channelInactive
  • channelRead
  • channelReadComplete

加上在父类``中定义的两个:

  • handlerAdded
  • handlerRemoved

这些回调接口的调用书序是什么呢?我们通过写一个LifeCycleHandler来看下ChannelInboundHandler的生命周期的顺序。

public class LifeCycleInBoundHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRegistered(ChannelHandlerContext ctx)
throws Exception {
out.println("channelRegistered: channel注册到NioEventLoop");
super.channelRegistered(ctx);
} @Override
public void channelUnregistered(ChannelHandlerContext ctx)
throws Exception {
out.println("channelUnregistered: channel取消和NioEventLoop的绑定");
super.channelUnregistered(ctx);
} @Override
public void channelActive(ChannelHandlerContext ctx)
throws Exception {
out.println("channelActive: channel准备就绪");
super.channelActive(ctx);
} @Override
public void channelInactive(ChannelHandlerContext ctx)
throws Exception {
out.println("channelInactive: channel被关闭");
super.channelInactive(ctx);
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
out.println("channelRead: channel中有可读的数据" );
super.channelRead(ctx, msg);
} @Override
public void channelReadComplete(ChannelHandlerContext ctx)
throws Exception {
out.println("channelReadComplete: channel读数据完成");
super.channelReadComplete(ctx);
} @Override
public void handlerAdded(ChannelHandlerContext ctx)
throws Exception {
out.println("handlerAdded: handler被添加到channel的pipeline");
super.handlerAdded(ctx);
} @Override
public void handlerRemoved(ChannelHandlerContext ctx)
throws Exception {
out.println("handlerRemoved: handler从channel的pipeline中移除");
super.handlerRemoved(ctx);
}
}

启动服务器和Client,连接成功后,断开client的连接,Server端输出结果如下:

handlerAdded: handler被添加到channel的pipeline
channelRegistered: channel注册到NioEventLoop
channelActive: channel准备就绪
channelRead: channel中有可读的数据
channelReadComplete: channel读数据完成
channelReadComplete: channel读数据完成
channelInactive: channel被关闭
channelUnregistered: channel取消和NioEventLoop的绑定
handlerRemoved: handler从channel的pipeline中移除

从上面结果可以知道,从连接建立到连接断开,handler的生命周期回调接口调用顺序如下:

handlerAdded -> channelRegistered
-> channelActive -> channelRead -> channelReadComplete
-> channelInactive -> channelUnRegistered -> handlerRemoved

下面具体说下每个回调的具体含义:

  1. handlerAdded: 新建立的连接会按照初始化策略,把handler添加到该channel的pipeline里面,也就是channel.pipeline.addLast(new LifeCycleInBoundHandler)执行完成后的回调;
  2. channelRegistered: 当该连接分配到具体的worker线程后,该回调会被调用。
  3. channelActive:channel的准备工作已经完成,所有的pipeline添加完成,并分配到具体的线上上,说明该channel准备就绪,可以使用了。
  4. channelRead:客户端向服务端发来数据,每次都会回调此方法,表示有数据可读;
  5. channelReadComplete:服务端每次读完一次完整的数据之后,回调该方法,表示数据读取完毕;
  6. channelInactive:当连接断开时,该回调会被调用,说明这时候底层的TCP连接已经被断开了。
  7. channelUnREgistered: 对应channelRegistered,当连接关闭后,释放绑定的workder线程;
  8. handlerRemoved: 对应handlerAdded,将handler从该channel的pipeline移除后的回调方法。

Netty中ChannelHandler的生命周期的更多相关文章

  1. spring IOC 容器中 Bean 的生命周期

    IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...

  2. (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  3. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  4. Android应用程序中Activity的生命周期

    Android应用程序中Activity的生命周期 对于Android来说Activity的生命周期是非常的重要,尤其是对于新学者来说,只有充分了解了Activity的生命周期,才能写出优良用户体验的 ...

  5. Spring IOC容器中Bean的生命周期

    1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...

  6. 深入理解Spring中bean的生命周期

    [Spring中bean的生命周期] bean的生命周期 1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期: (1).生命周期图: (2).具体事例 ...

  7. Spring中Bean的生命周期及其扩展点

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...

  8. 简:Spring中Bean的生命周期及代码示例

    (重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...

  9. spring BeanFactory及ApplicationContext中Bean的生命周期

    spring bean 的生命周期 spring BeanFactory及ApplicationContext在读取配置文件后.实例化bean前后.设置bean的属性前后这些点都可以通过实现接口添加我 ...

随机推荐

  1. windowserver 2012安装openssh

    下载https://github.com/PowerShell/Win32-OpenSSH/releases解压放到C:\Program Files\OpenSSH-Win64 进入到C:\Progr ...

  2. 直接拿来用,10个PHP代码片段(收藏)

    直接拿来用,10个PHP代码片段(一) http://www.csdn.net/article/2013-07-23/2816316-10-php-snippets-for-developers 直接 ...

  3. 探究 Go 语言 defer 语句的三种机制

    Golang 的 1.13 版本 与 1.14 版本对 defer 进行了两次优化,使得 defer 的性能开销在大部分场景下都得到大幅降低,其中到底经历了什么原理? 这是因为这两个版本对 defer ...

  4. SpringCloud入门(六): Hystrix监控

    Hystrix.stream 监控 <!--. 配置pom文件,引入actuator包--> <dependency> <groupId>org.springfra ...

  5. Ubuntu 16.04.5部署Django环境

    1.安装python环境 使用如下命令安装的是3.5.x版本 sudo apt-get install python3-pip 如果使用下面的命令,会安装2.x版本的python sudo apt-g ...

  6. Nginx之反向代理配置(二)

    前文我们聊了Nginx的防盗链.反向代理以及开启nginx代理缓存,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12417130.html:今天我们继续说ng ...

  7. Failed to open the key database file. c;\\User\\w\\Destop\\SecureCRT_FX6.5.3\\Config\\KnowHosts\\Hostsmap.txt这个问题的解决方法

    1.首先将这段错误在百度翻译上面查询一下,是什么意思,查询结果如下: 打开密钥数据库文件失败.C:\用户\ w \平台\ securecrt_fx6.5.3 \\ \\ \\ hostsmap.txt ...

  8. Flex实现九宫格

    写一个靠谱的flex布局 <!DOCTYPE html> <html> <style> .block { padding-top: 30%; margin-top: ...

  9. 用CSS3实现钟表效果

    背景:最近在学习CSS3,看到了一个小案例,通过自己的学习,动手实现了它,现在把它分享出来. 效果图 实现过程 1.首先我们需要在页面中写出一个静态的钟表效果.首先我们需要一个表盘div wrap 对 ...

  10. ubuntu下安装typescript

    安装ts之前需要安装好node, 安装ts: 1. npm install -g typescript /opt/node/bin/tsc -> /opt/node/lib/node_modul ...