gpt4 book ai didi

java - 未调用 httpAsyncClient.execute() 回调方法

转载 作者:行者123 更新时间:2023-12-01 14:35:26 32 4
gpt4 key购买 nike

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();
}

}
}

上面的代码没有调用rest服务,下面是日志-

    *****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--------------

已完成 未被记录。

如果我如下所示切换代码的​​注释 -

    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();
}

然后会调用rest服务,如下图-

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

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

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

shutting down

似乎没有调用 async,如果它调用了 async,那么它将像这样记录 -

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

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

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


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

shutting down

最佳答案

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");

关于java - 未调用 httpAsyncClient.execute() 回调方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30639532/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com