Class WriteTimeoutHandler

  • All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelOutboundHandler

    public class WriteTimeoutHandler
    extends io.netty.channel.ChannelOutboundHandlerAdapter
    Raises a WriteTimeoutException when a write operation cannot finish in a certain period of time.
     // The connection is closed when a write operation cannot finish in 30 seconds.
    
     public class MyChannelInitializer extends ChannelInitializer<Channel> {
         public void initChannel(Channel channel) {
             channel.pipeline().addLast("writeTimeoutHandler", new WriteTimeoutHandler(30);
             channel.pipeline().addLast("myHandler", new MyHandler());
         }
     }
    
     // Handler should handle the WriteTimeoutException.
     public class MyHandler extends ChannelDuplexHandler {
         @Override
         public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                 throws Exception {
             if (cause instanceof WriteTimeoutException) {
                 // do something
             } else {
                 super.exceptionCaught(ctx, cause);
             }
         }
     }
    
     ServerBootstrap bootstrap = ...;
     ...
     bootstrap.childHandler(new MyChannelInitializer());
     ...
     
    See Also:
    ReadTimeoutHandler, IdleStateHandler
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

        io.netty.channel.ChannelHandler.Sharable
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)  
      void write​(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)  
      protected void writeTimedOut​(io.netty.channel.ChannelHandlerContext ctx)
      Is called when a write timeout was detected
      • Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter

        bind, close, connect, deregister, disconnect, flush, read
      • Methods inherited from class io.netty.channel.ChannelHandlerAdapter

        ensureNotSharable, exceptionCaught, handlerAdded, isSharable
      • Methods inherited from interface io.netty.channel.ChannelHandler

        exceptionCaught, handlerAdded
    • Constructor Detail

      • WriteTimeoutHandler

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

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

      • write

        public void write​(io.netty.channel.ChannelHandlerContext ctx,
                          Object msg,
                          io.netty.channel.ChannelPromise promise)
                   throws Exception
        Specified by:
        write in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        write in class io.netty.channel.ChannelOutboundHandlerAdapter
        Throws:
        Exception
      • handlerRemoved

        public void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)
                            throws Exception
        Specified by:
        handlerRemoved in interface io.netty.channel.ChannelHandler
        Overrides:
        handlerRemoved in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        Exception
      • writeTimedOut

        protected void writeTimedOut​(io.netty.channel.ChannelHandlerContext ctx)
                              throws Exception
        Is called when a write timeout was detected
        Throws:
        Exception