Home > Software design >  Spring batch continue job when a step fails
Spring batch continue job when a step fails

Time:11-17

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

CodePudding user response:

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();
}

CodePudding user response:

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();
  • Related