`
z_jiankun
  • 浏览: 159532 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Netty中的Idle事件

    博客分类:
  • Java
 
阅读更多

转:http://blog.csdn.net/guanxinquan/article/details/10990337

 

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

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

 

  1. import io.netty.bootstrap.ServerBootstrap;  
  2. import io.netty.channel.ChannelFuture;  
  3. import io.netty.channel.ChannelHandlerContext;  
  4. import io.netty.channel.ChannelInboundHandlerAdapter;  
  5. import io.netty.channel.ChannelInitializer;  
  6. import io.netty.channel.ChannelOption;  
  7. import io.netty.channel.EventLoopGroup;  
  8. import io.netty.channel.nio.NioEventLoopGroup;  
  9. import io.netty.channel.socket.SocketChannel;  
  10. import io.netty.channel.socket.nio.NioServerSocketChannel;  
  11. import io.netty.handler.timeout.IdleState;  
  12. import io.netty.handler.timeout.IdleStateEvent;  
  13. import io.netty.handler.timeout.IdleStateHandler;  
  14.   
  15.   
  16. public class NettyTest {  
  17.   
  18.     public static void main(String[] args) throws InterruptedException {  
  19.         EventLoopGroup bossGroup = new NioEventLoopGroup();   
  20.         EventLoopGroup workerGroup = new NioEventLoopGroup();  
  21.         try {  
  22.             ServerBootstrap b = new ServerBootstrap();  
  23.             b.group(bossGroup, workerGroup)  
  24.              .channel(NioServerSocketChannel.class)  
  25.              .childHandler(new ChannelInitializer<SocketChannel>() {  
  26.                 private static final int IDEL_TIME_OUT = 10;  
  27.                 private static final int READ_IDEL_TIME_OUT = 4;  
  28.                 private static final int WRITE_IDEL_TIME_OUT = 5;  
  29.       
  30.                  @Override  
  31.                  public void initChannel(SocketChannel ch) throws Exception {  
  32.                     ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(READ_IDEL_TIME_OUT, WRITE_IDEL_TIME_OUT, IDEL_TIME_OUT));  
  33.                     ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){  
  34.                         @Override  
  35.                         public void userEventTriggered(  
  36.                                 ChannelHandlerContext ctx, Object evt)  
  37.                                 throws Exception {  
  38.                             if(IdleStateEvent.class.isAssignableFrom(evt.getClass())){  
  39.                                 IdleStateEvent event = (IdleStateEvent) evt;  
  40.                                 if(event.state() == IdleState.READER_IDLE)  
  41.                                     System.out.println("read idle");  
  42.                                 else if(event.state() == IdleState.WRITER_IDLE)  
  43.                                     System.out.println("write idle");  
  44.                                 else if(event.state() == IdleState.ALL_IDLE)  
  45.                                     System.out.println("all idle");  
  46.                             }  
  47.                         }  
  48.                     });  
  49.                  }  
  50.              })  
  51.              .option(ChannelOption.SO_BACKLOG, 128)  
  52.              .childOption(ChannelOption.SO_KEEPALIVE, true);  
  53.               
  54.             ChannelFuture f = b.bind(8080).sync();  
  55.             f.channel().closeFuture().sync();  
  56.         } finally {  
  57.             workerGroup.shutdownGracefully();  
  58.             bossGroup.shutdownGracefully();  
  59.         }  
  60.     }  
  61.       
  62. }  

 

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics