gpt4 book ai didi

asynchttpclient - 如何等待所有请求完成

转载 作者:行者123 更新时间:2023-12-04 06:52:12 29 4
gpt4 key购买 nike

我正在使用宁 AsyncHttpClient从命令行程序。我需要等待所有请求结束,这样我才能安全地调用 close()在客户端。挑战在于我从程序的许多不同部分提出了许多请求。下面剥离了自己的代码,显示了我从 onCompleted 执行嵌套 HTTP 请求的一种情况。另一个请求:

final AsyncHttpClient asyncHttpClient = new AsyncHttpClient();

Future<Response> f1 = asyncHttpClient.prepareGet(url).execute(
new AsyncCompletionHandler<Response>() {

public Response onCompleted(Response response)
throws Exception {

//Make more HTTP calls
Future f2 = asyncHttpClient.prepareGet(url).execute(...);

return response;
}
});

这只是一个示例代码。真正的代码更复杂,并且有更多的 HTTP 调用。在我调用 close() 之前,确保所有通话都已完成的最佳方法是什么?为客户?

最佳答案

对于较少数量的请求,收集所有 Future s 然后使用 CompletableFuture.allOf(futureCollection).get()可能会奏效。

根据文档,这也可以工作(尚未测试)

((DefaultAsyncHttpClient)asyncHttpClient)
.getEventLoopGroup()
.awaitTermination(1, TimeUnit.MINUTES);

关于asynchttpclient - 如何等待所有请求完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28548375/

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