码迷,mamicode.com
首页 > Web开发 > 详细

netty中使用IdleStateHandler来发起心跳

时间:2015-07-07 17:18:35      阅读:20805      评论:0      收藏:0      [点我收藏+]

标签:

网络连接中,处理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>() {
				@Override
				public 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() {
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) {
		for (int i = 0; i < 10; i++) {
			ctx.writeAndFlush(Unpooled.copiedBuffer(ECHO_REQ.getBytes()));
		}
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("This is " + ++counter + " times receive server : [" + msg + "]");
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		cause.printStackTrace();
		ctx.close();
	}

	@Override
	public 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操作即可!


netty中使用IdleStateHandler来发起心跳

标签:

原文地址:http://my.oschina.net/mn1127/blog/475696

(3)
(6)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!