title: Netty client multiple requests
body: <p>First, I'll explain the situation and the logic that I'm trying to implement:</p>
  
  <ul>
  <li><p>I have multiple threads, each put result of it work, some object called <code>Result</code> into queue <code>QueueToSend</code></p></li>
  <li><p>My <code>NettyClient</code> runs in thread and takes <code>Result</code> from <code>QueueToSend</code> every 1 milisecond and should connect to server and send a message, that is created from <code>Result</code>. 
  I also need this connections to be asynch. So I need the <code>Result</code> list to be known by <code>NettyHandler</code> to send right message and process right result and then again send response.</p></li>
  </ul>
  
  <p>So I initialize <code>NettyClient</code> bootstrap </p>
  
  <pre><code>bootstrap = new ClientBootstrap(
                  new NioClientSocketChannelFactory(
                          Executors.newCachedThreadPool(),
                          Executors.newCachedThreadPool()));
  </code></pre>
  
  <p>and sets pipeline once when app starts.
  Then, every milisecond I take <code>Result</code> object from <code>QueueToSend</code> and connect to server</p>
  
  <pre><code>ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,port);
  ResultConcurrentHashMap.put(future.getChannel().getId(), result);
  </code></pre>
  
  <p>I decided to use static <code>ConcurrentHashMap</code> to save every result object taken from <code>QueueToSend</code> assosiated with channel.</p>
  
  <p>The first problem takes place in <code>NettyHandler</code> in method channelConnected, when I am trying to take <code>Result</code> object assosiated with channel from <code>ResultConcurrentHashMap</code>.</p>
  
  <pre><code>@Override
  public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
         Channel channel = ctx.getPipeline.getChannel();
         Result result = ResultConcurrentHashMap.get(channel.getId());
  }
  </code></pre>
  
  <p>But sometimes <code>result</code> is null (1 of 50), even thought it should be in <code>ResultConcurrentHashMap</code>. I think it happens cause that <code>channelConnected</code> event happens before <code>NettyClient</code> runs this code:</p>
  
  <pre><code>ResultConcurrentHashMap.put(future.getChannel().getId(), result);
  </code></pre>
  
  <p>May be it will not appear if I run <code>NettyServer</code> and <code>NettyClient</code> not on localhost both, but remotely, it will take moretime to estabilish the connection. But I need a solution for this issue.</p>
  
  <p><strong>Another issue</strong> is that I am sending messages every 1 milisecond asynchromously and I suppose that messages are may be mixed and server can not read them properly. If I run them one by one it will be ok :</p>
  
  <pre><code>future.getChannel().getCloseFuture().awaitUninterruptibly();
  </code></pre>
  
  <p>But I need asynchromus sending, and process right results, assosiated with channel and send responses. 
  What should I implement?</p>