4

我在AsyncHttpClient命令行程序中使用 ning。我需要等待所有请求结束,这样我才能安全地调用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()在我打电话给客户之前,确保所有电话都完成的最佳方法是什么?

4

1 回答 1

0

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

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

((DefaultAsyncHttpClient)asyncHttpClient)
  .getEventLoopGroup()
  .awaitTermination(1, TimeUnit.MINUTES);
于 2017-09-20T08:11:30.593 回答