| 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(); |
| 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(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pipeline.addLast(new IdleStateHandler(7000,7000,10, TimeUnit.SECONDS)); |
| |
| pipeline.addLast(new MyServerHandler()); |
| } |
| }); |
| |
| |
| ChannelFuture channelFuture = serverBootstrap.bind(7000).sync(); |
| channelFuture.channel().closeFuture().sync(); |
| |
| }finally { |
| bossGroup.shutdownGracefully(); |
| workerGroup.shutdownGracefully(); |
| } |
| } |
| } |
| public class MyServerHandler extends ChannelInboundHandlerAdapter { |
| |
| |
| |
| |
| |
| |
| |
| @Override |
| public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { |
| |
| if(evt instanceof 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("服务器做相应处理.."); |
| |
| |
| |
| } |
| } |
| } |
- 启动服务端,再启动群聊系统中的客户端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--超时时间--读写空闲 |
| 服务器做相应处理.. |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2021-08-09 错误处理
2021-08-09 spring boot文件上传