hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arian <armyofda12mnk...@gmail.com>
Subject Using HttpAsyncClient completely asynchronously, and shutdown client/request appropriately.
Date Mon, 21 Jan 2013 21:00:48 GMT
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="
https://www.some3rdParty.com/write_post_info.php?f_name=foo&l_name=bar&user_id=101"/>
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(ManagedClientAsyncConnectionImpl.java:112)
    at
org.apache.http.impl.nio.conn.ManagedClientAsyncConnectionImpl.getRoute(ManagedClientAsyncConnectionImpl.java:245)
    at
org.apache.http.client.protocol.RequestProxyAuthentication.process(RequestProxyAuthentication.java:74)
    at
org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:109)
    at
org.apache.http.nio.protocol.HttpAsyncRequestExecutor.requestReady(HttpAsyncRequestExecutor.java:151)
    at
org.apache.http.impl.nio.client.LoggingAsyncRequestExecutor.requestReady(LoggingAsyncRequestExecutor.java:85)
    at
org.apache.http.nio.protocol.HttpAsyncRequestExecutor.connected(HttpAsyncRequestExecutor.java:101)
    at
org.apache.http.impl.nio.client.LoggingAsyncRequestExecutor.connected(LoggingAsyncRequestExecutor.java:60)
    at
org.apache.http.impl.nio.DefaultHttpClientIODispatch.onConnected(DefaultHttpClientIODispatch.java:107)
    at
org.apache.http.impl.nio.DefaultHttpClientIODispatch.onConnected(DefaultHttpClientIODispatch.java:50)
    at
org.apache.http.impl.nio.reactor.AbstractIODispatch.connected(AbstractIODispatch.java:75)
    at
org.apache.http.impl.nio.reactor.BaseIOReactor.sessionCreated(BaseIOReactor.java:242)
    at
org.apache.http.impl.nio.reactor.AbstractIOReactor.processNewChannels(AbstractIOReactor.java:427)
    at
org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:291)
    at
org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106)
    at
org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:604)
    at java.lang.Thread.run(Thread.java:662)



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("
https://www.some3rdParty.com/write_post_info.php");
    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 ();
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message