Apache HttpClient:限制每秒总调用次数

Apache HttpClient: Limit total calls per second

我需要将每秒 HTTP 调用的数量限制为最多 10。这是根据允许的配额。

HttpClient 有这方面的功能吗?或者任何自定义实现也可以。

您可以尝试 ScheduledThreadPoolExecutor

来自 javadoc:

A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically

您只需使用 schedule 方法并将其传递给 Runnable,其中 Runnable 通过 HttpClient 进行调用。您可以安排 Runnable 到 运行 每秒 10 次,或根据需要安排。 Executor 将通过 HttpClient 对您的呼叫进行排队,并且只有 运行 每秒最多 10 个。