【问题标题】:How to wait for all requests to finish如何等待所有请求完成
【发布时间】:2015-04-17 08:58:40
【问题描述】:

我在命令行程序中使用 ning 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() 之前,确保所有呼叫都完成的最佳方法是什么?

【问题讨论】:

  • 你有没有想过这个问题?

标签: asynchttpclient ning


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-26
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多