0

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?

adesai
  • 370
  • 3
  • 22

2 Answers2

0

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

Sambit
  • 7,625
  • 7
  • 34
  • 65
0

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.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50