栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Netty-9-Netty心跳检测

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Netty-9-Netty心跳检测

Netty心跳检测机制案例

要求:

  1. 编写一个 Netty心跳检测机制案例, 当服务器超过3秒没有读时,就提示读空闲
  2. 当服务器超过5秒没有写操作时,就提示写空闲
  3. 实现当服务器超过7秒没有读或者写操作时,就提示读写空闲
Server:
package com.atguigu.netty.NettyHeartBeat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;



public class HeartServer {
    public static void main(String[] args) throws InterruptedException {
        //线程组
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    //在bossGroup 关联一个日志处理器
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            //加入Netty提供的IdleStateHandle
                            //TODO 处理空闲状态的处理器
                            //当IdleStateEvent 触发后就会传递给管道(pipeline)的下一个Handle去处理 通过回调下一个handle 的 UserEventTiggered 方法去处理
                            

                            pipeline.addLast(new IdleStateHandler(3, 5, 7, TimeUnit.SECONDS));

                            //TODO 加入一个对空闲检测进一步处理的Handle(自定义)
                            pipeline.addLast(new HeartServerhandle());


                        }
                    });

            //启动服务器
            ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();
            //监关闭事件
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

ServerHandle
package com.atguigu.netty.NettyHeartBeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;



public class HeartServerhandle extends ChannelInboundHandlerAdapter {

    
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        //判断是不是IdleStateEvent类型
        if (evt instanceof IdleStateEvent){
            //将evt 向下转型 IdleStateEvent
            IdleStateEvent event = (IdleStateEvent) evt;
            //判断是什么事件心跳检测不通过
            //READER_IDLE 读空闲, WRITER_IDLE 写空闲, ALL_IDLE 读写空闲
            String eventType = "";
            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 + "超时事件发生");
			
			//如果发生空闲了直接关闭通道 空闲事件只会发生一次 谁心跳短谁先发生
            ctx.channel().close();
        }

    }
}

复用之前客户端测试即可

结果:
六月 25, 2022 5:59:13 下午 io.netty.handler.logging.LoggingHandler channelRegistered
信息: [id: 0x82bc9da5] REGISTERED
六月 25, 2022 5:59:13 下午 io.netty.handler.logging.LoggingHandler bind
信息: [id: 0x82bc9da5] BIND: 0.0.0.0/0.0.0.0:7000
六月 25, 2022 5:59:13 下午 io.netty.handler.logging.LoggingHandler channelActive
信息: [id: 0x82bc9da5, L:/0:0:0:0:0:0:0:0:7000] ACTIVE
六月 25, 2022 6:00:42 下午 io.netty.handler.logging.LoggingHandler channelRead
信息: [id: 0x82bc9da5, L:/0:0:0:0:0:0:0:0:7000] READ: [id: 0xae0eee37, L:/127.0.0.1:7000 - R:/127.0.0.1:49403]
六月 25, 2022 6:00:42 下午 io.netty.handler.logging.LoggingHandler channelReadComplete
信息: [id: 0x82bc9da5, L:/0:0:0:0:0:0:0:0:7000] READ COMPLETE
通道: /127.0.0.1:49403读空闲超时事件发生
通道: /127.0.0.1:49403写空闲超时事件发生
通道: /127.0.0.1:49403读空闲超时事件发生
通道: /127.0.0.1:49403读写空闲超时事件发生
通道: /127.0.0.1:49403读空闲超时事件发生
通道: /127.0.0.1:49403写空闲超时事件发生
通道: /127.0.0.1:49403读空闲超时事件发生
通道: /127.0.0.1:49403读写空闲超时事件发生

进程已结束,退出代码为 -1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992747.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号