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();
}
}
}
上面的代码没有调用其余的服务,下面是日志 -
*****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--------------
completed
未被记录。
如果我切换代码的注释,如下所示 -
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();
}
然后将调用其余服务,如下所示 -
*****get--------------
*****completed--------------
*****loop--------------
*****get--------------
*****completed--------------
*****loop--------------
*****get--------------
*****completed--------------
*****loop--------------
shutting down
似乎异步未被调用,如果它调用async
,那么它将被记录为这样 -
*****get--------------
*****loop--------------
*****get--------------
*****loop--------------
*****get--------------
*****loop--------------
*****completed--------------
*****completed--------------
*****completed--------------
shutting down
答案 0 :(得分:0)
future.get()会等到获得回复。所以调用它会使http请求同步。只需让你的程序等待一段时间,你就可以按预期得到输出。
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");