正在尝试配置Sping Boot Retry for Simple Method。当设置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
2条答案
按热度按时间yrdbyhpb1#
在这次变更后,运行良好。
vlju58qv2#
删除
RetryConfiguration
的@Bean
,添加@EnableRetry
时已自动导入相同的配置类。因此配置加倍。当您同时拥有注解和
@Bean
方法时,您将获得用于重试逻辑的双重代理/拦截器。每个拦截器将执行配置的重试次数,但是内部拦截器将在每次被调用时执行配置的重试次数。因此,当重试计数为2时,外部拦截器将调用内部拦截器两次,只有在执行每个调用两次之后才会失败。现在,当将数字增加到3时,外部拦截器最终将调用内部拦截器3次,每次调用都会执行3次重试。