首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以根据HttpStatus状态码在spring-retry中设置RetryPolicy吗?

可以根据HttpStatus状态码在spring-retry中设置RetryPolicy吗?
EN

Stack Overflow用户
提问于 2014-12-02 03:56:53
回答 3查看 10.9K关注 0票数 12

是否可以根据错误状态码在弹簧重试(https://github.com/spring-projects/spring-retry)中设置RetryPolicy?例如,我想在HttpStatus.INTERNAL_SERVER_ERROR状态码为503的HttpServerErrorException上重试。因此,它应该忽略所有其他错误代码-- 500 - 502和504 - 511。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-02 23:05:02

RestTemplatesetErrorHandler选项,DefaultResponseErrorHandler是缺省选项。

它的代码如下所示:

代码语言:javascript
复制
public void handleError(ClientHttpResponse response) throws IOException {
    HttpStatus statusCode = getHttpStatusCode(response);
    switch (statusCode.series()) {
        case CLIENT_ERROR:
            throw new HttpClientErrorException(statusCode, response.getStatusText(),
                    response.getHeaders(), getResponseBody(response), getCharset(response));
        case SERVER_ERROR:
            throw new HttpServerErrorException(statusCode, response.getStatusText(),
                    response.getHeaders(), getResponseBody(response), getCharset(response));
        default:
            throw new RestClientException("Unknown status code [" + statusCode + "]");
    }
}

因此,您可以为该方法提供自己的实现,以简化所需的状态代码周围的RetryPolicy

票数 9
EN

Stack Overflow用户

发布于 2018-05-17 02:21:32

对于其他面临同样问题的人,我发布了这个答案。实现自定义重试策略,方法如下:

代码语言:javascript
复制
class InternalServerExceptionClassifierRetryPolicy extends ExceptionClassifierRetryPolicy {
public InternalServerExceptionClassifierRetryPolicy() {
    final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
    simpleRetryPolicy.setMaxAttempts(3);

    this.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {
        @Override
        public RetryPolicy classify(Throwable classifiable) {
            if (classifiable instanceof HttpServerErrorException) {
                // For specifically 500 and 504
                if (((HttpServerErrorException) classifiable).getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR
                        || ((HttpServerErrorException) classifiable)
                                .getStatusCode() == HttpStatus.GATEWAY_TIMEOUT) {
                    return simpleRetryPolicy;
                }
                return new NeverRetryPolicy();
            }
            return new NeverRetryPolicy();
        }
    });
}}

Ans简单地叫它如下:

代码语言:javascript
复制
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(new InternalServerExceptionClassifierRetryPolicy())
票数 7
EN

Stack Overflow用户

发布于 2021-12-21 09:50:05

您也可以在SinmpleRetryPolicy的retryableExceptions列表中添加具体的错误码。

代码语言:javascript
复制
Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>();
retryableExceptions.put(HttpClientErrorException.Unauthorized.class, true);
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(5, retryableExceptions));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27236216

复制
相关文章

相似问题

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