netty中使用IdleStateHandler来发起心跳
来源:互联网 发布:海涛旅游 知乎 编辑:程序博客网 时间:2024/05/17 17:56
ps
:这个写在client 端即可,不需要加在server端
网络连接中,处理Idle事件是很常见的,一般情况下,客户端与服务端在指定时间内没有任何读写请求,就会认为连接是idle的。此时,客户端需要向服务端发送ping消息,来维持服务端与客户端的链接。那么怎么判断客户端在指定时间里没有任何读写请求呢?netty中为我们提供一个特别好用的IdleStateHandler来干这个苦差事!请看下面代码:
public class EchoClient {private final static int readerIdleTimeSeconds = 40;//读操作空闲30秒private final static int writerIdleTimeSeconds = 50;//写操作空闲60秒private final static int allIdleTimeSeconds = 100;//读写全部空闲100秒 public void connect(int port, String host) throws Exception {// 配置客户端NIO线程组EventLoopGroup group = new NioEventLoopGroup();try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() {@Overridepublic void initChannel(SocketChannel ch)throws Exception { ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(readerIdleTimeSeconds, writerIdleTimeSeconds,allIdleTimeSeconds)); ch.pipeline().addLast( new DelimiterBasedFrameDecoder(1024, delimiter)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new EchoClientHandler());} }); // 发起异步连接操作 ChannelFuture f = b.connect(host, port).sync(); // 当代客户端链路关闭 f.channel().closeFuture().sync();} finally { // 优雅退出,释放NIO线程组 group.shutdownGracefully();} } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception {int port = 8080;if (args != null && args.length > 0) { try {port = Integer.valueOf(args[0]); } catch (NumberFormatException e) {// 采用默认值 }}new EchoClient().connect(port, "127.0.0.1"); }}
在netty的客户端中添加:
ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(readerIdleTimeSeconds, writerIdleTimeSeconds,allIdleTimeSeconds));
这个处理器,它的作用就是用来检测客户端的读取超时的,该类的第一个参数是指定读操作空闲秒数,第二个参数是指定写操作的空闲秒数,第三个参数是指定读写空闲秒数,当有操作操作超出指定空闲秒数时,便会触发UserEventTriggered事件。所以我们只需要在自己的handler中截获该事件,然后发起相应的操作即可(比如说发起ping操作)。以下是我们自定义的handler中的代码:
public class EchoClientHandler extends ChannelHandlerAdapter {private int counter;static final String ECHO_REQ = "Hi, Lilinfeng. Welcome to Netty.$_";public EchoClientHandler() {}@Overridepublic void channelActive(ChannelHandlerContext ctx) {for (int i = 0; i < 10; i++) {ctx.writeAndFlush(Unpooled.copiedBuffer(ECHO_REQ.getBytes()));}}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println("This is " + ++counter + " times receive server : [" + msg + "]");}@Overridepublic void channelReadComplete(ChannelHandlerContext ctx) throws Exception {ctx.flush();}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {cause.printStackTrace();ctx.close();}@Overridepublic void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {IdleStateEvent event = (IdleStateEvent) evt;if (event.state() == IdleState.READER_IDLE)System.out.println("read idle");else if (event.state() == IdleState.WRITER_IDLE)System.out.println("write idle");else if (event.state() == IdleState.ALL_IDLE)System.out.println("all idle");}}
这里,我们重点看重新的 userEventTriggered方法:
首先,判断evt事件是不是IdleStateEvent事件;
然后,继续判断是读空闲事件还是写空闲事件还是读写空闲事件;
最后,根据不同事件类型发起相应的操作
好了,现在回到我们的主题,我们的目的是在客户端在写空闲超时时,客户端主动发起一次平操作,所以我们只需要判断写空闲超时,发起ping操作即可!
0 0
- netty中使用IdleStateHandler来发起心跳
- netty中使用IdleStateHandler来发起心跳
- Netty 之 Netty心跳之IdleStateHandler
- Netty心跳检测篇之IdleStateHandler
- 一起学Netty(十一)之 Netty心跳之IdleStateHandler
- 一起学Netty(十一)之 Netty心跳之IdleStateHandler
- 一起学Netty(十一)之 Netty心跳之IdleStateHandler
- netty 心跳与IdleStateHandler与断线重连
- Netty学习(五)—IdleStateHandler心跳机制
- Netty--心跳
- Netty.心跳
- socket中使用心跳来检测连接是否断开
- Netty保持心跳
- netty 心跳检测
- Netty心跳检测
- Netty心跳机制
- 浅析 Netty心跳机制
- netty自定义心跳机制
- linux 多线程详解(互斥、信号、条件cond、锁)
- hdu 1272 并查集的应用
- 大数据_Storm_Storm之--配置项详解
- ionic插件基础(六)——自定义插件的demo
- Spring两大核心概念之IOC
- netty中使用IdleStateHandler来发起心跳
- HDU1031——Design T-Shirt(结构体排序)
- android线程池原理和实现
- UVA 784-Maze Exploration#
- Content-Type:application/x-www-form-urlencoded 和 multipart/form-data
- 使用tableHeaderView的时候动态改变高度
- [BZOJ4521][CQOI2016] 手机号码 - 数位DP
- apt-get remove, apt-get autoremove和aptitude remove的区别
- 每天一个linux命令(12):more命令