展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

Netty心跳检测机制

1) 编写一个 Netty心跳检测机制案例, 当服务器超过3秒没有读时,就提示读空闲
2) 当服务器超过5秒没有写操作时,就提示写空闲
3) 实现当服务器超过7秒没有读或者写操作时,就提示读写空闲
  • 编写服务端
public class MyServer {
public static void main(String[] args) throws Exception{
//创建两个线程组
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(); //8个NioEventLoop
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.handler(new LoggingHandler(LogLevel.INFO));
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//加入一个netty 提供 IdleStateHandler
/*
说明
1. IdleStateHandler 是netty 提供的处理空闲状态的处理器
2. long readerIdleTime : 表示多长时间没有读, 就会发送一个心跳检测包检测是否连接
3. long writerIdleTime : 表示多长时间没有写, 就会发送一个心跳检测包检测是否连接
4. long allIdleTime : 表示多长时间没有读写, 就会发送一个心跳检测包检测是否连接
5. 文档说明
triggers an {@link IdleStateEvent} when a {@link Channel} has not performed
* read, write, or both operation for a while.
* 6. 当 IdleStateEvent 触发后 , 就会传递给管道 的下一个handler去处理
* 通过调用(触发)下一个handler 的 userEventTiggered , 在该方法中去处理 IdleStateEvent(读空闲,写空闲,读写空闲)
*/
pipeline.addLast(new IdleStateHandler(7000,7000,10, TimeUnit.SECONDS));
//加入一个对空闲检测进一步处理的handler(自定义)
pipeline.addLast(new MyServerHandler());
}
});
//启动服务器
ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();
channelFuture.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
  • 编写服务端处理器
public class MyServerHandler extends ChannelInboundHandlerAdapter {
/**
*
* @param ctx 上下文
* @param evt 事件
* @throws Exception
*/
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if(evt instanceof IdleStateEvent) {
//将 evt 向下转型 IdleStateEvent
IdleStateEvent event = (IdleStateEvent) evt;
String eventType = null;
switch (event.state()) {
case READER_IDLE:
eventType = "读空闲";
break;
case WRITER_IDLE:
eventType = "写空闲";
break;
case ALL_IDLE:
eventType = "读写空闲";
break;
}
System.out.println(ctx.channel().remoteAddress() + "--超时时间--" + eventType);
System.out.println("服务器做相应处理..");
//如果发生空闲,我们关闭通道
// ctx.channel().close();
}
}
}
  • 启动服务端,再启动群聊系统中的客户端GroupChatClient
# 服务端控制台
8 09, 2022 10:58:48 上午 io.netty.handler.logging.LoggingHandler channelRegistered
信息: [id: 0x055ca1cd] REGISTERED
8 09, 2022 10:58:48 上午 io.netty.handler.logging.LoggingHandler bind
信息: [id: 0x055ca1cd] BIND: 0.0.0.0/0.0.0.0:7000
8 09, 2022 10:58:48 上午 io.netty.handler.logging.LoggingHandler channelActive
信息: [id: 0x055ca1cd, L:/0:0:0:0:0:0:0:0:7000] ACTIVE
8 09, 2022 10:59:02 上午 io.netty.handler.logging.LoggingHandler channelRead
信息: [id: 0x055ca1cd, L:/0:0:0:0:0:0:0:0:7000] READ: [id: 0x7ce7293f, L:/127.0.0.1:7000 - R:/127.0.0.1:60519]
8 09, 2022 10:59:02 上午 io.netty.handler.logging.LoggingHandler channelReadComplete
信息: [id: 0x055ca1cd, L:/0:0:0:0:0:0:0:0:7000] READ COMPLETE
/127.0.0.1:60519--超时时间--读写空闲
服务器做相应处理..
/127.0.0.1:60519--超时时间--读写空闲
服务器做相应处理..
/127.0.0.1:60519--超时时间--读写空闲
服务器做相应处理..
/127.0.0.1:60519--超时时间--读写空闲
服务器做相应处理..
/127.0.0.1:60519--超时时间--读写空闲
服务器做相应处理..
posted @   DogLeftover  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2021-08-09 错误处理
2021-08-09 spring boot文件上传
点击右上角即可分享
微信分享提示