如何等待所有请求完成

编程入门 行业动态 更新时间:2024-10-28 02:26:24
本文介绍了如何等待所有请求完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am using ning AsyncHttpClient from a command line program. I need to wait for all requests to end so I can safely call close() on the client. The challenge is that I make many requests from many different parts of the program. Stripped own code below that shows one scenario where I do a nested HTTP request from the onCompleted of another request:

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()之前,确保所有呼叫完成的最佳方法是什么?

This is an example code only. Real code is more complex and has many more HTTP calls. What will be the best way to make sure all calls are finished before I call close() for the client?

推荐答案

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

For smaller number of requests, collecting all Futures and then use CompletableFuture.allOf(futureCollection).get() might work.

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

According to the docs, this could work as well (not tested yet)

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

更多推荐

如何等待所有请求完成

本文发布于:2023-11-26 14:22:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634121.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!