Resolve issue where spring.batch.job.names was not expressed

when using TaskJobLauncherCommandLineRunner

resolves #614
This commit is contained in:
Glenn Renfro
2019-08-22 09:20:10 -04:00
committed by Michael Minella
parent 87b8555b66
commit d39ed3c7cb
4 changed files with 50 additions and 4 deletions

View File

@@ -33,6 +33,7 @@ public class TaskBatchProperties {
/**
* Comma-separated list of job names to execute on startup (for instance,
* `job1,job2`). By default, all Jobs found in the context are executed.
* @deprecated use spring.batch.job.names instead of spring.cloud.task.batch.jobNames.
*/
private String jobNames = "";

View File

@@ -26,6 +26,7 @@ import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
@@ -49,11 +50,12 @@ public class TaskJobLauncherAutoConfiguration {
@Bean
public TaskJobLauncherCommandLineRunnerFactoryBean jobLauncherCommandLineRunner(
JobLauncher jobLauncher, JobExplorer jobExplorer, List<Job> jobs,
JobRegistry jobRegistry, JobRepository jobRepository) {
JobRegistry jobRegistry, JobRepository jobRepository,
BatchProperties batchProperties) {
TaskJobLauncherCommandLineRunnerFactoryBean taskJobLauncherCommandLineRunnerFactoryBean;
taskJobLauncherCommandLineRunnerFactoryBean = new TaskJobLauncherCommandLineRunnerFactoryBean(
jobLauncher, jobExplorer, jobs, this.properties, jobRegistry,
jobRepository);
jobRepository, batchProperties);
return taskJobLauncherCommandLineRunnerFactoryBean;
}

View File

@@ -24,6 +24,7 @@ import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.cloud.task.batch.handler.TaskJobLauncherCommandLineRunner;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -55,8 +56,9 @@ public class TaskJobLauncherCommandLineRunnerFactoryBean
public TaskJobLauncherCommandLineRunnerFactoryBean(JobLauncher jobLauncher,
JobExplorer jobExplorer, List<Job> jobs,
TaskBatchProperties taskBatchProperties, JobRegistry jobRegistry,
JobRepository jobRepository) {
Assert.notNull(taskBatchProperties, "properties must not be null");
JobRepository jobRepository, BatchProperties batchProperties) {
Assert.notNull(taskBatchProperties, "taskBatchProperties must not be null");
Assert.notNull(batchProperties, "batchProperties must not be null");
this.jobLauncher = jobLauncher;
this.jobExplorer = jobExplorer;
Assert.notEmpty(jobs, "jobs must not be null nor empty");
@@ -64,6 +66,12 @@ public class TaskJobLauncherCommandLineRunnerFactoryBean
this.jobNames = taskBatchProperties.getJobNames();
this.jobRegistry = jobRegistry;
this.taskBatchProperties = taskBatchProperties;
if (StringUtils.hasText(batchProperties.getJob().getNames())) {
this.jobNames = batchProperties.getJob().getNames();
}
else {
this.jobNames = taskBatchProperties.getJobNames();
}
this.order = taskBatchProperties.getCommandLineRunnerOrder();
this.jobRepository = jobRepository;
}

View File

@@ -16,12 +16,15 @@
package org.springframework.cloud.task.batch.configuration;
import java.lang.reflect.Field;
import org.junit.Test;
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.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.task.batch.handler.TaskJobLauncherCommandLineRunner;
import org.springframework.cloud.task.batch.listener.TaskBatchExecutionListenerTests;
@@ -62,6 +65,38 @@ public class TaskJobLauncherAutoConfigurationTests {
});
}
@Test
public void testAutoBuiltDataSourceWithBatchJobNames() {
this.contextRunner
.withPropertyValues("spring.cloud.task.batch.fail-on-job-failure=true",
"spring.batch.job.names=job1,job2",
"spring.cloud.task.batch.jobNames=foobar")
.run(context -> {
validateJobNames(context, "job1,job2");
});
}
@Test
public void testAutoBuiltDataSourceWithTaskBatchJobNames() {
this.contextRunner
.withPropertyValues("spring.cloud.task.batch.fail-on-job-failure=true",
"spring.cloud.task.batch.jobNames=job1,job2")
.run(context -> {
validateJobNames(context, "job1,job2");
});
}
private void validateJobNames(AssertableApplicationContext context, String jobNames)
throws Exception {
JobLauncherCommandLineRunner jobLauncherCommandLineRunner = context
.getBean(JobLauncherCommandLineRunner.class);
Field field = jobLauncherCommandLineRunner.getClass().getSuperclass()
.getDeclaredField("jobNames");
field.setAccessible(true);
String names = (String) field.get(jobLauncherCommandLineRunner);
assertThat(names).isEqualTo(jobNames);
}
@Test
public void testAutoBuiltDataSourceWithTaskJobLauncherCLRDisabled() {
this.contextRunner.run(context -> {