Added ability to cofigure CommandLineRunner order

This commit adds the ability to configure the order for the
TaskJobLauncherCommandLineRunner.  It also provides minor polish on a
few other code review related items.
This commit is contained in:
Michael Minella
2018-02-12 17:00:22 -06:00
parent cd4dbe6a93
commit 1ae7930d09
7 changed files with 66 additions and 13 deletions

View File

@@ -23,6 +23,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* Spring Batch.
*
* @author Glenn Renfro
* @author Michael Minella
*
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.cloud.task.batch")
public class TaskBatchProperties {
@@ -33,6 +36,13 @@ public class TaskBatchProperties {
*/
private String jobNames = "";
/**
* The order for the {@coce CommandLineRunner} used to run batch jobs when
* {@code spring.cloud.task.batch.failOnJobFailure=true}. Defaults to 0 (same as the
* {@link org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner}).
*/
private int commandLineRunnerOrder = 0;
public String getJobNames() {
return this.jobNames;
}
@@ -40,4 +50,12 @@ public class TaskBatchProperties {
public void setJobNames(String jobNames) {
this.jobNames = jobNames;
}
public int getCommandLineRunnerOrder() {
return commandLineRunnerOrder;
}
public void setCommandLineRunnerOrder(int commandLineRunnerOrder) {
this.commandLineRunnerOrder = commandLineRunnerOrder;
}
}

View File

@@ -23,7 +23,6 @@ import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@@ -36,7 +35,7 @@ import org.springframework.context.annotation.Configuration;
* @author Glenn Renfro
*/
@Configuration
@ConditionalOnProperty(name = "spring.cloud.task.batch.commandLineRunnerEnabled", havingValue = "true", matchIfMissing = false)
@ConditionalOnProperty(name = "spring.cloud.task.batch.failOnJobFailure", havingValue = "true", matchIfMissing = false)
@EnableConfigurationProperties(TaskBatchProperties.class)
public class TaskJobLauncherAutoConfiguration {
@@ -46,8 +45,15 @@ public class TaskJobLauncherAutoConfiguration {
@Bean
public TaskJobLauncherCommandLineRunnerFactoryBean jobLauncherCommandLineRunner(JobLauncher jobLauncher,
JobExplorer jobExplorer, List<Job> jobs, JobRegistry jobRegistry) {
TaskJobLauncherCommandLineRunnerFactoryBean taskJobLauncherCommandLineRunner = new TaskJobLauncherCommandLineRunnerFactoryBean(
jobLauncher, jobExplorer, jobs, this.properties.getJobNames(), jobRegistry);
return taskJobLauncherCommandLineRunner;
TaskJobLauncherCommandLineRunnerFactoryBean taskJobLauncherCommandLineRunnerFactoryBean =
new TaskJobLauncherCommandLineRunnerFactoryBean(jobLauncher,
jobExplorer,
jobs,
this.properties.getJobNames(),
jobRegistry);
taskJobLauncherCommandLineRunnerFactoryBean.setOrder(this.properties.getCommandLineRunnerOrder());
return taskJobLauncherCommandLineRunnerFactoryBean;
}
}

View File

@@ -44,6 +44,8 @@ public class TaskJobLauncherCommandLineRunnerFactoryBean implements FactoryBean<
private JobRegistry jobRegistry;
private Integer order = 0;
public TaskJobLauncherCommandLineRunnerFactoryBean(JobLauncher jobLauncher,
JobExplorer jobExplorer, List<Job> jobs, String jobNames,
JobRegistry jobRegistry) {
@@ -55,6 +57,10 @@ public class TaskJobLauncherCommandLineRunnerFactoryBean implements FactoryBean<
this.jobRegistry = jobRegistry;
}
public void setOrder(int order) {
this.order = order;
}
@Override
public TaskJobLauncherCommandLineRunner getObject() throws Exception {
TaskJobLauncherCommandLineRunner taskJobLauncherCommandLineRunner =
@@ -64,6 +70,11 @@ public class TaskJobLauncherCommandLineRunnerFactoryBean implements FactoryBean<
taskJobLauncherCommandLineRunner.setJobNames(this.jobNames);
}
taskJobLauncherCommandLineRunner.setJobRegistry(this.jobRegistry);
if(this.order != null) {
taskJobLauncherCommandLineRunner.setOrder(this.order);
}
return taskJobLauncherCommandLineRunner;
}