atom feed1 message in org.apache.hc.httpclient-usersUsing HttpAsyncClient completely asyn...
FromSent OnAttachments
ArianJan 21, 2013 1:00 pm 
Subject:Using HttpAsyncClient completely asynchronously, and shutdown client/request appropriately.
From:Arian (
Date:Jan 21, 2013 1:00:48 pm

Hello all,

So I'm using the DefaultHttpAsyncClient to send a POST http request to a 3rd party that records some information. Almost like a 'tracking' request, (but they dont want it done on the client-side if JS is disabled, or using a GET via <img src=""/> tag), so I'm making an http POST request to them on the server-side. I also dont want to wait for the request in my code, so using the HttpAsyncClient instead of the HttpClient.

I am iffy on threading/concurrency in Java, but think i get the basic jist. I see that a new thread is created for the FutureCallback. Right now the future.get() line below seems to wait for the 'future' thread to get done before going further in the application (so its a 'blocking' operation on an otherwise non-blocking async callback operation i am doing, right?)... I don't need to do anything with the response (i just want to try to send the http POST and do other things while that happens), so I removed the future.get()... but it errors sometimes [and does the 'cancelled' event]... I think maybe cause the httpclient/request is shutdown/closed before the callback can do its thing maybe?... If so, how can I close the httpclient/request with the completed/failed/cancelled callback operations?

Here is the error i get:

SEVERE: http-outgoing-1 [CLOSED] HTTP protocol exception: null org.apache.http.impl.conn.ConnectionShutdownException at org.apache.http.impl.nio.conn.ManagedClientAsyncConnectionImpl.ensurePoolEntry( at org.apache.http.impl.nio.conn.ManagedClientAsyncConnectionImpl.getRoute( at org.apache.http.client.protocol.RequestProxyAuthentication.process( at org.apache.http.protocol.ImmutableHttpProcessor.process( at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.requestReady( at org.apache.http.impl.nio.client.LoggingAsyncRequestExecutor.requestReady( at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.connected( at org.apache.http.impl.nio.client.LoggingAsyncRequestExecutor.connected( at org.apache.http.impl.nio.DefaultHttpClientIODispatch.onConnected( at org.apache.http.impl.nio.DefaultHttpClientIODispatch.onConnected( at org.apache.http.impl.nio.reactor.AbstractIODispatch.connected( at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionCreated( at org.apache.http.impl.nio.reactor.AbstractIOReactor.processNewChannels( at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute( at org.apache.http.impl.nio.reactor.BaseIOReactor.execute( at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$ at

Here is the code pretty much below: Thanks for any ideas, Ari

//################## POST params to send in async request String f_name = "foo"; String l_name = "bar"; String user_id = "101"; List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("first_name", f_name)); params.add(new BasicNameValuePair("last_name", l_name)); params.add(new BasicNameValuePair("user_id", user_id)); UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params); //##################

HttpAsyncClient httpclient = new DefaultHttpAsyncClient(); httpclient.start(); HttpPost request = null; try { request = new HttpPost(""); request.setEntity(ent);

//sync: //HttpResponse response1 = httpclient.execute(request); //async: Future<HttpResponse> future = httpclient.execute(request, new FutureCallback<HttpResponse>() { public void completed(final HttpResponse response) { System.out.println("->" + response.getStatusLine()); }

public void failed(final Exception ex) { System.out.println("->" + ex); }

public void cancelled() { System.out.println(" cancelled"); } }); //ERRORS sometimes when below future.get() is commented out //HttpResponse response1 = future.get();

doOtherThings(); //like insert stuff into database, etc... i dont want to wait for the response to return for the contacting the 3rd party ... if an exception happens there, then I'll just do stuff in the failed/cancelled methods.

//Do I need to consume the response when using the ASYNC client??? //org.apache.http.HttpEntity entity = response1.getEntity(); //EntityUtils.consume(entity); //do something useful with the response body and ensure it is fully consumed [seems to be needed to make sure stream is closed]

} finally { request.releaseConnection(); httpclient.shutdown (); }