On Wed, 2013-01-23 at 09:16 -0500, Arian wrote:
> 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 ();*
> }
> });
>
Generally I would stay that one should choose an async HTTP client over
a blocking one only when two conditions are met: (1) your application
needs to execute ten thousands of requests concurrently and (2) response
latency tends to be high (connections can stay idle for a considerable
period of time awaiting a response from the target server). Otherwise,
you will be much better off executing requests with a blocking client
using a pool of worker threads.
> 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.
>
Servlet context listener's #contextDestroyed() method would be the right
place.
http://docs.oracle.com/javaee/5/api/javax/servlet/ServletContextListener.html
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]