3
package com.lt.uadb.app.resource.test;

import java.util.concurrent.Future;

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.DefaultHttpAsyncClient;
import org.apache.http.nio.client.HttpAsyncClient;
import org.junit.Test;

public class AsyncSampleResourceTest {
    private static final String PATH = "http://192.168.1.112:8080/uadb.app/rest/sample/async/get";

    @Test
    public void testHttpAsyncClientFutureCallBack() throws Exception {
        final HttpAsyncClient httpAsyncClient = new DefaultHttpAsyncClient();
        httpAsyncClient.start();
        final HttpGet request = new HttpGet(PATH);
        try {
            for (int i = 0; i < 5; i++) {
                System.out.println("*****get--------------");
                Future<HttpResponse> future = httpAsyncClient.execute(request,
                        new FutureCallback<HttpResponse>() {
                            @Override
                            public void completed(HttpResponse httpResponse) {
                                System.out
                                        .println("*****completed--------------");
                            }

                            @Override
                            public void failed(Exception e) {
                                System.out.println("*****failed--------------");
                            }

                            @Override
                            public void cancelled() {
                                System.out
                                        .println("*****cancelled--------------");
                            }
                        });
                // if run code in try catch,the rest service will invoke
                // try {
                // HttpResponse result = future.get();
                // if (result != null) {
                // System.out.println("Request successfully executed");
                // } else {
                // System.out.println("Request failed");
                // }
                // } catch (InterruptedException | ExecutionException e1) {
                // e1.printStackTrace();
                // }
            }
            System.out.println("*****loop--------------");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("shutting down");
            httpAsyncClient.getConnectionManager().shutdown();
        }

    }
}

The above code is not invoking the rest service, Below is the log -

    *****get--------------
    2015-06-04 16:51:53,372 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 1] start execution
      2015-06-04 16:51:53,385 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 1] Request connection for {}->http://192.168.1.112:8080
      2015-06-04 16:51:53,389 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection request: [route: {}->http://192.168.1.112:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      *****get--------------
    2015-06-04 16:51:53,393 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 2] start execution
      2015-06-04 16:51:53,394 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 2] Request connection for {}->http://192.168.1.112:8080
      2015-06-04 16:51:53,394 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection request: [route: {}->http://192.168.1.112:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      *****get--------------
    2015-06-04 16:51:53,394 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 3] start execution
      2015-06-04 16:51:53,396 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 3] Request connection for {}->http://192.168.1.112:8080
      2015-06-04 16:51:53,396 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection request: [route: {}->http://192.168.1.112:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      *****get--------------
    2015-06-04 16:51:53,396 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 4] start execution
      2015-06-04 16:51:53,397 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 4] Request connection for {}->http://192.168.1.112:8080
      2015-06-04 16:51:53,397 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection request: [route: {}->http://192.168.1.112:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      *****get--------------
    2015-06-04 16:51:53,397 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 5] start execution
      2015-06-04 16:51:53,397 [main] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 5] Request connection for {}->http://192.168.1.112:8080
      2015-06-04 16:51:53,397 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection request: [route: {}->http://192.168.1.112:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      *****loop--------------
    shutting down
    2015-06-04 16:51:53,398 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection manager is shutting down
      2015-06-04 16:51:53,401 [Thread-0] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 1] Cancelled
      *****cancelled--------------
    2015-06-04 16:51:53,402 [main] [org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager] [DEBUG] - Connection manager shut down
      2015-06-04 16:51:53,402 [Thread-0] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 2] Cancelled
      *****cancelled--------------
    2015-06-04 16:51:53,402 [Thread-0] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 3] Cancelled
      *****cancelled--------------
    2015-06-04 16:51:53,402 [Thread-0] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 4] Cancelled
      *****cancelled--------------
    2015-06-04 16:51:53,403 [Thread-0] [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 5] Cancelled
      *****cancelled--------------

The completed is not getting logged.

If I toggle comment of the code as shown below -

    try {
  HttpResponse result = future.get();
  if (result != null) {
    System.out.println("Request successfully executed");
  } else {
    System.out.println("Request failed");
  }
} catch (InterruptedException | ExecutionException e1) {
  e1.printStackTrace();
}

then the rest service will be invoked, as shown below -

      *****get-------------- 
      *****completed--------------
    *****loop--------------

    *****get-------------- 
      *****completed--------------
    *****loop--------------

    *****get-------------- 
      *****completed--------------
    *****loop--------------

    shutting down 

It seems async is not getting invoked, if it's invoke async, then it will be logged like this -

      *****get-------------- 
    *****loop--------------

    *****get-------------- 
    *****loop--------------

    *****get-------------- 
    *****loop--------------


      *****completed--------------
      *****completed--------------
      *****completed--------------

    shutting down 
TheCodingFrog
  • 3,406
  • 3
  • 21
  • 27
geosmart
  • 518
  • 4
  • 15
  • i had tryed jersey client ,and i face the same questrion,the request is blocked until the server response! – geosmart Jun 04 '15 at 09:10
  • if the execut() not call .get() ,the server not response ,and throw canceld like [org.apache.http.impl.nio.client.DefaultHttpAsyncClient] [DEBUG] - [exchange: 5] Cancelled – geosmart Jun 04 '15 at 09:27

1 Answers1

2

future.get() will wait until get a response. So calling this would make http requests synchronized. Just make your program wait for a while, you can get the output as you expected.

        final CountDownLatch latch = new CountDownLatch(requests.length);  
        for (final HttpGet request : requests) {  
            httpclient.execute(request, new FutureCallback<HttpResponse>() {  

                @Override  
                public void completed(final HttpResponse response) {  
                    latch.countDown();  
                    System.out.println("******completed");  
                }  
                @Override  
                public void failed(final Exception ex) {  
                    latch.countDown();  
                    System.out.println("*******failed");  
                }  
                @Override  
                public void cancelled() {  
                    latch.countDown();  
                    System.out.println("********canceled");  
                }  
            });  
        }  
        latch.await();  
        System.out.println("Shutting down");  
Man Shen
  • 401
  • 2
  • 16