当前位置:网站首页>SimpleChannelInboundHandler使用总结
SimpleChannelInboundHandler使用总结
2022-08-02 06:18:00 【旷野历程】
当客户端到达服务端时,建立连接的有 channelActive 和 handlerAdded ,关闭连接的有 channelInactive 和 handlerRemoved ,应该如何决定使用?
代码:
import com.xh.netty.common.config.NettyConfig;
import com.xh.netty.common.constant.NettyCons;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
@Override
public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {
System.out.println("channelActive");
}
@Override
public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {
log.info("handlerAdded");
}
@Override
public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {
System.out.println("channelInactive");
}
@Override
public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {
log.info("handlerRemoved");
}
}
运行结果
handlerAdded
channelActive
channelInactive
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 方法进行异常捕获。
边栏推荐
- 打卡day05
- MySQL Advanced SQL Statements
- Nodejs installation and global configuration (super detailed)
- HCIP 第四天
- July 18-July 31, 2022 (Ue4 video tutorials and documentation, 20 hours. Total 1412 hours, 8588 hours left)
- 武汉高性能计算大会2022举办,高性能计算生态发展再添新动力
- The nacos source code can not find the istio package
- HCIP day one
- MySQL 23 classic interviews hang the interviewer
- File upload vulnerability (2)
猜你喜欢
随机推荐
The second day HCIP
交换网络----三种生成树协议
[数据集][VOC]眼睛佩戴数据集VOC格式6000张
MySQL - Multi-table query and case detailed explanation
At age 94, pioneer Turing award winner, computational complexity theory, Juris Hartmanis, died
node安装及环境变量配置
Go inside the basic knowledge
Servlet
typescript ‘props‘ is declared but its value is never read 解决办法
MySQL Advanced Study Notes
Py's mlxtend: a detailed guide to the introduction, installation, and usage of the mlxtend library
Pagoda+FastAdmin 404 Not Found
实验7 MPLS实验
Leetcode周赛304
【暑期每日一题】洛谷 P1255 数楼梯
HCIP 第三天实验
Leetcode Weekly 304
Nodejs installation tutorial
Connection reset by peer problem analysis
第06章 索引的数据结构【2.索引及调优篇】【MySQL高级】