hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dmitry Potapov <potapo...@gmail.com>
Subject Re: Issue with nested async request calls
Date Mon, 19 Dec 2016 09:40:55 GMT
Hi,

This is expected behavior. Async client threads process multiple requests
simultaneously, once you get one blocked it became unable to process other
requests, like your subrequest, so it won't be able to complete or fail your
request, so your Future object will never be notified.

Rule of thumb for async client (and async server as well) is to not use any
blocking operations is callbacks.

-- 
Dmitry Potapov

On Sun, Dec 18, 2016 at 10:44:26PM -0500, Sachin Nikumbh wrote:
> Hi,
> 
> I am using the async client to send a simple GET request. In the callback
> that I am using for the first request, I send another async request.
> However, in my second request, I am not using callback but instead waiting
> for response using future.get().
> 
> This call to future.get() blocks forever. If I instead use a callback for
> the 2nd request as well, everything works okay. Here's my test code:
> 
> ===============================================================
> import org.apache.http.HttpResponse;
> import org.apache.http.client.methods.HttpGet;
> import org.apache.http.concurrent.FutureCallback;
> import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
> import org.apache.http.impl.nio.client.HttpAsyncClients;
> import java.util.concurrent.Future;
> 
> public class TestApache {
> 
>     public static void main(String[] args) throws Exception{
> 
>         CloseableHttpAsyncClient httpclient =
> HttpAsyncClients.createDefault();
>         httpclient.start();
>         HttpGet request = new HttpGet("http://www.apache.org/");
>         MyCallback1 mcb = new MyCallback1(httpclient);
>         httpclient.execute(request, mcb);
>     }
> }
> 
> class MyCallback1 implements FutureCallback<HttpResponse> {
> 
>     private CloseableHttpAsyncClient httpclient;
> 
>     public MyCallback1(CloseableHttpAsyncClient httpclient){
>         this.httpclient = httpclient;
>     }
> 
>     public void cancelled() {
>     }
> 
>     public void completed(HttpResponse response) {
>         System.out.println("Received response in outer callback");
>         System.out.println("Response: " + response.getStatusLine());
>         System.out.println("Sending another request from outer callback");
> 
>         HttpGet request = new HttpGet("http://www.apache.org/");
>         MyCallback2 mcb = new MyCallback2(httpclient);
>         Future<HttpResponse> future = httpclient.execute(request, mcb);
> 
>         try {
>             System.out.println("Waiting for response for the request
> created in the outer callback");
>             HttpResponse newResponse = future.get();
>             System.out.println("Inner Response: " +
> newResponse.getStatusLine());
>         }catch(Exception ex){
>             System.out.println(ex);
>         }
>     }
> 
>     public void failed(Exception ex) {
> 
>     }
> }
> 
> class MyCallback2 implements FutureCallback<HttpResponse> {
> 
>     private CloseableHttpAsyncClient httpclient;
> 
>     public MyCallback2(CloseableHttpAsyncClient httpclient){
>         this.httpclient = httpclient;
>     }
> 
>     public void cancelled() {}
> 
>     public void completed(HttpResponse response) {
> 
>         System.out.println("Received response inside inner callback");
>         System.out.println("Response: " + response.getStatusLine());
>         try {
>             httpclient.close();
>         }catch(Exception ex){
>             System.out.println(ex);
>         }
>     }
> 
>     public void failed(Exception ex) {}
> }
> ===============================================================
> 
> If you replace the entire try-catch in the completed method of MyCallback1,
> the test program works fine.
> 
> Is this expected? If so, why?
> 
> Thanks
> Sachin

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message