Run a Spring Batch job several times

Asked

Viewed 547 times

3

By default, Spring Batch does not allow you to execute a job that already has a complete execution with the same parameters. If you try to run, get the message:

"A job instance already exists and is complete for Parameters={Baz=Quix, foo=bar}. If you want to run this job Again, change the Parameters."

The problem is that I need to run the same batch several times a day, thinking about it, I used the incrementer quoted in the official documentation:

@Configuration
public class BatchConfiguration {
  @Autowired
  private JobBuilderFactory jobBuilderFactory;
  @Autowired
  private StepBuilderFactory stepBuilderFactory;

  @Bean
  public Job job() {
    return this.jobBuilderFactory
      .get("jobRestService1").incrementer(
        new RunIdIncrementer()
      )
      .start(step1()).build();
  }

  @Bean
  public Step step1() {
    return this.stepBuilderFactory
      .get("step1").tasklet((stepContribution, chunkContext) -> {
        System.out.println("Step1 ran today!");
        return RepeatStatus.FINISHED;
    }).build();
  }
}

What it does is generate a parameter run.id automatically. It is as if you are passing different parameters to allow re-export. If I run the job directly, it works, it actually generates the run.id and allows re-experience. The problem is when I try to run it with a Joblauncher:

@RestController
public class JobLaunchingController {
  @Autowired
  private JobLauncher jobLauncher;
  @Autowired
  private ApplicationContext context;

  @PostMapping(path = "/run")
  public ExitStatus runJob(@RequestBody JobLaunchRequest request) throws Exception {
    Job job = this.context.getBean(request.getName(), Job.class);
    return this.jobLauncher.run(job, request.getJobParameters()).getExitStatus();
  }
}

He ignores the incrementer and uses only the parameters I passed to the run.

How do I get him to summon the incrementer when I run the job with Joblauncher? I don’t want to have to create the parameter run id. at hand, since I set up the job to create it for me through the incrementer.

1 answer

1


To invoke the incrementer, it is only necessary to call the getNextJobParameters. For this, it is also necessary to inject the jobExplorer. Would look like this:

@RestController
public class JobLaunchingController {
  @Autowired
  private JobLauncher jobLauncher;
  @Autowired
  private JobExplorer jobExplorer;
  @Autowired
  private ApplicationContext context;

  @PostMapping(path = "/run")
  public ExitStatus runJob(@RequestBody JobLaunchRequest request) throws Exception {
    Job job = this.context.getBean(request.getName(), Job.class);

    JobParameters jobParameters = new JobParametersBuilder(request.getJobParameters(), this.jobExplorer).getNextJobParameters(job)
        .toJobParameters();

    return this.jobLauncher.run(job, jobParameters).getExitStatus();
  }
}

The parameters are constructed by JobParameterBuilder according to what was passed in the request and in the jobExplorer, which will generate the increment parameter configured in the job by invoking the getNextJobParameters.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.