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

网络I/o编程模型17 netty框架实现心跳机制

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

网络I/o编程模型17 netty框架实现心跳机制

一 心跳机制说明
IdleStateHandler 是netty提供的处理空闲状态的处理器
long readerIdleTime: 表示多长时间没有读,就会发送一个心跳, 检测包检测是否连接。
long writeIdleTime: 表示多长时间没有写,就会发送一个心跳, 检测包检测是否连接。
long allIdleTime: 表示多长时间没有写,就会发送一个心跳,  检测包检测是否连接。
当IdleStateEvent触发后,就会传递给管道的下一个handler去处理。
二  代码案例

1.server代码

package com.ljf.netty.netty.heartbeat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
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.awt.*;
import java.util.concurrent.TimeUnit;


public class NettyHeartheatServer {
    public static void main(String[] args) {
        //创建两个线程组
        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() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline=ch.pipeline();
                
                pipeline.addLast(new IdleStateHandler(13,5,2, TimeUnit.SECONDS));
                //加入一个对空闲检测进一步处理的handler();
                pipeline.addLast(new MyServerHandler());
            }
        });
        //启动服务器
        ChannelFuture  channelFuture = serverBootstrap.bind(6666).sync();
        channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

2. handler代码

package com.ljf.netty.netty.heartbeat;

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


public class MyServerHandler extends ChannelInboundHandlerAdapter {
    public void userEventTriggered(ChannelHandlerContext ctx, Object msg){
        if(msg instanceof IdleStateEvent){
            IdleStateEvent evnet=(IdleStateEvent)msg;
            String eventType=null;
            switch(evnet.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();
        }
    }
}

3.测试:

启动服务端

 2.启动客户端

 4.测试

1.将自定义handler中:ctx.channel().close() 代码注释掉

 2.服务端:响应一次,后面就不再进行响应了。

 3.客户端

 

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

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

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