简体   繁体   中英

Implement fault tolerance in Spring Batch

I have following batch job implemented in Spring Batch config:

@Bean
public Job myJob(Step step1, Step step2, Step step3) {
    return jobs.get("myJob").start(step1).next(step2).next(step3).build();
}

@Bean
public Step step1(ItemReader<String> myReader,
        ItemProcessor<String, String> myProcessor,
        ItemWriter<String> myWriter) {
    return steps.get("step1").<String, String>chunk(1)
            .reader(myReader)
            .processor(myProcessor)
            .writer(myWriter)
            .build();
}

I would like to retry the step1 (also step2 and step3 so forth) on certain exception and rollback the job on any failure (also between the retries). I understand that rollback is not going to be automatic and I am clear what to rollback for each step with writing custom code.

What is the best way to implement this?

Spring framework provides @Retryable and @Recover annotations to retry and to recover when something fails. You can check this article. https://www.baeldung.com/spring-retry

Fault tolerance features in Spring Batch are applied to items in chunk-oriented steps, not to the entire step.

What you can try to do is use a flow with a decider where you restart from step1 if an exception occurs in one of the subsequent steps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM