gpt4 book ai didi

java - 如何在 Apache httpclient 中使用指数退避策略?

转载 作者:搜寻专家 更新时间:2023-10-31 20:32:56 24 4
gpt4 key购买 nike

docs指定一个 ExponentialBackOffSchedulingStrategy 类,但它似乎不存在于版本 4.5 中。

此外,没有找到任何解释如何使用它的内容。

以前有人用过类似的东西吗?

最佳答案

如果您使用这样的 Maven 包安装 Apache HTTP Client 4.5.x(Grails Maven 依赖语法):

compile "org.apache.httpcomponents:httpclient:4.5.1"
compile "org.apache.httpcomponents:httpcore:4.4.3"

您需要添加另一个 jar 来获取这些类,如下所示:

compile 'org.apache.httpcomponents:httpclient-cache:4.5.1'

参见 https://hc.apache.org/httpcomponents-client-4.5.x/download.html

然后你可以这样连接它:

 import org.apache.http.impl.client.*;
import org.apache.http.impl.client.cache.*;
import org.apache.http.client.methods.*;

CloseableHttpClient createClient() {
CachingHttpClientBuilder hcb = new CachingHttpClientBuilder();
CacheConfig cc = CacheConfig.DEFAULT;
ExponentialBackOffSchedulingStrategy ebo = new ExponentialBackOffSchedulingStrategy(cc);
hcb.setSchedulingStrategy(ebo);
CloseableHttpClient hc = hcb.build();
return hc;
}

// You'll need to replace the URL below with something that returns a 5xx error
CloseableHttpClient client = createClient();
HttpUriRequest request = new HttpGet("http://www.example.com/throwsServerError");
for (int i=0; i<4; i++) {
CloseableHttpResponse response = client.execute(request);
println new Date().toString() + " " + response.getStatusLine().getStatusCode();
}

关于java - 如何在 Apache httpclient 中使用指数退避策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391005/

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