首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Apache httpclient中使用指数回退策略?

如何在Apache httpclient中使用指数回退策略?
EN

Stack Overflow用户
提问于 2016-02-14 19:03:55
回答 2查看 5.8K关注 0票数 5

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

此外,我没有找到任何解释如何使用它的东西。

以前有人用过这样的东西吗?

EN

回答 2

Stack Overflow用户

发布于 2016-08-03 05:51:49

如果使用Maven包安装Apache HTTP Client 4.5.x,则如下所示(Grails Maven依赖关系语法):

代码语言:javascript
复制
compile "org.apache.httpcomponents:httpclient:4.5.1"
compile "org.apache.httpcomponents:httpcore:4.4.3"

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

代码语言:javascript
复制
compile 'org.apache.httpcomponents:httpclient-cache:4.5.1'

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

然后你可以像这样把它连接起来:

代码语言:javascript
复制
 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();
 }
票数 1
EN

Stack Overflow用户

发布于 2020-12-04 09:39:16

ExponentialBackOffSchedulingStrategy在缓存条目上工作,这就是为什么它只能与CachingClient一起使用。我不认为它可以用来在失败的请求上实现指数回退重试机制。

我将实现一个简单的指数退避策略,如下所示:

代码语言:javascript
复制
class MyExponentialBackoffRetryHandler extends HttpRequestRetryHandler {
  public MyExponentialBackoffRetryHandler(int maxNumRetries, int backOffRate, int initialExpiry, int maxExpiry) {
  } 
 boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
   if (executionCount == maxNumRetries) {
     return false;
   }else {
     long nextBackOffDelay = initialExpiry * Math.pow(backOffRate, executionCount - 
1)
     long delay = Math.min(maxExpiry, nextBackOffDelay)
     Thread.sleep(delay);
     return true;
   } 

}
}

此外,为了触发重试,您需要添加一个抛出IOException的响应拦截器,例如:

代码语言:javascript
复制
class MyResponseInterceptor extends HttpResponseInterceptor {
  void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
    if (response.getStatusLine.getStatusCode == 503) {
        throw new IOException("Please retry")
    }
  }
}

最后,您可以构建HTTP客户端实例,如下所示:

代码语言:javascript
复制
HttpClientBuilder
    .create()
    .setRetryHandler(new MyExponentialBackoffRetryHandler(...))
    .addInterceptorFirst(new MyResponseInterceptor())
    .build()

您可以通过向https://httpstat.us/503发送请求来轻松地测试客户端

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35391005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档