Idle event in Netty

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

 

In the network connection, it is very common to process the Idle event. For example, in the mqtt service, if the client and the server do not have any read and write requests within the specified time, the connection will be considered to be idle . At this time, the client is in the specified idle . If no ping message is sent to the server within the time , the server can disconnect the connection with the client.

The following code demonstrates how to set the idle event in netty .

 

  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事件的处理。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481458&siteId=291194637