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