当前位置:网站首页>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 方法进行异常捕获。
边栏推荐
- 2022年7月18日-7月31日(Ue4视频教程和文档,20小时。合计1412小时,剩8588小时)
- 打卡day05
- node安装及环境变量配置
- File upload vulnerability (2)
- optional
- [Dataset][VOC] Male and female dataset voc format 6188 sheets
- MySQL driver jar package download -- nanny tutorial
- .NET静态代码织入——肉夹馍(Rougamo) 发布1.1.0
- MySQL 23 classic interviews hang the interviewer
- Specified URL is not reachable,caused by :‘Read timed out
猜你喜欢
随机推荐
解决C#非静态字段、方法或属性“islandnum.Program.getIslandCount(int[][], int, int)”要求对象引用
Reverse resolve dns server
MySQL Advanced Study Notes
abaqus如何快速导入其他cae文件的assembly?
HCIP day 3 experiment
How the Internet of Things is changing the efficiency of city operations
C# FileInfo class
文件上传漏洞(二)
Pagoda+FastAdmin 404 Not Found
Leetcode Weekly 304
【npm install 报错问题合集】- npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
MySQL high-level statements (1)
Vscode连接远程服务器出现‘Acquiring lock on/home/~’问题
MySQL driver jar package download -- nanny tutorial
HCIP 第二天
HCIP day one
Toolbox App 1.25 新功能一览 | 版本更新
Node installation and environment variable configuration
速看!PMP新考纲、PMBOK第七版解读
两篇不错的php debug教程