title: How Netty ChannelFuture works?
body: <p>I've read <a href="http://netty.io/wiki/user-guide-for-4.x.html#the-solution" rel="nofollow noreferrer">Netty Guide</a>, it does not explain much on ChannelFuture. I find ChannelFuture is a complex idea when applying it.</p>
  
  <p>What I am trying to do is to write message to a context after it's initial response. Different from typical request/response flow. I need a flow like this:</p>
  
  <ol>
  <li>Client send request -> Server (netty)</li>
  <li>Server send a response with ctx.writeAndFlush(msg);</li>
  <li>Server send some more message to that ctx after step 2 is complete.</li>
  </ol>
  
  <p>The problem is that if I do something like this, the second write will not send out:</p>
  
  <pre><code>ctx.writeAndFlush(response);
  Message newMsg = createMessage();
  ctx.writeAndFlush(newMsg);   //will not send to client
  </code></pre>
  
  <p>Then I try to use ChannelFuture, it works, but I am not sure if I am logically correct:</p>
  
  <pre><code>ChannelFuture msgIsSent = ctx.writeAndFlush(response);
  if(msgIsSent.isDone())
  {
      Message newMsg = createMessage();
      ctx.writeAndFlush(newMsg);   //this works
  }
  </code></pre>
  
  <p>or should I use a ChannelFutureListener() instead?</p>
  
  <pre><code>ChannelFuture msgIsSent = ctx.writeAndFlush(response);
  msgIsSent.addListener(new ChannelFutureListener(){
  @Override
  public void operationComplete(ChannelFuture future)
      {
         Message newMsg = createMessage();
         ctx.writeAndFlush(newMsg);
      }
  });
  </code></pre>
  
  <p>Will this also works?</p>
  
  <p>Which one is the best practice approach? Is there any potential problem using method 2?</p>