1

I have a job which accepts parameters x=y and I have scheduled to run every 10 seconds. Here's how I start it:

final JobExecution previousExecution = jobRepository.getLastJobExecution(jobId, jobParameters);
if (previousExecution != null && previousExecution.getStartTime() != null) {
    return jobOperator.startNextInstance(jobId);
} else {
    return jobOperator.start(jobId, PropertiesConverter.propertiesToString(jobParametersConverter.getProperties(jobParameters)));
}

The first time I start the job, it goes in the else clause and it starts successfully. After 10 seconds it goes inside the if clause and it starts successfully too. Then I manually run the job (via REST API) but this time with parameters x=z and it runs it successfully too. Then 10 seconds pass and the job is about to be start again. Now jobRepository.getLastJobExecution returns the correct x=y execution, but guess what - jobOperator.startNextInstance does not care about your parameters - it only accepts jobId. And inside it loads the wrong x=z instance and starts running the job with x=z from now on until forever every 10 seconds.

My question is - why is the startNextInstance not accept the jobParameters? I want to start a nextInstance for given job parameters, why is it not allowed?

Petar Tahchiev
  • 4,336
  • 4
  • 35
  • 48

1 Answers1

1

JobOperator, per the documentation is a low level interface and really isn't the ideal way to launch Spring Batch Jobs. The JobLauncher is really the right way to launch jobs in Spring Batch and does accept parameters.

Michael Minella
  • 20,843
  • 4
  • 55
  • 67
  • Hmmm.. `JobOperator` internally calls `JobLauncher` - perhaps that's why I thought the `JobLauncher` is the low-level one. – Petar Tahchiev Jun 02 '17 at 21:18
  • I don't have the full history on why the `JobOperator` was created, but most Spring components that launch jobs use the `JobLauncher`. For example, in Spring Boot, jobs are launched via the `JobLauncherCommandLineRunner`. – Michael Minella Jun 02 '17 at 21:20