String jobIdentifier = job.getName();
JobParameters jobParameters = new JobParameters();
List<JobInstance> lastInstances = this.jobExplorer.getJobInstances(jobIdentifier,
0, 1);
JobParametersIncrementer incrementer = job.getJobParametersIncrementer();
Map<String, JobParameter> additionals = additionalParameters.getParameters();
if (lastInstances.isEmpty()) {
// Start from a completely clean sheet
if (incrementer != null) {
jobParameters = incrementer.getNext(new JobParameters());
}
}
else {
List<JobExecution> lastExecutions = this.jobExplorer
.getJobExecutions(lastInstances.get(0));
JobExecution previousExecution = lastExecutions.get(0);
if (previousExecution == null) {
// Normally this will not happen - an instance exists with no executions
if (incrementer != null) {
jobParameters = incrementer.getNext(new JobParameters());
}
}
else if (previousExecution.getStatus() == BatchStatus.STOPPED
|| previousExecution.getStatus() == BatchStatus.FAILED) {
// Retry a failed or stopped execution
jobParameters = previousExecution.getJobParameters();
for (Entry<String, JobParameter> parameter : new HashMap<String, JobParameter>(
additionals).entrySet()) {
// Non-identifying additional parameters can be added to a retry
if (!parameter.getValue().isIdentifying()) {
additionals.remove(parameter.getKey());
}
}
}
else if (incrementer != null) {
// New instance so increment the parameters if we can
if (incrementer != null) {
jobParameters = incrementer.getNext(previousExecution
.getJobParameters());
}
}
}