首页 > 解决方案 > @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 表达式匹配时)

我不知道我的错在哪里

标签: javaspring-bootspring-batchspring-retry

解决方案


在 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");
    }
}

推荐阅读