Spring Retry doesn#39;t works when we use RetryTemplate?(当我们使用RetryTemplate时,Spring重试不起作用吗?)
问题描述
我通过引用from the following course开发了一个重试机制。下面是我在Spring Batch中开发的代码,在这个代码中@Recover
方法没有被调用。我在这里做错了什么?
@EnableRetry
@Configuration
public class RetryConfig {
@Value("${retry.interval.in.seconds}")
private long retryIntervalInSeconds;
@Value("${max.attempts}")
private int attempts;
@Bean
public RetryTemplate mdsRetryTemplate() {
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(attempts);
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(1000 * retryIntervalInSeconds);
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(retryPolicy);
template.setBackOffPolicy(backOffPolicy);
return template;
}
}
下面是控制器
@RestController
@Slf4j
public class BatchJobController {
@Autowired
private JobLauncher jobLauncher;
@Autowired
@Qualifier(value = "sampleAcctJob")
private Job sampleAcctJob;
@Autowired
private RetryTemplate retryTemplate;
@GetMapping(value = "/invoke-job")
public String handle() throws Throwable {
long diff = 0;
JobExecution je= this.invokeJob();
Date start = je.getCreateTime();
Date end = je.getEndTime();
diff = end.getTime() - start.getTime();
return "All data has been loaded successfully.";
}
private JobExecution invokeJob() throws Throwable {
// PDF Job
JobParameters pdfParams = new JobParametersBuilder()
.addString(".id", String.valueOf(System.currentTimeMillis()))
.addDate("date", new Date()).toJobParameters();
return retryTemplate.execute(retryContext -> {
JobExecution jobExecution = jobLauncher.run(sampleAcctJob, pdfParams);
if(!jobExecution.getAllFailureExceptions().isEmpty()) {
log.error("============== sampleAcctJob Job failed, retrying.... ================");
throw jobExecution.getAllFailureExceptions().iterator().next();
}
logDetails(jobExecution);
return jobExecution;
});
}
private void logDetails(JobExecution jobExecution) {
log.info("JOB_NAME = {}, JOB_STATUS = {}, START_TIME={}, END_TIME = {} ",
jobExecution.getJobInstance().getJobName(),
jobExecution.getStatus(),
jobExecution.getStartTime(),
jobExecution.getEndTime());
}
@Recover
private void recover() {
System.out.println("===============");
}
}
推荐答案
您正在以编程方式使用retryTemplate
,因此需要提供RecoveryCallback
作为第二个参数:
retryTemplate.execute(new MyRetryCallback(), new MyRecoveryCallback());
如果要使用使用注释的声明方式,则需要用@Retryable
注释可重试方法,用@Recover
注释恢复方法。
您可以在主页中找到每种方法的示例:https://github.com/spring-projects/spring-retry#quick-start
这篇关于当我们使用RetryTemplate时,Spring重试不起作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当我们使用RetryTemplate时,Spring重试不起作用吗?
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01