zoukankan      html  css  js  c++  java
  • :Netty中的Idle事件

    网络连接中,处理Idle事件是很常见的,比如在mqtt服务中,客户端与服务端在指定时间内没有任何读写请求,就会认为连接是idle的,此时,客户端在指定的idle时间内没有向服务端发送ping消息,服务端可以断开与客户端的链接。

    下面的代码演示了在netty中如何设置idle事件。

     

    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.ChannelOption;
    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.timeout.IdleState;
    import io.netty.handler.timeout.IdleStateEvent;
    import io.netty.handler.timeout.IdleStateHandler;
    
    
    public class NettyTest {
    
    	public static void main(String[] args) throws InterruptedException {
    	    EventLoopGroup bossGroup = new NioEventLoopGroup(); 
    	    EventLoopGroup workerGroup = new NioEventLoopGroup();
    	    try {
    	        ServerBootstrap b = new ServerBootstrap();
    	        b.group(bossGroup, workerGroup)
    	         .channel(NioServerSocketChannel.class)
    	         .childHandler(new ChannelInitializer<SocketChannel>() {
    	        	private static final int IDEL_TIME_OUT = 10;
    	            private static final int READ_IDEL_TIME_OUT = 4;
    	        	private static final int WRITE_IDEL_TIME_OUT = 5;
    	
    	             @Override
    	             public void initChannel(SocketChannel ch) throws Exception {
    	         		ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(READ_IDEL_TIME_OUT, WRITE_IDEL_TIME_OUT, IDEL_TIME_OUT));
    	                ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
    	                	@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");
    	                		}
    	                	}
    	                });
    	             }
    	         })
    	         .option(ChannelOption.SO_BACKLOG, 128)
    	         .childOption(ChannelOption.SO_KEEPALIVE, true);
    	        
    	        ChannelFuture f = b.bind(8080).sync();
    	        f.channel().closeFuture().sync();
    	    } finally {
    	        workerGroup.shutdownGracefully();
    	        bossGroup.shutdownGracefully();
    	    }
    	}
    	
    }
    

     

    首先添加了idleStateHandler用于监听链接idle,如果连接到达idle时间,这个handler会触发idleEvent,之后通过重写userEventTriggered方法,完成idle事件的处理。

    可以用telnet进行测试:

    telnet 127.0.0.1 8080

    测试结果:

    read idle

    write idle

    read idle

    all idle

    write idle

    read idle

    write idle

    read idle

  • 相关阅读:
    SDUT-3376_数据结构实验之查找四:二分查找
    SDUT-3375_数据结构实验之查找三:树的种类统计
    SDUT-3373_数据结构实验之查找一:二叉排序树
    深度优先遍历和广度优先遍历
    SDUT-2498_AOE网上的关键路径
    SDUT-2140_判断给定图是否存在合法拓扑序列
    SDUT-2144_最小生成树
    SDUT-3364_欧拉回路
    SDUT-3363_驴友计划
    Java练习 SDUT-2271_Eddy的难题
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318131.html
Copyright © 2011-2022 走看看