老问题,但仍适用于当前版本(3.0.5):
如果您通过以下方式开始作业执行
JobExecution jobExecution = launcher.run(job, jobParameters);
使用例如SimpleJobLauncher
类,那么增量器永远不会被调用。如果检查方法的“调用者”,Incrementer.getNext(JobParameters)
调用者的数量是有限的:
- org.springframework.batch.core.launch.support.CommandLineJobRunner
CommandLineJobRunner在调用启动器之前getNext()
有条件地调用“-next” :
if (opts.contains("-next")) {
JobParameters nextParameters = getNextJobParameters(job);
Map<String, JobParameter> map = new HashMap<String, JobParameter>(nextParameters.getParameters());
map.putAll(jobParameters.getParameters());
jobParameters = new JobParameters(map);
}
JobExecution jobExecution = launcher.run(job, jobParameters);
- org.springframework.batch.core.launch.support.SimpleJobOperator
这由 Spring Admin Web 应用程序使用,它与 CommandLineJobRunner 中的实现基本相同:
if (lastInstances.isEmpty()) {
parameters = incrementer.getNext(new JobParameters());
if (parameters == null) {
throw new JobParametersNotFoundException("No bootstrap parameters found for job=" + jobName);
}
}
else {
List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
parameters = incrementer.getNext(lastExecutions.get(0).getJobParameters());
}
logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
try {
return jobLauncher.run(job, parameters).getId();
}
catch (JobExecutionAlreadyRunningException e) {
throw new UnexpectedJobExecutionException(String.format(ILLEGAL_STATE_MSG, "job already running", jobName,
parameters), e);
}
因此,如果您使用 JobLauncher 类来启动 Jobs,在调用 jobLauncher 之前必须注意调用增量器,以使用所需的值增强作业参数。