Skip to content

Commit 62b9268

Browse files
committed
Polish "Fix Spring Batch job restart parameters handling"
See gh-14933
1 parent ad3c3ad commit 62b9268

File tree

2 files changed

+64
-70
lines changed

2 files changed

+64
-70
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.Collections;
2222
import java.util.HashMap;
23+
import java.util.LinkedHashMap;
2324
import java.util.Map;
2425
import java.util.Properties;
2526

@@ -33,7 +34,6 @@
3334
import org.springframework.batch.core.JobParameter;
3435
import org.springframework.batch.core.JobParameters;
3536
import org.springframework.batch.core.JobParametersBuilder;
36-
import org.springframework.batch.core.JobParametersIncrementer;
3737
import org.springframework.batch.core.JobParametersInvalidException;
3838
import org.springframework.batch.core.configuration.JobRegistry;
3939
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
@@ -51,6 +51,7 @@
5151
import org.springframework.context.ApplicationEventPublisher;
5252
import org.springframework.context.ApplicationEventPublisherAware;
5353
import org.springframework.core.Ordered;
54+
import org.springframework.util.Assert;
5455
import org.springframework.util.PatternMatchUtils;
5556
import org.springframework.util.StringUtils;
5657

@@ -76,13 +77,13 @@ public class JobLauncherCommandLineRunner
7677

7778
private JobParametersConverter converter = new DefaultJobParametersConverter();
7879

79-
private JobLauncher jobLauncher;
80+
private final JobLauncher jobLauncher;
8081

81-
private JobRegistry jobRegistry;
82+
private final JobExplorer jobExplorer;
8283

83-
private JobExplorer jobExplorer;
84+
private final JobRepository jobRepository;
8485

85-
private JobRepository jobRepository;
86+
private JobRegistry jobRegistry;
8687

8788
private String jobNames;
8889

