问题描述
@Service
public class ScheduledTask {
// Running each hour at 0 min. and 0 seconds
@Scheduled(cron = "0 0 * * * *")
@Retryable(value = Throwable.class,maxAttempts = 2,backoff = @Backoff(delay = 1000))
public void execute() {
log.info("Executing scheduled task!");
throw new HttpTimeoutException("Let's see whether retry will rerun");
}
}
我可以看到正在执行的任务一次。但我没有看到任何重试。 @Scheduled
和 @Retryable
之间是否存在任何不兼容或我遗漏了什么?
或者是否有更好的方法来重试 @Scheduled
任务?
解决方法
确保您的配置类上有 @EnableRetry
注释以启用 @Retryable
注释处理。
使用 @EnableScheduling
安排时间也是如此。
除了@EnableRetry
和@EnableScheduling
之外,还要确保你有spring-aspects(或spring-boot-starter-aop
)的依赖。