Skip to content

Commit 2cba0f2

Browse files
committed
Tasks supports JobLauncherApplicationRunner
Will support JobLauncherCommandLineRunner if running task on Boot 2.2.0 resolves #645 Updated based on code review and rebased Updated code based on code review
1 parent 13b6e2d commit 2cba0f2

File tree

6 files changed

+377
-13
lines changed

6 files changed

+377
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2020-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.task.batch.configuration;
18+
19+
import java.util.List;
20+
21+
import org.springframework.batch.core.Job;
22+
import org.springframework.batch.core.configuration.JobRegistry;
23+
import org.springframework.batch.core.explore.JobExplorer;
24+
import org.springframework.batch.core.launch.JobLauncher;
25+
import org.springframework.batch.core.repository.JobRepository;
26+
import org.springframework.beans.factory.FactoryBean;
27+
import org.springframework.boot.autoconfigure.batch.BatchProperties;
28+
import org.springframework.cloud.task.batch.handler.TaskJobLauncherApplicationRunner;
29+
import org.springframework.util.Assert;
30+
import org.springframework.util.StringUtils;
31+
32+
/**
33+
* Factory bean for creating an instance of {@link TaskJobLauncherApplicationRunner}.
34+
*
35+
* @author Glenn Renfro
36+
* @since 2.3.0
37+
*/
38+
public class TaskJobLauncherApplicationRunnerFactoryBean
39+
implements FactoryBean<TaskJobLauncherApplicationRunner> {
40+
41+
private JobLauncher jobLauncher;
42+
43+
private JobExplorer jobExplorer;
44+
45+
private List<Job> jobs;
46+
47+
private String jobNames;
48+
49+
private JobRegistry jobRegistry;
50+
51+
private Integer order = 0;
52+
53+
private TaskBatchProperties taskBatchProperties;
54+
55+
private JobRepository jobRepository;
56+
57+
public TaskJobLauncherApplicationRunnerFactoryBean(JobLauncher jobLauncher,
58+
JobExplorer jobExplorer, List<Job> jobs,
59+
TaskBatchProperties taskBatchProperties, JobRegistry jobRegistry,
60+
JobRepository jobRepository, BatchProperties batchProperties) {
61+
Assert.notNull(taskBatchProperties, "taskBatchProperties must not be null");
62+
Assert.notNull(batchProperties, "batchProperties must not be null");
63+
Assert.notEmpty(jobs, "jobs must not be null nor empty");
64+
65+
this.jobLauncher = jobLauncher;
66+
this.jobExplorer = jobExplorer;
67+
this.jobs = jobs;
68+
this.jobNames = taskBatchProperties.getJobNames();
69+
this.jobRegistry = jobRegistry;
70+
this.taskBatchProperties = taskBatchProperties;
71+
if (StringUtils.hasText(batchProperties.getJob().getNames())) {
72+
this.jobNames = batchProperties.getJob().getNames();
73+
}
74+
else {
75+
this.jobNames = taskBatchProperties.getJobNames();
76+
}
77+
this.order = taskBatchProperties.getCommandLineRunnerOrder();
78+
this.jobRepository = jobRepository;
79+
}
80+
81+
public void setOrder(int order) {
82+
this.order = order;
83+
}
84+
85+
@Override
86+
public TaskJobLauncherApplicationRunner getObject() {
87+
TaskJobLauncherApplicationRunner taskJobLauncherApplicationRunner = new TaskJobLauncherApplicationRunner(
88+
this.jobLauncher, this.jobExplorer, this.jobRepository,
89+
this.taskBatchProperties);
90+
taskJobLauncherApplicationRunner.setJobs(this.jobs);
91+
if (StringUtils.hasText(this.jobNames)) {
92+
taskJobLauncherApplicationRunner.setJobNames(this.jobNames);
93+
}
94+
taskJobLauncherApplicationRunner.setJobRegistry(this.jobRegistry);
95+
96+
if (this.order != null) {
97+
taskJobLauncherApplicationRunner.setOrder(this.order);
98+
}
99+
return taskJobLauncherApplicationRunner;
100+
}
101+
102+
@Override
103+
public Class<?> getObjectType() {
104+
return TaskJobLauncherApplicationRunner.class;
105+
}
106+
107+
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 the original author or authors.
2+
* Copyright 2018-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,8 @@
2727
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2828
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.batch.BatchProperties;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
3032
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3133
import org.springframework.context.annotation.Bean;
3234
import org.springframework.context.annotation.Conditional;
@@ -48,6 +50,7 @@ public class TaskJobLauncherAutoConfiguration {
4850
private TaskBatchProperties properties;
4951

5052
@Bean
53+
@ConditionalOnMissingClass("org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner")
5154
public TaskJobLauncherCommandLineRunnerFactoryBean jobLauncherCommandLineRunner(
5255
JobLauncher jobLauncher, JobExplorer jobExplorer, List<Job> jobs,
5356
JobRegistry jobRegistry, JobRepository jobRepository,
@@ -60,4 +63,19 @@ public TaskJobLauncherCommandLineRunnerFactoryBean jobLauncherCommandLineRunner(
6063
return taskJobLauncherCommandLineRunnerFactoryBean;
6164
}
6265

66+
@Bean
67+
@ConditionalOnClass(
68+
name = "org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner")
69+
public TaskJobLauncherApplicationRunnerFactoryBean taskJobLauncherApplicationRunner(
70+
JobLauncher jobLauncher, JobExplorer jobExplorer, List<Job> jobs,
71+
JobRegistry jobRegistry, JobRepository jobRepository,
72+
BatchProperties batchProperties) {
73+
TaskJobLauncherApplicationRunnerFactoryBean taskJobLauncherApplicationRunnerFactoryBean;
74+
taskJobLauncherApplicationRunnerFactoryBean = new TaskJobLauncherApplicationRunnerFactoryBean(
75+
jobLauncher, jobExplorer, jobs, this.properties, jobRegistry,
76+
jobRepository, batchProperties);
77+
78+
return taskJobLauncherApplicationRunnerFactoryBean;
79+
}
80+
6381
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 the original author or authors.
2+
* Copyright 2018-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,8 @@
3333
* Factory bean for creating an instance of {@link TaskJobLauncherCommandLineRunner}.
3434
*
3535
* @author Glenn Renfro
36+
* @deprecated Use
37+
* {@link org.springframework.cloud.task.batch.handler.TaskJobLauncherApplicationRunner}
3638
*/
3739
public class TaskJobLauncherCommandLineRunnerFactoryBean
3840
implements FactoryBean<TaskJobLauncherCommandLineRunner> {

0 commit comments

Comments
 (0)