Andres Felipe
Andres Felipe

Reputation: 684

How to create a fault tolerant JobStep

I have a Spring Batch project where I have several jobs. I moduralized them in a single job to execute them sequentially like this:

@Autowired
@Qualifier("job1")
private Job job1;

@Autowired
@Qualifier("job2")
private Job job2;

@Autowired
@Qualifier("job3")
private Job job3;

@Bean(name = "step1")
public Step step1() {
    return stepBuilderFactory.get("step1").job(job1).build();
}

@Bean(name = "step2")
public Step step2() {
    return stepBuilderFactory.get("step2").job(job2).build();
}

@Bean(name = "step3")
public Step step3() {
    return stepBuilderFactory.get("step3").job(job3).build();
}

@Bean(name = "parentJob")
public Job parentJob(@Qualifier("step1") Step step1, @Qualifier("step2") Step step2, @Qualifier("step3") Step step3) {
    return jobBuilderFactory.get("parentJob")
            .incrementer(new RunIdIncrementer())
            .repository(jobRepository)
            .start(step1)
            .next(step2)
            .next(step3)
            .build();
}

However if one of the JobSteps fail the parent job fails as well. Is there any mechanism to render fault tolerant all the JobSteps so that if one of them fails the parent job continues?

Upvotes: 0

Views: 234

Answers (1)

Sabir Khan
Sabir Khan

Reputation: 10132

You are basically looking for a conditional flow as described here i.e. you want to move on to next job even if previous job has failed and that is not default flow. You need to use pattern - * to achieve that.

In Java Config, it would be like , .on("*").to(...) and so on.

Your pattern parameter to on(..) can be anything from ExitStatus and * provided you are not using any other custom statuses.

Hope it helps !!

Upvotes: 1

Related Questions