首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【心跳的两种模式】

【心跳的两种模式】

作者头像
贺公子之数据科学与艺术
发布2025-08-29 16:54:33
发布2025-08-29 16:54:33
10200
代码可运行
举报
运行总次数:0
代码可运行
心跳的两种模式是纯净的IdleStateHandler和纯净的IdleStateHandler+次数计时策略。
  1. 纯净的IdleStateHandler模式:使用参数设置的间隔空闲时间,如果超过这个时间,就认为心跳失败。
代码语言:javascript
代码运行次数:0
运行
复制
public class HeartbeatHandler extends ChannelInboundHandlerAdapter {

    private static final int IDLE_TIME = 10; // 设置空闲时间为10秒

    private IdleStateHandler idleStateHandler;

    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        ctx.pipeline().addLast(idleStateHandler = new IdleStateHandler(0, 0, IDLE_TIME));
    }

    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            if (idleStateEvent.state() == IdleState.READER_IDLE) {
                // 心跳失败处理
                ctx.close();
            } else if (idleStateEvent.state() == IdleState.WRITER_IDLE) {
                // 发送心跳消息
                ctx.channel().writeAndFlush("heartbeat");
            }
        }
    }

}
  1. 纯净的IdleStateHandler+次数计时策略模式:在一定时间内连续几次心跳失败才认为连接断开。这种模式考虑了心跳事件之间的时间间隔,如果间隔时间过长,就不会计算为心跳失败。
代码语言:javascript
代码运行次数:0
运行
复制
public class HeartbeatHandler extends ChannelInboundHandlerAdapter {

    private static final int IDLE_TIME = 10; // 设置空闲时间为10秒
    private static final int MAX_FAILURE_TIMES = 3; // 设置最大心跳失败次数为3次
    private static final int MAX_FAILURE_INTERVAL = 5; // 设置心跳失败次数间隔为5秒

    private IdleStateHandler idleStateHandler;
    private int failureTimes;
    private ScheduledExecutorService executorService;

    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        ctx.pipeline().addLast(idleStateHandler = new IdleStateHandler(0, 0, IDLE_TIME));
        failureTimes = 0;
        executorService = Executors.newSingleThreadScheduledExecutor();
    }

    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            if (idleStateEvent.state() == IdleState.READER_IDLE || idleStateEvent.state() == IdleState.WRITER_IDLE) {
                failureTimes++;
                if (failureTimes >= MAX_FAILURE_TIMES) {
                    // 心跳失败次数超过最大次数,断开连接
                    ctx.close();
                } else {
                    // 重新计时
                    executorService.schedule(() -> {
                        failureTimes = 0;
                    }, MAX_FAILURE_INTERVAL, TimeUnit.SECONDS);
                }
            }
        }
    }

    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        executorService.shutdown();
    }

}

这两种模式都是通过空闲检查Handler来实现的,可以根据具体需求选择适合的模式来建立心跳机制。 基于Netty的实现,使用这两种模式之一可以建立心跳机制,保持与服务器的连接。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 心跳的两种模式是纯净的IdleStateHandler和纯净的IdleStateHandler+次数计时策略。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档