title: java Non-blocking HTTP client
body: <p>I have a high volume java application in which I have to send http posts to another server.
  Currently I'm using <strong>org.apache.commons.httpclient</strong> library:</p>
  
  <pre><code>private static void sendData(String data) {
  HttpClient httpclient = new HttpClient();
  StringRequestEntity requestEntity;
  try {
      requestEntity = new StringRequestEntity(data, "application/json", "UTF-8");
      String address = "http://&lt;my host&gt;/events/"
      PostMethod postMethod = new PostMethod(address);
      postMethod.setRequestEntity(requestEntity);
  
      httpclient.executeMethod(postMethod);
  
  } catch (Exception e) {
      LOG.error("Failed to send data ", e);
  
  }
  }
  </code></pre>
  
  <p>This means I'm sending my http requests synchronously, which doesn't fit my multithreaded high volume app. So I would like to change those calls to asynchronous non-blocking http calls.</p>
  
  <p>I was going through number of options such as <a href="http://hc.apache.org/httpcomponents-asyncclient-4.0.x/examples.html" rel="noreferrer">apache async client</a> and <a href="http://xsocket.sourceforge.net/core/tutorial/V2/TutorialCore.htm" rel="noreferrer">xsocket</a> but was not able to make it work.</p>
  
  <p>Tried <a href="https://github.com/AsyncHttpClient/async-http-client" rel="noreferrer">ning</a>:</p>
  
  <pre><code>private static void sendEventToGrpahiteAsync(String event) {
  LOG.info("\n" + "sendEventToGrpahiteAsync");
  try (AsyncHttpClient asyncHttpClient = new AsyncHttpClient()) {
      BoundRequestBuilder post = asyncHttpClient.preparePost();
      post.addHeader("Content-Type", "application/json");
      post.setBodyEncoding("UTF-8");
      post.setBody(event);
      post.execute(new HttpRequestCompletionHandler());
  } catch (Exception e) {
      LOG.error("Failed to sending event", e);
  }
  }
  </code></pre>
  
  <p>I tried Apache <a href="http://hc.apache.org/httpcomponents-asyncclient-4.0.x/examples.html" rel="noreferrer">HttpAsyncClient</a>:</p>
  
  <pre><code>private static void sendEventToGrpahiteAsync(String event) {
  LOG.info("\n" + "sendEventToGrpahiteAsync");
  try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault()) {
      httpclient.start();
      HttpPost request = new HttpPost(addr);
      StringEntity entity = new StringEntity(event, ContentType.create("application/json", Consts.UTF_8));
      request.setEntity(entity);
      httpclient.execute(request, null);
  } catch (Exception e) {
      LOG.error("Failed to sending event", e);
  }
  }
  </code></pre>
  
  <p>I tried <a href="http://xsocket.sourceforge.net/http/tutorial/V2/TutorialHttp.htm" rel="noreferrer">xsocket</a>:</p>
  
  <pre><code>private static void sendEventToGrpahiteAsync2(String event) {
  LOG.info("\n" + "sendEventToGrpahiteAsync");
  try (INonBlockingConnection con = new NonBlockingConnection(&lt;SERVER_IP&gt;, 80);
      IHttpClientEndpoint httpClientConnection = new HttpClientConnection(con)) {
      IHttpResponseHandler responseHandler = new MyResponseHandler();
      IHttpRequest request = new PostRequest(url_address, "application/json", Consts.UTF_8.toString(), event);
      request.setTransferEncoding(Consts.UTF_8.toString());
      httpClientConnection.send(request, responseHandler);
  } catch (Exception e) {
      LOG.error("Failed to sending event", e);
  }
  }
  </code></pre>
  
  <p>I get no exceptions but the post doesn't get to the target as well.
  To be clear, the target is a <a href="http://graphite.wikidot.com/screen-shots" rel="noreferrer">graphite server</a> so once a post arrives it is clearly seen in a graph. The synchronous posts works well, I can see the result on the graph, but none of the asynchronous posts shows on my destination graph.</p>
  
  <p>What am I missing?</p>
  
  <p>Thanks</p>