• SimpleChannelInboundHandler使用总结


    当客户端到达服务端时,建立连接的有 channelActive 和 handlerAdded ,关闭连接的有 channelInactive 和 handlerRemoved ,应该如何决定使用?

    代码:

    1. import com.xh.netty.common.config.NettyConfig;
    2. import com.xh.netty.common.constant.NettyCons;
    3. import io.netty.channel.Channel;
    4. import io.netty.channel.ChannelHandlerContext;
    5. import io.netty.channel.SimpleChannelInboundHandler;
    6. import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
    7. import io.netty.util.AttributeKey;
    8. import lombok.extern.slf4j.Slf4j;
    9. import org.springframework.stereotype.Component;
    10. @Slf4j
    11. @Component
    12. public class WebSocketHandler extends SimpleChannelInboundHandler {
    13.     @Override
    14.     public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {
    15.         System.out.println("channelActive");
    16.     }
    17.     @Override
    18.     public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {
    19.         log.info("handlerAdded");
    20.     }
    21.     @Override
    22.     public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {
    23.         System.out.println("channelInactive");
    24.     }
    25.     @Override
    26.     public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {
    27.         log.info("handlerRemoved");
    28.     }
    29. }

    运行结果

    1. handlerAdded
    2. channelActive
    3. channelInactive
    4. handlerRemoved

    ChannelHandler 的生命周期

    ChannelHandler 接口定义 handlerAdded 和 handlerRemoved 两个方法,也就是 ChannelHandler 的生命周期从 add 开始,remove 结束。让我们看看源码中是怎样的一个过程对ChannelHandler的处理。


    1.Channel 创建连接

    当新的客户端连接到服务端之后,会依次调用 ChannelHandler 中的方法,完成 ChannelPipeline 对 ChannelHandler 的添加。channel 会注册到 EventLoop 中并激活当前 channel。

    执行顺序:handlerAdded -> channelRegistered -> channelActive

    2.EventLoop 监听

    当客户端发送请求信息到服务端时,会调用 channelRead 方法完成请求信息读取;读取完成后则调用 channelReadComplete 方法,表示此次读事件完成。

    执行顺序:channelRead -> channelReadComplete

    3.Channel 关闭连接

    客户端完成请求后,会关闭连接(或者长时间没有请求被服务端主动close掉),关闭连接时,服务端检测到该 channel 的关闭,则会依次调用 ChannelHandler 中的方法完成注销删除。

    执行顺序:channelInactive -> channelUnregistered -> handlerRemoved

    4.Channel 异常

    当channel在读取数据时发生异常,则抛出,此时会调用 ChannelPipeline.fireExceptionCaught() 方法,后续依次调用 ChannelHandler.exceptionCaught() 方法来完成异常处理。

    总结:

    ChannelHandler 生命周期过程:

    handlerAdded –> channelRegistered –> channelActive –> channelRead –> channelReadComplete –> channelInactive –> channelUnregistered –> handlerRemoved。

    在执行期间都会伴随着 exceptionCaught 方法进行异常捕获。

  • 相关阅读:
    SQL开发笔记之专栏介绍
    Spring Data JPA系列5:让IDEA自动帮你写JPA实体定义代码
    光伏组件机器视觉新突破!维视智造上线汇流带引线焊接检测新方案 “误检率”低至0.01%
    4.2 数据库安全性控制
    vs中C++编译未生成exe
    云原生 | Docker - [Portainer]
    SLAM ORB-SLAM2(1)总体框架
    日期类练习题
    【JAVA】泛型
    mysq基础语句+高级操作(学这篇就够了)
  • 原文地址:https://blog.csdn.net/xhaimail/article/details/126059636