Skip to content

Commit 9b9099d

Browse files
nikitanagar08claude
andcommitted
Fix #5340: Allow restart of jobs with JobParameterIncrementer from command line
This commit addresses the issue where jobs using JobParameterIncrementer could not be restarted from the command line. Previously, the start() method would always use the incrementer to create a new instance, even when there was a failed/stopped job instance that could be restarted. Changes: - Modified SimpleJobOperator.start() to first try running the job with provided parameters (which restarts failed/stopped instances) - Only use the incrementer when JobInstanceAlreadyCompleteException is thrown (meaning the job instance is already complete) This allows jobs with JobParameterIncrementer to be restarted from the command line when they fail or stop, while still creating new instances when the previous instance is complete. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d8632a3 commit 9b9099d

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,10 @@ public Long start(String jobName, Properties parameters)
172172

173173
/**
174174
* Start a new instance of a job with the specified parameters. If the job defines a
175-
* {@link JobParametersIncrementer}, then the incrementer will be used to calculate
176-
* the next parameters in the sequence and the provided parameters will be ignored.
175+
* {@link JobParametersIncrementer} and there is no restartable job instance for the
176+
* provided parameters, then the incrementer will be used to calculate the next
177+
* parameters in the sequence. Otherwise, the job will be launched (or restarted) with
178+
* the provided parameters.
177179
* @param job the {@link Job} to start
178180
* @param jobParameters the {@link JobParameters} to start the job with
179181
* @return the {@link JobExecution} that was started
@@ -191,12 +193,21 @@ public JobExecution start(Job job, JobParameters jobParameters) throws JobInstan
191193
Assert.notNull(job, "The Job must not be null.");
192194
Assert.notNull(jobParameters, "The JobParameters must not be null.");
193195
if (job.getJobParametersIncrementer() != null) {
194-
if (!jobParameters.isEmpty() && logger.isWarnEnabled()) {
195-
logger.warn(String.format(
196-
"Attempting to launch job: [%s] which defines an incrementer with additional parameters: [%s]. Additional parameters will be ignored.",
197-
job.getName(), jobParameters));
196+
// First, try to run the job with the provided parameters.
197+
// This will restart failed/stopped job instances if they exist.
198+
try {
199+
return run(job, jobParameters);
200+
}
201+
catch (JobInstanceAlreadyCompleteException e) {
202+
// Job instance with these parameters is already complete.
203+
// Use the incrementer to get the next parameters for a new instance.
204+
if (!jobParameters.isEmpty() && logger.isWarnEnabled()) {
205+
logger.warn(String.format(
206+
"Job instance for [%s] with parameters [%s] is already complete. Using incrementer to get next parameters.",
207+
job.getName(), jobParameters));
208+
}
209+
return startNextInstance(job);
198210
}
199-
return startNextInstance(job);
200211
}
201212
return run(job, jobParameters);
202213
}

0 commit comments

Comments
 (0)