Skip to content

Commit c00ac51

Browse files
committed
TaskJobLauncherCommandLineRunner now inherits from JobLauncherCommandLineRunner
Used to be a copy of. No just inherits. Added feature check for completed/failed jobs before throwing exception Currently in Task if the user sets failOnJobFailure and they are using a TaskExecutor it will not throw an exception when task fails. This because at evaluation time the job has not started. With the latest change Task waits for all jobs to complete before deciding whether an exception needs to be thrown or not.
1 parent 8f9c8b6 commit c00ac51

File tree

7 files changed

+325
-156
lines changed

7 files changed

+325
-156
lines changed

spring-cloud-task-batch/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,12 @@
7979
<groupId>org.springframework.boot</groupId>
8080
<artifactId>spring-boot-test</artifactId>
8181
</dependency>
82+
<dependency>
83+
<groupId>org.junit.jupiter</groupId>
84+
<artifactId>junit-jupiter-api</artifactId>
85+
<version>${junit.version}</version>
86+
<scope>test</scope>
87+
</dependency>
88+
8289
</dependencies>
8390
</project>

spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/configuration/TaskBatchProperties.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ public class TaskBatchProperties {
4343
*/
4444
private int commandLineRunnerOrder = 0;
4545

46+
/**
47+
* Maximum wait time that Spring Cloud task will wait for tasks to complete
48+
* when spring.cloud.task.batch.failOnJobFailure is set to true. Defaults
49+
* to 0. 0 indicates no wait time is enforced.
50+
*/
51+
private long failOnJobFailurewaitTimeInMillis = 0;
52+
53+
/**
54+
* Fixed delay that Spring Cloud Task will wait when checking if
55+
* {@link org.springframework.batch.core.JobExecution}s have completed,
56+
* when spring.cloud.task.batch.failOnJobFailure is set to true. Defaults
57+
* to 5000.
58+
*/
59+
private long failOnJobFailurePollIntervalInMillis = 5000l;
60+
4661
public String getJobNames() {
4762
return this.jobNames;
4863
}
@@ -58,4 +73,20 @@ public int getCommandLineRunnerOrder() {
5873
public void setCommandLineRunnerOrder(int commandLineRunnerOrder) {
5974
this.commandLineRunnerOrder = commandLineRunnerOrder;
6075
}
76+
77+
public long getFailOnJobFailurewaitTimeInMillis() {
78+
return failOnJobFailurewaitTimeInMillis;
79+
}
80+
81+
public void setFailOnJobFailurewaitTimeInMillis(long failOnJobFailurewaitTimeInMillis) {
82+
this.failOnJobFailurewaitTimeInMillis = failOnJobFailurewaitTimeInMillis;
83+
}
84+
85+
public long getFailOnJobFailurePollIntervalInMillis() {
86+
return failOnJobFailurePollIntervalInMillis;
87+
}
88+
89+
public void setFailOnJobFailurePollIntervalInMillis(long failOnJobFailurePollIntervalInMillis) {
90+
this.failOnJobFailurePollIntervalInMillis = failOnJobFailurePollIntervalInMillis;
91+
}
6192
}

spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/configuration/TaskJobLauncherAutoConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.springframework.batch.core.explore.JobExplorer;
2424
import org.springframework.batch.core.launch.JobLauncher;
2525
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
27+
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
2628
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2729
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2830
import org.springframework.context.annotation.Bean;
@@ -37,6 +39,7 @@
3739
@Configuration
3840
@ConditionalOnProperty(name = "spring.cloud.task.batch.failOnJobFailure", havingValue = "true", matchIfMissing = false)
3941
@EnableConfigurationProperties(TaskBatchProperties.class)
42+
@AutoConfigureBefore(BatchAutoConfiguration.class)
4043
public class TaskJobLauncherAutoConfiguration {
4144

4245
@Autowired
@@ -49,11 +52,9 @@ public TaskJobLauncherCommandLineRunnerFactoryBean jobLauncherCommandLineRunner(
4952
new TaskJobLauncherCommandLineRunnerFactoryBean(jobLauncher,
5053
jobExplorer,
5154
jobs,
52-
this.properties.getJobNames(),
55+
this.properties,
5356
jobRegistry);
5457

55-
taskJobLauncherCommandLineRunnerFactoryBean.setOrder(this.properties.getCommandLineRunnerOrder());
56-
5758
return taskJobLauncherCommandLineRunnerFactoryBean;
5859
}
5960
}

spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/configuration/TaskJobLauncherCommandLineRunnerFactoryBean.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ public class TaskJobLauncherCommandLineRunnerFactoryBean implements FactoryBean<
4646

4747
private Integer order = 0;
4848

49+
/**
50+
* Maximum wait time that Spring Cloud task will wait for tasks to complete
51+
* when spring.cloud.task.batch.failOnJobFailure is set to true. Defaults
52+
* to 0. 0 indicates no wait time is enforced.
53+
*/
54+
private long failOnJobFailurewaitTimeInMillis = 0;
55+
56+
/**
57+
* Fixed delay that Spring Cloud Task will wait when checking if
58+
* {@link org.springframework.batch.core.JobExecution}s have completed,
59+
* when spring.cloud.task.batch.failOnJobFailure is set to true. Defaults
60+
* to 5000.
61+
*/
62+
private long failOnJobFailurePollIntervalInMillis = 5000l;
63+
4964
public TaskJobLauncherCommandLineRunnerFactoryBean(JobLauncher jobLauncher,
5065
JobExplorer jobExplorer, List<Job> jobs, String jobNames,
5166
JobRegistry jobRegistry) {
@@ -57,6 +72,21 @@ public TaskJobLauncherCommandLineRunnerFactoryBean(JobLauncher jobLauncher,
5772
this.jobRegistry = jobRegistry;
5873
}
5974

75+
public TaskJobLauncherCommandLineRunnerFactoryBean(JobLauncher jobLauncher,
76+
JobExplorer jobExplorer, List<Job> jobs, TaskBatchProperties properties,
77+
JobRegistry jobRegistry) {
78+
Assert.notNull(properties, "properties must not be null");
79+
this.jobLauncher = jobLauncher;
80+
this.jobExplorer = jobExplorer;
81+
Assert.notEmpty(jobs, "jobs must not be null nor empty");
82+
this.jobs = jobs;
83+
this.jobNames = properties.getJobNames();
84+
this.jobRegistry = jobRegistry;
85+
this.failOnJobFailurePollIntervalInMillis = properties.getFailOnJobFailurePollIntervalInMillis();
86+
this.failOnJobFailurewaitTimeInMillis = properties.getFailOnJobFailurewaitTimeInMillis();
87+
this.order = properties.getCommandLineRunnerOrder();
88+
}
89+
6090
public void setOrder(int order) {
6191
this.order = order;
6292
}
@@ -74,12 +104,29 @@ public TaskJobLauncherCommandLineRunner getObject() throws Exception {
74104
if(this.order != null) {
75105
taskJobLauncherCommandLineRunner.setOrder(this.order);
76106
}
77-
107+
taskJobLauncherCommandLineRunner.setFailOnJobFailurePollIntervalInMillis(this.failOnJobFailurePollIntervalInMillis);
108+
taskJobLauncherCommandLineRunner.setFailOnJobFailurewaitTimeInMillis(this.failOnJobFailurewaitTimeInMillis);
78109
return taskJobLauncherCommandLineRunner;
79110
}
80111

81112
@Override
82113
public Class<?> getObjectType() {
83114
return TaskJobLauncherCommandLineRunner.class;
84115
}
116+
117+
public long getFailOnJobFailurewaitTimeInMillis() {
118+
return failOnJobFailurewaitTimeInMillis;
119+
}
120+
121+
public void setFailOnJobFailurewaitTimeInMillis(long failOnJobFailurewaitTimeInMillis) {
122+
this.failOnJobFailurewaitTimeInMillis = failOnJobFailurewaitTimeInMillis;
123+
}
124+
125+
public long getFailOnJobFailurePollIntervalInMillis() {
126+
return failOnJobFailurePollIntervalInMillis;
127+
}
128+
129+
public void setFailOnJobFailurePollIntervalInMillis(long failOnJobFailurePollIntervalInMillis) {
130+
this.failOnJobFailurePollIntervalInMillis = failOnJobFailurePollIntervalInMillis;
131+
}
85132
}

0 commit comments

Comments
 (0)