Spring Batch重试不适用于retrytemplate [英] spring batch retry not working with retrytemplate
问题描述
在此示例中,我尝试了Spring Batch重试.重试功能在Spring Batch中不起作用,并且可以使用.我正在尝试使用retrytemplate实现相同的功能,但是在抛出异常时看不到重试不起作用.
I tried the spring batch retry in this example. Retry feature is not working in Spring Batch and it works. I am trying to achieve the same with retrytemplate, but couldn't see the retry not working when thrown exception.
@Configuration
@EnableBatchProcessing
//@EnableRetry
public class RetryBatchJob {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
public ItemReader<Integer> itemReader() {
return new ListItemReader<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
}
@Bean
public ItemWriter<Integer> itemWriter() {
return items -> {
for (Integer item : items) {
System.out.println("item = " + item);
if (item.equals(7)) {
throw new Exception("Sevens are sometime nasty, let's retry them");
}
}
};
}
@Bean
public Step step() {
return steps.get("step")
.<Integer, Integer>chunk(2)
.reader(itemReader())
.writer(itemWriter())
/*.faultTolerant()
.retryLimit(5)
.retry(Exception.class)*/
.build();
}
@Bean
public Job job() {
Job job = null;
try {
job = retryTemplate().execute(new RetryCallback<Job, Throwable>() {
@Override
public Job doWithRetry(RetryContext context) throws Throwable {
return jobs.get("job")
.start(step())
.build();
}
});
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return job;
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(RetryBatchJob.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(5, singletonMap(Exception.class, true));
retryPolicy.setMaxAttempts(5);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
}
使用RetryTemplate时会丢失某些东西吗?我也尝试过在步骤和作业方法上进行声明式配置,但是没有运气.
Am I missing something when I use RetryTemplate? I tried declarative configuration on step and job methods too, but no luck.
@Retryable(value = {Exception.class},
maxAttemptsExpression = "5"
)
注意:使用spring-retry 1.2.2 RELEASE.
Note: using spring-retry 1.2.2 RELEASE.
推荐答案
作业执行过程中引发的异常不会停止执行,作业继续执行,直到返回执行结果失败"或已完成...",这使其成为肯定结果用于RetryTemplate.execute().您可以利用返回的执行状态在失败的情况下引发runtimeException.
Thrown exceptions during job execution do not stop the execution, the job continue until it returns the execution result either FAILED OR COMPLETED ... Which makes it a positive result for RetryTemplate.execute(). You can take advantage of the execution status returned to throw a runtimeException in case of failure.
RetryTemplate template = new RetryTemplate();
ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
exponentialBackOffPolicy.setInitialInterval(5000);
exponentialBackOffPolicy.setMultiplier(ExponentialBackOffPolicy.DEFAULT_MULTIPLIER);
exponentialBackOffPolicy.setMaxInterval(ExponentialBackOffPolicy.DEFAULT_MAX_INTERVAL);
Map<Class<? extends Throwable>, Boolean> exceptions = new HashMap<>();
exceptions.put(Exception.class, true);
SimpleRetryPolicy policy = new SimpleRetryPolicy(3, exceptions);
template.setRetryPolicy(policy);
template.setBackOffPolicy(exponentialBackOffPolicy);
template.execute(new RetryCallback<JobExecution, Exception>() {
@Override
public JobExecution doWithRetry(RetryContext context) throws Exception {
return runJob(job, paramMap);
}
});
功能:runJob()
Function: runJob()
public JobExecution runJob(Job job, Map<String, JobParameter> paramMap) throws Exception {
JobExecution exe = jobLauncher.run(job, new JobParameters(paramMap));
if(exe.getStatus().equals(BatchStatus.FAILED))
throw new RuntimeException(exe.toString());
return exe;
}
这篇关于Spring Batch重试不适用于retrytemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!