Class ReadTimeoutHandler

  • All Implemented Interfaces:
    io.netty5.channel.ChannelHandler

    public class ReadTimeoutHandler
    extends IdleStateHandler
    Raises a ReadTimeoutException when no data was read within a certain period of time.
     // The connection is closed when there is no inbound traffic
     // for 30 seconds.
    
     public class MyChannelInitializer extends ChannelInitializer<Channel> {
         public void initChannel(Channel channel) {
             channel.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(30));
             channel.pipeline().addLast("myHandler", new MyHandler());
         }
     }
    
     // Handler should handle the ReadTimeoutException.
     public class MyHandler implements ChannelHandler {
         @Override
         public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                 throws Exception {
             if (cause instanceof ReadTimeoutException) {
                 // do something
             } else {
                 super.exceptionCaught(ctx, cause);
             }
         }
     }
    
     ServerBootstrap bootstrap = ...;
     ...
     bootstrap.childHandler(new MyChannelInitializer());
     ...
     
    See Also:
    WriteTimeoutHandler, IdleStateHandler
    • Constructor Detail

      • ReadTimeoutHandler

        public ReadTimeoutHandler​(int timeoutSeconds)
        Creates a new instance.
        Parameters:
        timeoutSeconds - read timeout in seconds
      • ReadTimeoutHandler

        public ReadTimeoutHandler​(long timeout,
                                  TimeUnit unit)
        Creates a new instance.
        Parameters:
        timeout - read timeout
        unit - the TimeUnit of timeout
    • Method Detail

      • readTimedOut

        protected void readTimedOut​(io.netty5.channel.ChannelHandlerContext ctx)
                             throws Exception
        Is called when a read timeout was detected.
        Throws:
        Exception