Skip to content

Commit eb42128

Browse files
committed
Fix parameters handling when using an incrementer
This commit changes the way job parameters are handled when the job defines an incrementer. Before this commit, additional parameters were overriding the ones of the previous execution. This commit changes that by ignoring additional parameters and warning the user about this incorrect usage of the framework. Resolves #4910
1 parent 9209fb4 commit eb42128

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ default Long start(String jobName, Properties parameters)
7373
}
7474

7575
/**
76-
* Start a new instance of a job with the specified parameters.
76+
* Start a new instance of a job with the specified parameters. If the job defines a
77+
* {@link JobParametersIncrementer}, then the incrementer will be used to calculate
78+
* the next parameters in the sequence and the provided parameters will be ignored.
7779
* @param job the {@link Job} to start
7880
* @param jobParameters the {@link JobParameters} to start the job with
7981
* @return the {@link JobExecution} that was started
@@ -90,7 +92,7 @@ default Long start(String jobName, Properties parameters)
9092
default JobExecution start(Job job, JobParameters jobParameters)
9193
throws NoSuchJobException, JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException,
9294
JobRestartException, JobParametersInvalidException {
93-
return run(job, jobParameters);
95+
throw new UnsupportedOperationException();
9496
}
9597

9698
/**
@@ -179,20 +181,15 @@ Long startNextInstance(String jobName) throws NoSuchJobException, JobParametersN
179181
* method (or a similar one) at the same time.
180182
* @param job the job to launch
181183
* @return the {@link JobExecution} created when the job is launched
182-
* @throws NoSuchJobException if there is no such job definition available
183-
* @throws JobParametersNotFoundException if the parameters cannot be found
184-
* @throws JobParametersInvalidException thrown if some of the job parameters are
185-
* invalid.
186184
* @throws UnexpectedJobExecutionException if an unexpected condition arises
187185
* @throws JobRestartException thrown if a job is restarted illegally.
188186
* @throws JobExecutionAlreadyRunningException thrown if attempting to restart a job
189187
* that is already executing.
190188
* @throws JobInstanceAlreadyCompleteException thrown if attempting to restart a
191189
* completed job.
192190
*/
193-
JobExecution startNextInstance(Job job) throws NoSuchJobException, JobParametersNotFoundException,
194-
JobRestartException, JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException,
195-
UnexpectedJobExecutionException, JobParametersInvalidException;
191+
JobExecution startNextInstance(Job job) throws JobRestartException, JobExecutionAlreadyRunningException,
192+
JobInstanceAlreadyCompleteException, UnexpectedJobExecutionException;
196193

197194
/**
198195
* Send a stop signal to the {@link JobExecution} with the supplied id. The signal is

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,39 @@ public Long start(String jobName, Properties parameters)
165165

166166
}
167167

168+
/**
169+
* Start a new instance of a job with the specified parameters. If the job defines a
170+
* {@link JobParametersIncrementer}, then the incrementer will be used to calculate
171+
* the next parameters in the sequence and the provided parameters will be ignored.
172+
* @param job the {@link Job} to start
173+
* @param jobParameters the {@link JobParameters} to start the job with
174+
* @return the {@link JobExecution} that was started
175+
* @throws NoSuchJobException if the given {@link Job} is not registered
176+
* @throws JobParametersInvalidException thrown if any of the job parameters are
177+
* @throws JobExecutionAlreadyRunningException if the JobInstance identified by the
178+
* properties already has an execution running. invalid.
179+
* @throws JobRestartException if the execution would be a re-start, but a re-start is
180+
* either not allowed or not needed.
181+
* @throws JobInstanceAlreadyCompleteException if the job has been run before with the
182+
* same parameters and completed successfully
183+
* @throws IllegalArgumentException if the job or job parameters are null.
184+
*/
185+
public JobExecution start(Job job, JobParameters jobParameters)
186+
throws NoSuchJobException, JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException,
187+
JobRestartException, JobParametersInvalidException {
188+
Assert.notNull(job, "The Job must not be null.");
189+
Assert.notNull(jobParameters, "The JobParameters must not be null.");
190+
if (job.getJobParametersIncrementer() != null) {
191+
if (logger.isWarnEnabled()) {
192+
logger.warn(String.format(
193+
"Attempting to launch the job %s which defines an incrementer with additional parameters={%s}. Those parameters will be ignored.",
194+
job.getName(), jobParameters));
195+
}
196+
startNextInstance(job);
197+
}
198+
return run(job, jobParameters);
199+
}
200+
168201
@SuppressWarnings("removal")
169202
@Override
170203
@Deprecated(since = "6.0", forRemoval = true)
@@ -228,8 +261,7 @@ public Long startNextInstance(String jobName)
228261
}
229262

230263
@Override
231-
public JobExecution startNextInstance(Job job)
232-
throws NoSuchJobException, UnexpectedJobExecutionException, JobParametersInvalidException {
264+
public JobExecution startNextInstance(Job job) throws UnexpectedJobExecutionException {
233265
Assert.notNull(job, "Job must not be null");
234266
Assert.notNull(job.getJobParametersIncrementer(),
235267
"No job parameters incrementer found for job=" + job.getName());
@@ -271,6 +303,9 @@ public JobExecution startNextInstance(Job job)
271303
String.format(ILLEGAL_STATE_MSG, "job instance already complete", job.getName(), nextParameters),
272304
e);
273305
}
306+
catch (JobParametersInvalidException e) {
307+
throw new UnexpectedJobExecutionException("Invalid job parameters " + nextParameters, e);
308+
}
274309

275310
}
276311

spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/TaskExecutorJobOperatorTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ void testStartNewInstanceAlreadyExists() {
182182
assertThrows(JobInstanceAlreadyExistsException.class, () -> jobOperator.start("foo", properties));
183183
}
184184

185+
@Test
186+
void testStartWithIncrementer() throws Exception {
187+
jobOperator.start(job, new JobParameters());
188+
verify(jobRepository).getLastJobInstance("foo");
189+
}
190+
185191
@Test
186192
void testResumeSunnyDay() throws Exception {
187193
jobParameters = new JobParameters();

0 commit comments

Comments
 (0)