hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arian <armyofda12mnk...@gmail.com>
Subject Re: Using HttpAsyncClient completely asynchronously, and shutdown client/request appropriately.
Date Wed, 23 Jan 2013 14:16:03 GMT
Hey Oleg,
I figured that out as well yesterday (that the client and/or request is
shutdown/closed before the callback/thread has a chance to do anything with
it).

That was a very quick example of what I was trying to do ... In reality,
one instance of HttpAsyncClient is created for the web application and
re-used as needed.
Each time its used, it will only be used for one request (but up to 200 per
hour).
I just didn't want it to take up any time by waiting for a request when
more important things need to happen like database inserts (just want to
hand it to the httpclient on the side to do 1 peice of work).
I guess I could have also spawned a new Worker Thread, and calling the
regular HttpClient for 1 request (basically doing similar thing as the
HttpAsyncClient, hence why just using HttpAsyncClient).

So my real code looks more similar to this now:

            *final *HttpPost request = new HttpPost("
https://www.some3rdParty.com/write_post_info.php"); //needs to be final so
can use it within the callback to release it

            request.setEntity(ent); //dynamically set new form POST
information

            Future<HttpResponse> future =
*AsyncUtil.getInstance()*.execute(request,
new FutureCallback<HttpResponse>() {
                public void completed(final HttpResponse response) {
                    System.out.println("->" + response.getStatusLine());
                    *request.releaseConnection ();*
                }

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

                public void cancelled() {
                    System.out.println(" cancelled");
                    *request.releaseConnection ();*
                }
            });

Not sure where to shutdown the client for the webapplication. Right now I
just shut it down if http client during the starts up throws an Exception.

Thanks,
-Ari







On Wed, Jan 23, 2013 at 5:48 AM, Oleg Kalnichevski <olegk@apache.org> wrote:

> On Mon, 2013-01-21 at 16:00 -0500, Arian wrote:
> > 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 ();
> > }
>
> Your cod simply shuts down the client in the finally clause before it
> has any chance of completing the request.
>
> Oleg
>
> PS: why on earth are you using HttpAsyncClient for executing a _single_
> request?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

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