title: Netty - fire ReadTimeoutHandler if multiple reads in channel
body: <p>My I/O flow is following:</p>
  
  <ol>
  <li>client sends data#1 to channel</li>
  <li>server(handler) receives data from database according the client data and sends it to the client</li>
  <li>client sends data#2 to channel</li>
  <li>server(handler) receives data again from database according the client data and sends it back to the client and closes channel</li>
  </ol>
  
  <p>If first read in channel takes too long, ReadTimeoutHandler fires exception as expected. But if first read is ok (= fast enough) and second read in channels takes too long, no TimeoutException is thrown and handler waits 5 minutes until it closes channel. It seems that ReadTimeoutHandler only works for the first read in channel. Is it even possible to get ReadTimeoutHandler work with multiple reads in the channel?</p>
  
  <p>Used Netty version: 4.0.12</p>
  
  <p>public class MyServer {</p>
  
  <pre><code>private static final class MyInitializer extends ChannelInitializer&lt;SocketChannel&gt; {
  
  ...
  
      @Override
      public void initChannel(SocketChannel channel) throws Exception {
          channel.pipeline().addLast(
                  new ReadTimeoutHandler(5, TimeUnit.SECONDS),
                  new MyHandler(server, serverConnection));
      }
  
  ...
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>public class MyHandler extends SimpleChannelInboundHandler {</p>
  
  <pre><code>private static final Logger LOG = LoggerFactory.getLogger(MyHandler.class);
  
  @Override
  public void channelActive(ChannelHandlerContext ctx) throws Exception {
      super.channelActive(ctx);
  }
  
  @Override
  protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
      Message message = database.getMessage(msg);
      ChannelFuture operation = ctx.writeAndFlush(message) 
  
  if (message.isEnd()) operation.addListener(new CloseConverstationListener(ctx));
  }
  
  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
      if (cause instanceof ReadTimeoutException) {
          LOG.error("ReadTimeoutException");
      } 
  }
  
  @Override
  public void channelInactive(ChannelHandlerContext ctx) throws Exception {
      super.channelInactive(ctx);
  }
  
  private class CloseConverstationListener implements GenericFutureListener&lt;ChannelFuture&gt; {
      private final ChannelHandlerContext ctx;
  
      private CloseConverstationListener(ChannelHandlerContext ctx) {
          this.ctx = ctx;
      }
  
      @Override
      public void operationComplete(ChannelFuture future) throws Exception {
          future.channel().close().sync();
      }
  }
  </code></pre>
  
  <p>}</p>