首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@Retryable with @Scheduled不触发

@Retryable with @Scheduled不触发
EN

Stack Overflow用户
提问于 2021-09-24 14:27:35
回答 2查看 200关注 0票数 0

我已经编写了一个Spring Boot应用程序,其中我已经启用了Spring Batch Scheduling和Retriable,如下所示:

代码语言:javascript
复制
@SpringBootApplication
@EnableBatchProcessing
@EnableScheduling
@EnableRetry
public class FilldashboardApp {

}

我定义了一个控制器,如下:

代码语言:javascript
复制
@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表达式匹配时)

我不知道我的错在哪里

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-24 15:41:44

在Spring重试上查看this issue。你遇到的问题很可能是一个只在4.3.14,5.0.3版本中修复的Spring bug (SPR-16196)。我在没有使用Spring Batch的情况下测试了你的代码片段,它工作得很好。

代码语言:javascript
复制
@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");
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-09-24 14:56:36

您是否尝试过将所有预期的异常都包含为可重试的参数?

代码语言:javascript
复制
  @Retryable(maxAttempts = 10, backoff = @Backoff(delay = 1000),value={JobExecutionAlreadyRunningException.class})

我读了一些教程,我不确定当未指定异常(预期)时,可重试的行为如何

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69316689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档