Skip to content

Commit 76e723e

Browse files
committed
Decouple job execution creation from its execution in JobOperator
This commit introduces a new extension point to allow users to customize the way job executions are launched in the job operator. A typical use-case would be to delay the execution of a job until some condition becomes true (from the original feature request described in #3637) Credits to @hosuaby for the idea in #3637
1 parent e6c0321 commit 76e723e

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,16 @@ public class TaskExecutorJobLauncher implements JobLauncher, InitializingBean {
102102
public JobExecution run(final Job job, final JobParameters jobParameters)
103103
throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
104104
JobParametersInvalidException {
105-
106105
Assert.notNull(job, "The Job must not be null.");
107106
Assert.notNull(jobParameters, "The JobParameters must not be null.");
107+
JobExecution jobExecution = createJobExecution(job, jobParameters);
108+
launchJobExecution(job, jobExecution);
109+
return jobExecution;
110+
}
108111

112+
private JobExecution createJobExecution(Job job, JobParameters jobParameters)
113+
throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
114+
JobParametersInvalidException {
109115
JobInstance jobInstance = jobRepository.getJobInstance(job.getName(), jobParameters);
110116
ExecutionContext executionContext;
111117
if (jobInstance == null) { // fresh start
@@ -189,8 +195,17 @@ else if (status == BatchStatus.UNKNOWN) {
189195
* execution for this instance between the last assertion and the next method
190196
* returning successfully.
191197
*/
192-
final JobExecution jobExecution = jobRepository.createJobExecution(jobInstance, jobParameters,
193-
executionContext);
198+
return jobRepository.createJobExecution(jobInstance, jobParameters, executionContext);
199+
}
200+
201+
/**
202+
* Launch the job execution using the task executor.
203+
* @param job the job to be executed.
204+
* @param jobExecution the job execution to be used for this run.
205+
* @since 6.0
206+
*/
207+
protected void launchJobExecution(Job job, JobExecution jobExecution) {
208+
JobParameters jobParameters = jobExecution.getJobParameters();
194209
try {
195210
taskExecutor.execute(new Runnable() {
196211

@@ -237,10 +252,11 @@ else if (t instanceof Error error) {
237252
if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) {
238253
jobExecution.setExitStatus(ExitStatus.FAILED.addExitDescription(e));
239254
}
240-
jobRepository.update(jobExecution); // FIXME should be in finally block
241-
}
242255

243-
return jobExecution;
256+
}
257+
finally {
258+
this.jobRepository.update(jobExecution);
259+
}
244260
}
245261

246262
/**

0 commit comments

Comments
 (0)