Netty ChannelHandler的生命周期

导读:本篇文章讲解 Netty ChannelHandler的生命周期,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

前言

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

首先我们先分析下网络连接的生命周期:连接建立 —> 数据交互 —> 连接断开

在数据交互阶段,包括从连接中读取数据和向连接中写入数据。知道了连接的生命周期,就可以按图索骥的在各个阶段进行想要的操作。

而在 Netty 中,网络连接的不同生命周期都可以通过回调的方式来绑定相应的逻辑,这个回调接口就是ChannelHandler,这里以 ChannelInboundHandler 为例分析。

主要接口

ChanneHandler 的类依赖关系图
yl9AcF.png
通过上面的类结构图,我们总结一下规律:

  1. ChannelHandler 有两个子接口,分别是 ChannelInboundHandlerChannelOutboundHandler,其实从字面意思就能知道,它们分别是入站出站的接口类。
  2. 如果我们自定义的业务 Handler 直接实现 ChannelInboundHandler 或者 ChannelOutboundHandler,那么我们需要实现的接口非常的多,增加了开发的难度。Netty 已经帮我们封装好了两个实现类,分别是 ChannelInboundHandlerAdapterChannelOutboundHandlerAdapter,这样可以大大简化了开发工作。

SimpleChannelInboundHandler 也可以,额外添加了几种方法

核心生命周期方法

ChannelInboundHandler中定义了如下和生命周期相关的接口:

方法 描述
channelRegistered channelRegistered 注册成功时触发
channelUnregistered channel 取消注册时触发
channelActive channel 连接就绪时触发
channelInactive channel 断开时触发
channelRead channel 有数据可读时触发
channelReadComplete channel 有数据可读,并且读完时触发

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

方法 描述
handlerAdded Handler 被加入 Pipeline 时触发(仅仅触发一次)
handlerRemoved handler 被从 Pipeline 移除时触发

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

public class LifeCycleInBoundHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRegistered(ChannelHandlerContext ctx)
            throws Exception {
        System.out.println("channelRegistered: channel注册到NioEventLoop");
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx)
            throws Exception {
        System.out.println("channelUnregistered: channel取消和NioEventLoop的绑定");
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx)
            throws Exception {
        System.out.println("channelActive: channel准备就绪");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx)
            throws Exception {
        System.out.println("channelInactive: channel被关闭");
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        System.out.println("channelRead: channel中有可读的数据" );
        super.channelRead(ctx, msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx)
            throws Exception {
        System.out.println("channelReadComplete: channel读数据完成");
        super.channelReadComplete(ctx);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx)
            throws Exception {
        System.out.println("handlerAdded: handler被添加到channel的pipeline");
        super.handlerAdded(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx)
            throws Exception {
        System.out.println("handlerRemoved: handler从channel的pipeline中移除");
        super.handlerRemoved(ctx);
    }
}

客户端发送一次请求

handlerAdded: handler被添加到channel的pipeline
channelRegistered: channel注册到NioEventLoop
channelActive: channel准备就绪
channelRead: channel中有可读的数据
channelReadComplete: channel读数据完成
channelReadComplete: channel读数据完成

客户端发送多次请求

channelReadComplete: channel读数据完成
channelReadComplete: channel读数据完成

channelReadComplete: channel读数据完成
channelReadComplete: channel读数据完成

channelReadComplete: channel读数据完成
channelReadComplete: channel读数据完成

通过执行结果我们发现,第一次的时候执行 handlerAdded()channelRegistered()channelActive(),后面就不会被执行了。

客户端的每次请求时,都会触发 channelRead()channelReadComplete() 两个核心方法。

关闭客户端

二月 03, 2021 9:16:59 下午 io.netty.channel.DefaultChannelPipeline onUnhandledInboundException
警告: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.io.IOException: 远程主机强迫关闭了一个现有的连接。
	at sun.nio.ch.SocketDispatcher.read0(Native Method)
	at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
	at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
	at sun.nio.ch.IOUtil.read(IOUtil.java:192)
	at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
	at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253)
	at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1134)
	at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350)
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:748)

channelInactive: channel被关闭
channelUnregistered: channel取消和NioEventLoop的绑定
handlerRemoved: handler从channel的pipeline中移除

总结,人为的关闭通道或者其他因素(比如:网络故障等),则会触发 channelInactivechannelUnregisteredhandlerRemoved 的执行。

生命周期总结

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

yQzO4f.png

  1. handlerAdded :当检测到新连接之后,调用 ch.pipeline().addLast(new LifeCycleHandler()); 之后的回调,表示在当前的 channel 中,已经成功添加了一个 handler 到双向链表pipeline) 中。
  2. channelRegistered:这个回调方法,表示当前的 channel 的所有的逻辑处理已经和某个 NIO 线程建立了绑定关系,从线程池里面去抓一个线程绑定在这个 channel 上,这里的 NIO 线程通常指的是 NioEventLoop
  3. channelActive:当 channel 的所有的业务逻辑链准备完毕,channel 的 pipeline 中已经添加完所有的 handler 以及绑定好一个 NIO 线程之后,这条连接算是真正激活了,接下来就会回调到此方法。
  4. channelRead:客户端向服务端发来数据,每次都会回调此方法,表示有数据可读。
  5. channelReadComplete:服务端每次读完一次完整的数据之后,回调该方法,表示数据读取完毕。
  6. channelInactive: 表面这条连接已经被关闭了,这条连接在 TCP 层面已经不再是 ESTABLISH 状态了。
  7. channelUnregistered: 既然连接已经被关闭,那么与这条连接绑定的线程就不需要对这条连接负责了,这个回调就表明与这条连接对应的 NIO 线程移除掉对这条连接的处理。
  8. handlerRemoved:给这条连接上添加的所有的业务逻辑处理器都给移除掉。

ChannelHandler 回调方法的执行顺序为:

  • 连接请求: handlerAdded () -> channelRegistered () -> channelActive () -> channelRead () -> channelReadComplete ();
  • 数据请求: channelRead () -> channelReadComplete ();
  • 通道被关闭:channelInactive () -> channelUnregistered () -> handlerRemoved ()。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/69735.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!