重试请求之间的超时Apache HttpClient

编程入门 行业动态 更新时间:2024-10-23 01:42:40
本文介绍了重试请求之间的超时Apache HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以分享如何配置现代HttpClient 4.5.3重试失败的请求并在每次重试之前等待一段时间吗?

Could somebody share how to configure modern HttpClient 4.5.3 to retry failed requests and wait for some time before each retry?

到目前为止,看来我正确地理解.setRetryHandler(new DefaultHttpRequestRetryHandler(X, false))将允许重试请求X次.

So far it looks like I got it correctly that .setRetryHandler(new DefaultHttpRequestRetryHandler(X, false)) will allow to retry requests X times.

但是我不明白如何配置退避:根据JavaDocs的.setConnectionBackoffStrategy()/.setBackoffManager()规定了其他事项,而不是重试之间的超时.

But I cannot understand how to configure backoff: .setConnectionBackoffStrategy() / .setBackoffManager() according to JavaDocs regulate something else, not timeout between retries.

推荐答案

关于动态延迟,我想提出以下建议:

About the dynamic delay, I want to suggest this:

CloseableHttpClient client = HttpClientBuilder.create() .setRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { return executionCount <= maxRetries ; } }) .setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() { int waitPeriod = 100; @Override public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) { waitPeriod *= 2; return executionCount <= maxRetries && response.getStatusLine().getStatusCode() >= 500; //important! } @Override public long getRetryInterval() { return waitPeriod; } }) .build();

附录: 请注意,如果发生IO错误(例如超时,端口未打开或连接关闭),将不会调用ServiceUnavailableRetryStrategy.retryRequest.在这种情况下,仅将调用HttpRequestRetryHandler.retryRequest,并且重试将立即发生或在固定的延迟之后发生(我无法最终弄清这一点).因此,oleg的答案实际上是正确的答案.在HttpClient 4.5的支持下无法做到这一点.

Appendix: Please note, that ServiceUnavailableRetryStrategy.retryRequest will NOT be called, if there was an IO error like timeout, port not open or connection closed. In such cases, only HttpRequestRetryHandler.retryRequest will be called, and the retry will happen either immediately or after a fixed delay (I could not finally clarify this). So oleg's answer is actually the right one. There is no way to do it with support of HttpClient 4.5.

(我实际上想将其称为设计错误,因为在现代微服务环境中,IO错误后的延迟重试非常重要.)

(I would actually like to call this a design bug, as delayed retries after an IO error are vitally important in a modern microservice environment.)

更多推荐

重试请求之间的超时Apache HttpClient

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

发布评论

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

>www.elefans.com

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