Class IdleStateHandler

  • All Implemented Interfaces:
    io.netty5.channel.ChannelHandler
    Direct Known Subclasses:
    ReadTimeoutHandler

    public class IdleStateHandler
    extends Object
    implements io.netty5.channel.ChannelHandler
    Triggers an IdleStateEvent when a Channel has not performed read, write, or both operation for a while.

    Supported idle states

    PropertyMeaning
    readerIdleTime an IdleStateEvent whose state is IdleState.READER_IDLE will be triggered when no read was performed for the specified period of time. Specify 0 to disable.
    writerIdleTime an IdleStateEvent whose state is IdleState.WRITER_IDLE will be triggered when no write was performed for the specified period of time. Specify 0 to disable.
    allIdleTime an IdleStateEvent whose state is IdleState.ALL_IDLE will be triggered when neither read nor write was performed for the specified period of time. Specify 0 to disable.
     // An example that sends a ping message when there is no outbound traffic
     // for 30 seconds.  The connection is closed when there is no inbound traffic
     // for 60 seconds.
    
     public class MyChannelInitializer extends ChannelInitializer<Channel> {
         @Override
         public void initChannel(Channel channel) {
             channel.pipeline().addLast("idleStateHandler", new IdleStateHandler(60, 30, 0));
             channel.pipeline().addLast("myHandler", new MyHandler());
         }
     }
    
     // Handler should handle the IdleStateEvent triggered by IdleStateHandler.
     public class MyHandler implements ChannelHandler {
         @Override
         public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
             if (evt instanceof IdleStateEvent) {
                 IdleStateEvent e = (IdleStateEvent) evt;
                 if (e.state() == IdleState.READER_IDLE) {
                     ctx.close();
                 } else if (e.state() == IdleState.WRITER_IDLE) {
                     ctx.writeAndFlush(new PingMessage());
                 }
             }
         }
     }
    
     ServerBootstrap bootstrap = ...;
     ...
     bootstrap.childHandler(new MyChannelInitializer());
     ...
     
    See Also:
    ReadTimeoutHandler, WriteTimeoutHandler
    • Constructor Detail

      • IdleStateHandler

        public IdleStateHandler​(int readerIdleTimeSeconds,
                                int writerIdleTimeSeconds,
                                int allIdleTimeSeconds)
        Creates a new instance firing IdleStateEvents.
        Parameters:
        readerIdleTimeSeconds - an IdleStateEvent whose state is IdleState.READER_IDLE will be triggered when no read was performed for the specified period of time. Specify 0 to disable.
        writerIdleTimeSeconds - an IdleStateEvent whose state is IdleState.WRITER_IDLE will be triggered when no write was performed for the specified period of time. Specify 0 to disable.
        allIdleTimeSeconds - an IdleStateEvent whose state is IdleState.ALL_IDLE will be triggered when neither read nor write was performed for the specified period of time. Specify 0 to disable.
      • IdleStateHandler

        public IdleStateHandler​(long readerIdleTime,
                                long writerIdleTime,
                                long allIdleTime,
                                TimeUnit unit)
        Creates a new instance firing IdleStateEvents.
        Parameters:
        readerIdleTime - an IdleStateEvent whose state is IdleState.READER_IDLE will be triggered when no read was performed for the specified period of time. Specify 0 to disable.
        writerIdleTime - an IdleStateEvent whose state is IdleState.WRITER_IDLE will be triggered when no write was performed for the specified period of time. Specify 0 to disable.
        allIdleTime - an IdleStateEvent whose state is IdleState.ALL_IDLE will be triggered when neither read nor write was performed for the specified period of time. Specify 0 to disable.
        unit - the TimeUnit of readerIdleTime, writeIdleTime, and allIdleTime
    • Method Detail

      • getReaderIdleTimeInMillis

        public long getReaderIdleTimeInMillis()
        Return the readerIdleTime that was given when instance this class in milliseconds.
      • getWriterIdleTimeInMillis

        public long getWriterIdleTimeInMillis()
        Return the writerIdleTime that was given when instance this class in milliseconds.
      • getAllIdleTimeInMillis

        public long getAllIdleTimeInMillis()
        Return the allIdleTime that was given when instance this class in milliseconds.
      • handlerAdded

        public void handlerAdded​(io.netty5.channel.ChannelHandlerContext ctx)
                          throws Exception
        Specified by:
        handlerAdded in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • handlerRemoved

        public void handlerRemoved​(io.netty5.channel.ChannelHandlerContext ctx)
                            throws Exception
        Specified by:
        handlerRemoved in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • channelRegistered

        public void channelRegistered​(io.netty5.channel.ChannelHandlerContext ctx)
                               throws Exception
        Specified by:
        channelRegistered in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • channelActive

        public void channelActive​(io.netty5.channel.ChannelHandlerContext ctx)
                           throws Exception
        Specified by:
        channelActive in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • channelInactive

        public void channelInactive​(io.netty5.channel.ChannelHandlerContext ctx)
                             throws Exception
        Specified by:
        channelInactive in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • channelRead

        public void channelRead​(io.netty5.channel.ChannelHandlerContext ctx,
                                Object msg)
                         throws Exception
        Specified by:
        channelRead in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • channelReadComplete

        public void channelReadComplete​(io.netty5.channel.ChannelHandlerContext ctx)
                                 throws Exception
        Specified by:
        channelReadComplete in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • write

        public io.netty5.util.concurrent.Future<Void> write​(io.netty5.channel.ChannelHandlerContext ctx,
                                                            Object msg)
        Specified by:
        write in interface io.netty5.channel.ChannelHandler
      • channelIdle

        protected void channelIdle​(io.netty5.channel.ChannelHandlerContext ctx,
                                   IdleStateEvent evt)
                            throws Exception
        Is called when an IdleStateEvent should be fired. This implementation calls ChannelHandlerContext.fireChannelInboundEvent(Object).
        Throws:
        Exception