java - @Retryable 和 @Scheduled 不会触发
问题描述
我编写了一个 Spring Boot 应用程序,其中启用了 Spring Batch Scheduling 和 Retriable,如下所示:
@SpringBootApplication
@EnableBatchProcessing
@EnableScheduling
@EnableRetry
public class FilldashboardApp {
}
我定义了一个控制器如下:
@Component
public class JobRunnerStats {
@Scheduled(cron = "0 0/2 * ? * *")
@Retryable(maxAttempts = 10, backoff = @Backoff(delay = 1000))
protected JobExecution runJob() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
JobParameters jobParameters = null;
if (this.jobParameters == null) {
jobParameters = buildJobParameters(null);
} else {
jobParameters = this.jobParameters;
this.jobParameters = null;
}
return simpleJobLauncher.run(this.jobStats, jobParameters);
}
}
2 分钟后,由于指定的 cron 表达式启动作业,因此在第一步中,我会将其发送到异常中,因此在作业执行和步骤中我已经跟踪了FAILED
状态。
因此,正如@Retryable 注释所解释的那样,我正在等待 1 秒(延迟 = 1000 微秒)后调用该作业,但该方法中的下一次访问是在 2 分钟后(因此,当 cron 表达式匹配时)
我不知道我的错在哪里
解决方案
在 Spring Retry 上查看此问题。您遇到的问题很可能是仅在 4.3.14、5.0.3 版本中修复的 Spring 错误 ( SPR-16196 ) 我在不使用 Spring Batch 的情况下尝试了您的代码段,它工作正常。
@Component
public class TestComponent {
Logger logger = LoggerFactory.getLogger(TestApplication.class);
@Retryable(maxAttempts = 10, backoff = @Backoff(delay = 1000))
@Scheduled(cron = "0 0/2 * ? * *")
public void foo() {
logger.info("foo");
throw new RuntimeException("runtime");
}
}
推荐阅读
- ios - 仅在 iOS 设备上提供字体文件的错误“未实现类型:7”
- javascript - 如何加快 Google ReCAPTCHA V3 页面加载速度
- javascript - javascript中if块内的变量和函数范围
- python - 安装库时 Pip 安装速度慢得离谱
- java - 当我尝试从手机上的 Android Studio 运行应用程序时,我的应用程序显示运行时异常
- python - 使用小部件修改 django 模型表单字段
- mysql - 在 MySQL 中使用 ROLLUP 时将小数列四舍五入
- python - python差异中的差异方法OLSregression
- python - 谷歌 colab 中的文件大于 RAM
- intellij-idea - 如何使用 IntelliJ IDEA 获取场景示例计数?