You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Retry maxAttempts配置不生效,执行次数超出预期

Spring Boot重试机制异常:设置maxAttempts后执行次数远超预期

问题现象

配置Spring Boot方法重试时,设置maxAttempts=3,目标方法却执行了9次;设置maxAttempts=2时执行了4次,不符合预期的重试次数。

相关代码

控制器类

@GetMapping("/getPendingTrailRenewals")
public void saveUserSubscription() {
    userSubscriptionService.checkMethod();
}

带重试配置的服务类

@Override
@Retryable(retryFor = DatabaseOperationException.class, maxAttempts = 3, backoff = @Backoff(delay = 100))
public void checkMethod(){
    log.info("checkMethod executed");
    throw new DatabaseOperationException("Testing");
}

重试配置类

@Configuration
@EnableRetry
public class RetryConfig {
    @Bean
    public RetryConfiguration retryConfiguration() {
        return new RetryConfiguration();
    }
}

执行日志

2023-09-25T18:15:42.260+05:30  INFO 21340 --- [nio-8081-exec-1] o.a.c.c.C.[.[localhost].[/api/v1]        : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-09-25T18:15:42.260+05:30  INFO 21340 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-09-25T18:15:42.261+05:30  INFO 21340 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
2023-09-25T18:15:42.297+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=0
2023-09-25T18:15:42.297+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=0
2023-09-25T18:15:42.298+05:30  INFO 21340 --- [nio-8081-exec-1] c.a.a.a.s.i.UserSubscriptionServiceImpl  : checkMethod executed
2023-09-25T18:15:43.298+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=1
2023-09-25T18:15:43.299+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=1
2023-09-25T18:15:43.299+05:30  INFO 21340 --- [nio-8081-exec-1] c.a.a.a.s.i.UserSubscriptionServiceImpl  : checkMethod executed
2023-09-25T18:15:44.303+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=2
2023-09-25T18:15:44.305+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=2
2023-09-25T18:15:44.305+05:30  INFO 21340 --- [nio-8081-exec-1] c.a.a.a.s.i.UserSubscriptionServiceImpl  : checkMethod executed
2023-09-25T18:15:44.305+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=3
2023-09-25T18:15:44.306+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry failed last attempt: count=3
2023-09-25T18:15:45.308+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=1
2023-09-25T18:15:45.308+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=1
2023-09-25T18:15:45.309+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=0
2023-09-25T18:15:45.309+05:30  INFO 21340 --- [nio-8081-exec-1] c.a.a.a.s.i.UserSubscriptionServiceImpl  : checkMethod executed
2023-09-25T18:15:46.311+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=1
2023-09-25T18:15:46.311+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=1
2023-09-25T18:15:46.311+05:30  INFO 21340 --- [nio-8081-exec-1] c.a.a.a.s.i.UserSubscriptionServiceImpl  : checkMethod executed
2023-09-25T18:15:47.312+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=2
2023-09-25T18:15:47.312+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=2
2023-09-25T18:15:47.312+05:30  INFO 21340 --- [nio-8081-exec-1] c.a.a.a.s.i.UserSubscriptionServiceImpl  : checkMethod executed
2023-09-25T18:15:47.313+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=3
2023-09-25T18:15:47.313+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry failed last attempt: count=3
2023-09-25T18:15:48.314+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=2
2023-09-25T18:15:48.314+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=2
2023-09-25T18:15:48.314+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=0
2023-09-25T18:15:48.314+05:30  INFO 21340 --- [nio-8081-exec-1] c.a.a.a.s.i.UserSubscriptionServiceImpl  : checkMethod executed
2023-09-25T18:15:49.315+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=1
2023-09-25T18:15:49.315+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=1
2023-09-25T18:15:49.315+05:30  INFO 21340 --- [nio-8081-exec-1] c.a.a.a.s.i.UserSubscriptionServiceImpl  : checkMethod executed
2023-09-25T18:15:50.317+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=2
2023-09-25T18:15:50.317+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry: count=2
2023-09-25T18:15:50.317+05:30  INFO 21340 --- [nio-8081-exec-1] c.a.a.a.s.i.UserSubscriptionServiceImpl  : checkMethod executed
2023-09-25T18:15:50.318+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=3
2023-09-25T18:15:50.318+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry failed last attempt: count=3
2023-09-25T18:15:50.318+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=3
2023-09-25T18:15:50.318+05:30 DEBUG 21340 --- [nio-8081-exec-1] o.s.retry.support.RetryTemplate          : Retry failed last attempt: count=3
2023-09-25T18:15:50.323+05:30 ERROR 21340 --- [nio-8081-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/api/v1] threw exception [Request processing failed: com.arimac.asec.adminusermgtservice.exception.DatabaseOperationException: Testing] with root cause

问题原因

日志中重复出现Retry: count=0,说明重试代理被多次创建,出现了嵌套重试的情况。根源在于RetryConfig中手动注册了RetryConfiguration的Bean,而@EnableRetry注解已经会自动配置Spring Retry的核心组件,手动添加该Bean会导致重试逻辑被多层代理,最终执行次数呈倍数增长。

解决方案

删除RetryConfig中的retryConfiguration()方法,仅保留@Configuration和@EnableRetry注解即可:

@Configuration
@EnableRetry
public class RetryConfig {
}

修改后,重试逻辑会按照@Retryable中配置的maxAttempts正常执行:设置maxAttempts=3时方法会执行3次(1次初始调用+2次重试),设置maxAttempts=2时执行2次(1次初始调用+1次重试)。

内容的提问来源于stack exchange,提问作者akalanka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 22:05:54