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?
JobOperator, per the documentation is a low level interface and really isn't the ideal way to launch Spring Batch Jobs. TheJobLauncheris really the right way to launch jobs in Spring Batch and does accept parameters.