From 2d3d617da6469434b44119efce3007a78bf02f2f Mon Sep 17 00:00:00 2001 From: Nikita Nagar Date: Fri, 20 Mar 2026 18:22:36 +0530 Subject: [PATCH] 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. Signed-off-by: Nikita Nagar --- .../launch/support/SimpleJobOperator.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java index a006243497..dbf589e250 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java @@ -172,8 +172,10 @@ public Long start(String jobName, Properties parameters) /** * Start a new instance of a job with the specified parameters. If the job defines a - * {@link JobParametersIncrementer}, then the incrementer will be used to calculate - * the next parameters in the sequence and the provided parameters will be ignored. + * {@link JobParametersIncrementer} and there is no restartable job instance for the + * provided parameters, then the incrementer will be used to calculate the next + * parameters in the sequence. Otherwise, the job will be launched (or restarted) with + * the provided parameters. * @param job the {@link Job} to start * @param jobParameters the {@link JobParameters} to start the job with * @return the {@link JobExecution} that was started @@ -191,12 +193,21 @@ public JobExecution start(Job job, JobParameters jobParameters) throws JobInstan Assert.notNull(job, "The Job must not be null."); Assert.notNull(jobParameters, "The JobParameters must not be null."); if (job.getJobParametersIncrementer() != null) { - if (!jobParameters.isEmpty() && logger.isWarnEnabled()) { - logger.warn(String.format( - "Attempting to launch job: [%s] which defines an incrementer with additional parameters: [%s]. Additional parameters will be ignored.", - job.getName(), jobParameters)); + // First, try to run the job with the provided parameters. + // This will restart failed/stopped job instances if they exist. + try { + return run(job, jobParameters); + } + catch (JobInstanceAlreadyCompleteException e) { + // Job instance with these parameters is already complete. + // Use the incrementer to get the next parameters for a new instance. + if (!jobParameters.isEmpty() && logger.isWarnEnabled()) { + logger.warn(String.format( + "Job instance for [%s] with parameters [%s] is already complete. Using incrementer to get next parameters.", + job.getName(), jobParameters)); + } + return startNextInstance(job); } - return startNextInstance(job); } return run(job, jobParameters); }