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;
}

View File

@@ -43,10 +43,25 @@ public class TaskJobLauncherAutoConfigurationTests {
@Test
public void testAutoBuiltDataSourceWithTaskJobLauncherCLR() {
this.contextRunner.
withPropertyValues("spring.cloud.task.batch.commandLineRunnerEnabled=true").
withPropertyValues("spring.cloud.task.batch.failOnJobFailure=true").
run(context -> {
assertThat(context).hasSingleBean(TaskJobLauncherCommandLineRunner.class);
assertThat(context).doesNotHaveBean(JobLauncherCommandLineRunner.class);
assertThat(context.getBean(TaskJobLauncherCommandLineRunner.class)
.getOrder())
.isEqualTo(0);
});
}
@Test
public void testAutoBuiltDataSourceWithTaskJobLauncherCLROrder() {
this.contextRunner.
withPropertyValues("spring.cloud.task.batch.failOnJobFailure=true",
"spring.cloud.task.batch.commandLineRunnerOrder=100").
run(context -> {
assertThat(context.getBean(TaskJobLauncherCommandLineRunner.class)
.getOrder())
.isEqualTo(100);
});
}
@@ -58,3 +73,4 @@ public class TaskJobLauncherAutoConfigurationTests {
});
}
}

View File

@@ -65,7 +65,7 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
private JobExplorer jobExplorer;
@Autowired
PlatformTransactionManager transactionManager;
private PlatformTransactionManager transactionManager;
private TaskJobLauncherCommandLineRunner runner;

View File

@@ -18,7 +18,6 @@ package org.springframework.cloud.task.batch.handler;
import java.util.Set;
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Test;
@@ -37,13 +36,11 @@ import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
import org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.task.batch.configuration.TaskBatchAutoConfiguration;
import org.springframework.cloud.task.batch.configuration.TaskJobLauncherAutoConfiguration;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -68,7 +65,7 @@ public class TaskJobLauncherCommandLineRunnerTests {
@Test
public void testTaskJobLauncherCLRSuccessFail() {
String[] enabledArgs = new String[] { "--spring.cloud.task.batch.commandLineRunnerEnabled=true" };
String[] enabledArgs = new String[] { "--spring.cloud.task.batch.failOnJobFailure=true" };
boolean isExceptionThrown = false;
try {
this.applicationContext = SpringApplication
@@ -88,7 +85,7 @@ public class TaskJobLauncherCommandLineRunnerTests {
@Test
public void testTaskJobLauncherPickOneJob() {
String[] enabledArgs = new String[] {
"--spring.cloud.task.batch.commandLineRunnerEnabled=true",
"--spring.cloud.task.batch.failOnJobFailure=true",
"--spring.cloud.task.batch.jobNames=jobSucceed" };
boolean isExceptionThrown = false;
try {