adesai
adesai

Reputation: 420

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?

Upvotes: 0

Views: 1879

Answers (2)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31590

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.

Upvotes: 0

Sambit
Sambit

Reputation: 8001

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

Upvotes: 0

Related Questions