@@ -96,17 +97,17 @@ public class JobLauncherCommandLineRunner
9697
* Create a new {@link JobLauncherCommandLineRunner}.
9798
* @param jobLauncher to launch jobs
9899
* @param jobExplorer to check the job repository for previous executions
99-
* @deprecated This constructor is deprecated in favor of
100-
* {@link JobLauncherCommandLineRunner#JobLauncherCommandLineRunner(JobLauncher, JobExplorer, JobRepository)}.
101-
* A job repository is required to check if a job instance exists with the given
102-
* parameters when running a job (which is not possible with the job explorer). This
103-
* constructor will be removed in a future version.
100+
* @deprecated since 2.0.7 in favor of
101+
* {@link #JobLauncherCommandLineRunner(JobLauncher, JobExplorer, JobRepository)}. A
102+
* job repository is required to check if a job instance exists with the given
103+
* parameters when running a job (which is not possible with the job explorer).
104104
*/
105105
@Deprecated
106106
public JobLauncherCommandLineRunner(JobLauncher jobLauncher,
107107
JobExplorer jobExplorer) {
108108
this.jobLauncher = jobLauncher;
109109
this.jobExplorer = jobExplorer;
110+
this.jobRepository = null;
110111
}
111112

112113
/**
@@ -118,6 +119,9 @@ public JobLauncherCommandLineRunner(JobLauncher jobLauncher,
118119
*/
119120
public JobLauncherCommandLineRunner(JobLauncher jobLauncher, JobExplorer jobExplorer,
120121
JobRepository jobRepository) {
122+
Assert.notNull(jobLauncher, "JobLauncher must not be null");
123+
Assert.notNull(jobExplorer, "JobExplorer must not be null");
124+
Assert.notNull(jobRepository, "JobRepository must not be null");
121125
this.jobLauncher = jobLauncher;
122126
this.jobExplorer = jobExplorer;
123127
this.jobRepository = jobRepository;
@@ -169,6 +173,20 @@ protected void launchJobFromProperties(Properties properties)
169173
executeRegisteredJobs(jobParameters);
170174
}
171175

176+
private void executeLocalJobs(JobParameters jobParameters)
177+
throws JobExecutionException {
178+
for (Job job : this.jobs) {
179+
if (StringUtils.hasText(this.jobNames)) {
180+
String[] jobsToRun = this.jobNames.split(",");
181+
if (!PatternMatchUtils.simpleMatch(jobsToRun, job.getName())) {
182+
logger.debug("Skipped job: " + job.getName());
183+
continue;
184+
}
185+
}
186+
execute(job, jobParameters);
187+
}
188+
}
189+
172190
private void executeRegisteredJobs(JobParameters jobParameters)
173191
throws JobExecutionException {
174192
if (this.jobRegistry != null && StringUtils.hasText(this.jobNames)) {
@@ -192,76 +210,56 @@ protected void execute(Job job, JobParameters jobParameters)
192210
throws JobExecutionAlreadyRunningException, JobRestartException,
193211
JobInstanceAlreadyCompleteException, JobParametersInvalidException,
194212
JobParametersNotFoundException {
195-
String jobName = job.getName();
196-
JobParameters parameters = jobParameters;
197-
boolean jobInstanceExists = this.jobRepository.isJobInstanceExists(jobName,
198-
parameters);
199-
if (jobInstanceExists) {
200-
JobExecution lastJobExecution = this.jobRepository
201-
.getLastJobExecution(jobName, jobParameters);
202-
if (lastJobExecution != null && isStoppedOrFailed(lastJobExecution)
203-
&& job.isRestartable()) {
204-
// Retry a failed or stopped execution with previous parameters
205-
JobParameters previousParameters = lastJobExecution.getJobParameters();
206-
/*
207-
* remove Non-identifying parameters from the previous execution's
208-
* parameters since there is no way to remove them programmatically. If
209-
* they are required (or need to be modified) on a restart, they need to
210-
* be (re)specified.
211-
*/
212-
JobParameters previousIdentifyingParameters = removeNonIdentifying(
213-
previousParameters);
214-
// merge additional parameters with previous ones (overriding those with
215-
// the same key)
216-
parameters = merge(previousIdentifyingParameters, jobParameters);
217-
}
218-
}
219-
else {
220-
JobParametersIncrementer incrementer = job.getJobParametersIncrementer();
221-
if (incrementer != null) {
222-
JobParameters nextParameters = new JobParametersBuilder(jobParameters,
223-
this.jobExplorer).getNextJobParameters(job).toJobParameters();
224-
parameters = merge(nextParameters, jobParameters);
225-
}
226-
}
213+
JobParameters parameters = getNextJobParameters(job, jobParameters);
227214
JobExecution execution = this.jobLauncher.run(job, parameters);
228215
if (this.publisher != null) {
229216
this.publisher.publishEvent(new JobExecutionEvent(execution));
230217
}
231218
}
232219

233-
private void executeLocalJobs(JobParameters jobParameters)
234-
throws JobExecutionException {
235-
for (Job job : this.jobs) {
236-
if (StringUtils.hasText(this.jobNames)) {
237-
String[] jobsToRun = this.jobNames.split(",");
238-
if (!PatternMatchUtils.simpleMatch(jobsToRun, job.getName())) {
239-
logger.debug("Skipped job: " + job.getName());
240-
continue;
241-
}
242-
}
243-
execute(job, jobParameters);
220+
private JobParameters getNextJobParameters(Job job, JobParameters jobParameters) {
221+
if (this.jobRepository != null
222+
&& this.jobRepository.isJobInstanceExists(job.getName(), jobParameters)) {
223+
return getNextJobParametersForExisting(job, jobParameters);
224+
}
225+
if (job.getJobParametersIncrementer() == null) {
226+
return jobParameters;
244227
}
228+
JobParameters nextParameters = new JobParametersBuilder(jobParameters,
229+
this.jobExplorer).getNextJobParameters(job).toJobParameters();
230+
return merge(nextParameters, jobParameters);
245231
}
246232

247-
private JobParameters removeNonIdentifying(JobParameters parameters) {
248-
Map<String, JobParameter> parameterMap = parameters.getParameters();
249-
HashMap<String, JobParameter> copy = new HashMap<>(parameterMap);
250-
for (Map.Entry<String, JobParameter> parameter : copy.entrySet()) {
251-
if (!parameter.getValue().isIdentifying()) {
252-
parameterMap.remove(parameter.getKey());
253-
}
233+
private JobParameters getNextJobParametersForExisting(Job job,
234+
JobParameters jobParameters) {
235+
JobExecution lastExecution = this.jobRepository.getLastJobExecution(job.getName(),
236+
jobParameters);
237+
if (isStoppedOrFailed(lastExecution) && job.isRestartable()) {
238+
JobParameters previousIdentifyingParameters = getGetIdentifying(
239+
lastExecution.getJobParameters());
240+
return merge(previousIdentifyingParameters, jobParameters);
254241
}
255-
return new JobParameters(parameterMap);
242+
return jobParameters;
256243
}
257244

258245
private boolean isStoppedOrFailed(JobExecution execution) {
259-
BatchStatus status = execution.getStatus();
246+
BatchStatus status = (execution != null) ? execution.getStatus() : null;
260247
return (status == BatchStatus.STOPPED || status == BatchStatus.FAILED);
261248
}
262249

250+
private JobParameters getGetIdentifying(JobParameters parameters) {
251+
HashMap<String, JobParameter> nonIdentifying = new LinkedHashMap<>(
252+
parameters.getParameters().size());
253+
parameters.getParameters().forEach((key, value) -> {
254+
if (value.isIdentifying()) {
255+
nonIdentifying.put(key, value);
256+
}
257+
});
258+
return new JobParameters(nonIdentifying);
259+
}
260+
263261
private JobParameters merge(JobParameters parameters, JobParameters additionals) {
264-
Map<String, JobParameter> merged = new HashMap<>();
262+
Map<String, JobParameter> merged = new LinkedHashMap<>();
265263
merged.putAll(parameters.getParameters());
266264
merged.putAll(additionals.getParameters());
267265
return new JobParameters(merged);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunnerTests.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.transaction.PlatformTransactionManager;
4747

4848
import static org.assertj.core.api.Assertions.assertThat;
49+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4950
import static org.assertj.core.api.Assertions.fail;
5051

5152
/**
@@ -150,17 +151,12 @@ public void retryFailedExecutionOnNonRestartableJob() throws Exception {
150151
// A failed job that is not restartable does not re-use the job params of
151152
// the last execution, but creates a new job instance when running it again.
152153
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(2);
153-
try {
154+
assertThatExceptionOfType(JobRestartException.class).isThrownBy(() -> {
154155
// try to re-run a failed execution
155156
this.runner.execute(this.job,
156157
new JobParametersBuilder().addLong("run.id", 1L).toJobParameters());
157158
fail("expected JobRestartException");
158-
}
159-
catch (JobRestartException ex) {
160-
assertThat(ex.getMessage())
161-
.isEqualTo("JobInstance already exists and is not restartable");
162-
// expected
163-
}
159+
}).withMessageContaining("JobInstance already exists and is not restartable");
164160
}
165161

166162
@Test

0 commit comments

Comments
 (0)