No TaskBatchExecutionListener without EnableTask
If SCT is on the classpath but EnableTask has not been used, the TaskBatchExecutionListener will no longer be registered with the jobs. Issue #651
This commit is contained in:
committed by
Glenn Renfro
parent
3d785ffd99
commit
a785fb00b1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2019 the original author or authors.
|
||||
* Copyright 2016-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,6 +25,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.cloud.task.batch.listener.TaskBatchExecutionListener;
|
||||
import org.springframework.cloud.task.configuration.TaskConfigurer;
|
||||
import org.springframework.cloud.task.configuration.TaskProperties;
|
||||
import org.springframework.cloud.task.listener.TaskLifecycleListener;
|
||||
import org.springframework.cloud.task.repository.TaskExplorer;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -39,7 +40,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author Michael Minella
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnBean({ Job.class })
|
||||
@ConditionalOnBean({Job.class, TaskLifecycleListener.class})
|
||||
@ConditionalOnProperty(
|
||||
name = { "spring.cloud.task.batch.listener.enable",
|
||||
"spring.cloud.task.batch.listener.enabled" },
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
|
||||
@@ -64,6 +65,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* @author Michael Minella
|
||||
@@ -151,6 +153,16 @@ public class TaskBatchExecutionListenerTests {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoListenerIfTaskNotEnabled() {
|
||||
this.applicationContext = SpringApplication.run(TaskNotEnabledConfiguration.class, ARGS);
|
||||
assertThat(applicationContext.getBean(Job.class)).isNotNull();
|
||||
assertThatThrownBy(() -> applicationContext.getBean(TaskBatchExecutionListenerBeanPostProcessor.class))
|
||||
.isInstanceOf(NoSuchBeanDefinitionException.class);
|
||||
assertThatThrownBy(() -> applicationContext.getBean(TaskBatchExecutionListener.class))
|
||||
.isInstanceOf(NoSuchBeanDefinitionException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleDataSources() {
|
||||
this.applicationContext = SpringApplication
|
||||
@@ -297,18 +309,36 @@ public class TaskBatchExecutionListenerTests {
|
||||
@Bean
|
||||
public Job job() {
|
||||
return this.jobBuilderFactory.get("job")
|
||||
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution,
|
||||
ChunkContext chunkContext) throws Exception {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
.start(this.stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).build()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBatchProcessing
|
||||
@TaskBatchTest
|
||||
@Import(EmbeddedDataSourceConfiguration.class)
|
||||
public static class TaskNotEnabledConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
@Bean
|
||||
public Job job() {
|
||||
return this.jobBuilderFactory.get("job")
|
||||
.start(this.stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).build()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@EnableBatchProcessing
|
||||
@TaskBatchTest
|
||||
@EnableTask
|
||||
@@ -325,18 +355,12 @@ public class TaskBatchExecutionListenerTests {
|
||||
public FactoryBean<Job> job() {
|
||||
return new FactoryBean<Job>() {
|
||||
@Override
|
||||
public Job getObject() throws Exception {
|
||||
public Job getObject() {
|
||||
return JobFactoryBeanConfiguration.this.jobBuilderFactory.get("job")
|
||||
.start(JobFactoryBeanConfiguration.this.stepBuilderFactory
|
||||
.get("step1").tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(
|
||||
StepContribution contribution,
|
||||
ChunkContext chunkContext)
|
||||
throws Exception {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
.get("step1").tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).build())
|
||||
.build();
|
||||
}
|
||||
@@ -417,26 +441,18 @@ public class TaskBatchExecutionListenerTests {
|
||||
@Bean
|
||||
public Job job1() {
|
||||
return this.jobBuilderFactory.get("job1").start(
|
||||
this.stepBuilderFactory.get("job1step1").tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution,
|
||||
ChunkContext chunkContext) throws Exception {
|
||||
System.out.println("Executed job1");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
this.stepBuilderFactory.get("job1step1").tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed job1");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).build()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job job2() {
|
||||
return this.jobBuilderFactory.get("job2").start(
|
||||
this.stepBuilderFactory.get("job2step1").tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution,
|
||||
ChunkContext chunkContext) throws Exception {
|
||||
System.out.println("Executed job2");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
this.stepBuilderFactory.get("job2step1").tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed job2");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).build()).build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user