Netty心跳检测机制(转载)


Netty心跳检测机制(转载)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41931364/article/details/122285046

源码剖析

protected void run(ChannelHandlerContext ctx) {
    long nextDelay = IdleStateHandler.this.readerIdleTimeNanos;
    if (!IdleStateHandler.this.reading) {
    	// IdleStateHandler.this.ticksInNanos() - IdleStateHandler.this.lastReadTime 是当前时间减去上一次读取数据的时间
    	// nextDelay 是设置的超时时间
        nextDelay -= IdleStateHandler.this.ticksInNanos() - IdleStateHandler.this.lastReadTime;
    }

	// 说明已经超时了
    if (nextDelay <= 0L) {
   		// 重新起一个线程,重新开始
        IdleStateHandler.this.readerIdleTimeout = IdleStateHandler.this.schedule(ctx, this, IdleStateHandler.this.readerIdleTimeNanos, TimeUnit.NANOSECONDS);
        boolean first = IdleStateHandler.this.firstReaderIdleEvent;
        IdleStateHandler.this.firstReaderIdleEvent = false;

        try {
        	// 准备后续的处理
            IdleStateEvent event = IdleStateHandler.this.newIdleStateEvent(IdleState.READER_IDLE, first);
            // 这里面会调用下一个handler的fireUserEventTriggered方法
            IdleStateHandler.this.channelIdle(ctx, event);
        } catch (Throwable var6) {
            ctx.fireExceptionCaught(var6);
        }
    } 
	// 没有超时,将和超时的时间差补齐再次执行,形成一个定时任务
	else {
        IdleStateHandler.this.readerIdleTimeout = IdleStateHandler.this.schedule(ctx, this, nextDelay, TimeUnit.NANOSECONDS);
    }

}

2.2.2、自定义处理类

这个得实现上面调用的 fireUserEventTriggered 方法

public class HeartBeatServerHandler extends SimpleChannelInboundHandler<String> {

    int readIdleTimes = 0;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
        System.out.println(" ====== > [server] message received : " + s);
        if ("Heartbeat Packet".equals(s)) {
            ctx.channel().writeAndFlush("ok");
        } else {
            System.out.println(" 其他信息处理 ... ");
        }
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        IdleStateEvent event = (IdleStateEvent) evt;

        String eventType = null;
        switch (event.state()) {
            case READER_IDLE:
                eventType = "读空闲";
                readIdleTimes++; // 读空闲的计数加1
                break;
            case WRITER_IDLE:
                eventType = "写空闲";
                // 不处理
                break;
            case ALL_IDLE:
                eventType = "读写空闲";
                // 不处理
                break;
        }



        System.out.println(ctx.channel().remoteAddress() + "超时事件:" + eventType);
        if (readIdleTimes > 3) {
            System.out.println(" [server]读空闲超过3次,关闭连接,释放更多资源");
            ctx.channel().writeAndFlush("idle close");
            ctx.channel().close();
        }
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.err.println("=== " + ctx.channel().remoteAddress() + " is active ===");
    }
}