daniel_ip
daniel_ip

Reputation: 21

Spring batch continue job when a step fails

I need the job to continue when one of the steps fails. Steps are dynamically generated and are independent.

  public Step step(Long id) {
    return stepBuilderFactory.get("STEP_" + id)
        .tasklet((contribution, chunkContext) -> {
          service.action(id);
          return RepeatStatus.FINISHED;
        }).build();
  }

I want to save the FAILED state if it fails but not terminate the execution. Is there a way to do it? Thanks

Upvotes: 0

Views: 3130

Answers (2)

daniel_ip
daniel_ip

Reputation: 21

This is the job flow. It is generated dynamically. There will always be a first step (remove(0)). This works for me. I would only need to implement the jump of the error with the failed step

SimpleJobBuilder jobBuilder = this.jobBuilderFactory.get("JOB").start(steps.remove(0));
      for(Step step : steps) {
        jobBuilder.next(step);
      }
return jobBuilder.build();

Upvotes: 0

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31590

You did not share your job flow definition, but it seems like it is a sequential flow, in which the default is to fail the job at the first step that fails, see Sequential Flow.

You need to define a conditional flow, as documented in Conditional Flow. Here is a quick example:

@Bean
public Job job() {
    return jobBuilderFactory.get("job")
                .start(stepA())
                .on("*").to(stepB())
                .from(stepA()).on("FAILED").to(stepC())
                .end()
                .build();
}

Upvotes: 2

Related Questions