首页 > 解决方案 > Spring Retry 不再调用 maxAttempts

问题描述

 try {
        reponseType = retryTemplate.execute((RetryCallback<X, RetryException>) context -> {
            try {
                log.error("Calling api attempt #" + context.getRetryCount());
                HttpEntity<x> xResponse = httpRestTemplate.exchange(requestUrl, HttpMethod.POST, entity, x.class);
                return xResponse.getBody();
            } catch (HttpStatusCodeException e) {
                if (e.getStatusCode().is5xxServerError()) {
                    throw new RetryException("api returned Server Error", e);
                }
                return null;
            }
        });
 } catch (RetryException e) {
        throw  e;

配置文件中定义了retryTemplate策略,httpRestTemplate为普通模板

@Bean
RetryTemplate retryTemplate() {
    RetryTemplate retryTemplate = new RetryTemplate();
    FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
    fixedBackOffPolicy.setBackOffPeriod(30000);
    retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
    retryTemplate.setRetryPolicy(new CustomRetryPolicy(3));
    return retryTemplate;
}

但在给定时间后无法再次调用 retryTemplate。谁能帮助我,我想我在异常游戏中感到困惑。

标签: spring-bootspring-retry

解决方案


这只会在e.getStatusCode().is5xxServerError(). 否则,您将返回null,从重试模板的角度来看,这是“成功”。模板只会在抛出异常时重试。

您可以在重试策略中对哪些异常可重试进行分类。


推荐阅读