Skip to content

Commit a785fb0

Browse files
hpoettkercppwfs
authored andcommitted
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
1 parent 3d785ff commit a785fb0

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -25,6 +25,7 @@
2525
import org.springframework.cloud.task.batch.listener.TaskBatchExecutionListener;
2626
import org.springframework.cloud.task.configuration.TaskConfigurer;
2727
import org.springframework.cloud.task.configuration.TaskProperties;
28+
import org.springframework.cloud.task.listener.TaskLifecycleListener;
2829
import org.springframework.cloud.task.repository.TaskExplorer;
2930
import org.springframework.context.ApplicationContext;
3031
import org.springframework.context.annotation.Bean;
@@ -39,7 +40,7 @@
3940
* @author Michael Minella
4041
*/
4142
@Configuration(proxyBeanMethods = false)
42-
@ConditionalOnBean({ Job.class })
43+
@ConditionalOnBean({Job.class, TaskLifecycleListener.class})
4344
@ConditionalOnProperty(
4445
name = { "spring.cloud.task.batch.listener.enable",
4546
"spring.cloud.task.batch.listener.enabled" },

spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/TaskBatchExecutionListenerTests.java

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.batch.core.step.tasklet.Tasklet;
3939
import org.springframework.batch.repeat.RepeatStatus;
4040
import org.springframework.beans.factory.FactoryBean;
41+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
4142
import org.springframework.beans.factory.annotation.Autowired;
4243
import org.springframework.boot.SpringApplication;
4344
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
@@ -64,6 +65,7 @@
6465

6566
import static org.assertj.core.api.Assertions.assertThat;
6667
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
68+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6769

6870
/**
6971
* @author Michael Minella
@@ -151,6 +153,16 @@ private void validateContext() {
151153

152154
}
153155

156+
@Test
157+
public void testNoListenerIfTaskNotEnabled() {
158+
this.applicationContext = SpringApplication.run(TaskNotEnabledConfiguration.class, ARGS);
159+
assertThat(applicationContext.getBean(Job.class)).isNotNull();
160+
assertThatThrownBy(() -> applicationContext.getBean(TaskBatchExecutionListenerBeanPostProcessor.class))
161+
.isInstanceOf(NoSuchBeanDefinitionException.class);
162+
assertThatThrownBy(() -> applicationContext.getBean(TaskBatchExecutionListener.class))
163+
.isInstanceOf(NoSuchBeanDefinitionException.class);
164+
}
165+
154166
@Test
155167
public void testMultipleDataSources() {
156168
this.applicationContext = SpringApplication
@@ -297,18 +309,36 @@ public static class JobConfiguration {
297309
@Bean
298310
public Job job() {
299311
return this.jobBuilderFactory.get("job")
300-
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
301-
@Override
302-
public RepeatStatus execute(StepContribution contribution,
303-
ChunkContext chunkContext) throws Exception {
304-
System.out.println("Executed");
305-
return RepeatStatus.FINISHED;
306-
}
312+
.start(this.stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
313+
System.out.println("Executed");
314+
return RepeatStatus.FINISHED;
307315
}).build()).build();
308316
}
309317

310318
}
311319

320+
@EnableBatchProcessing
321+
@TaskBatchTest
322+
@Import(EmbeddedDataSourceConfiguration.class)
323+
public static class TaskNotEnabledConfiguration {
324+
325+
@Autowired
326+
private JobBuilderFactory jobBuilderFactory;
327+
328+
@Autowired
329+
private StepBuilderFactory stepBuilderFactory;
330+
331+
@Bean
332+
public Job job() {
333+
return this.jobBuilderFactory.get("job")
334+
.start(this.stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
335+
System.out.println("Executed");
336+
return RepeatStatus.FINISHED;
337+
}).build()).build();
338+
}
339+
340+
}
341+
312342
@EnableBatchProcessing
313343
@TaskBatchTest
314344
@EnableTask
@@ -325,18 +355,12 @@ public static class JobFactoryBeanConfiguration {
325355
public FactoryBean<Job> job() {
326356
return new FactoryBean<Job>() {
327357
@Override
328-
public Job getObject() throws Exception {
358+
public Job getObject() {
329359
return JobFactoryBeanConfiguration.this.jobBuilderFactory.get("job")
330360
.start(JobFactoryBeanConfiguration.this.stepBuilderFactory
331-
.get("step1").tasklet(new Tasklet() {
332-
@Override
333-
public RepeatStatus execute(
334-
StepContribution contribution,
335-
ChunkContext chunkContext)
336-
throws Exception {
337-
System.out.println("Executed");
338-
return RepeatStatus.FINISHED;
339-
}
361+
.get("step1").tasklet((contribution, chunkContext) -> {
362+
System.out.println("Executed");
363+
return RepeatStatus.FINISHED;
340364
}).build())
341365
.build();
342366
}
@@ -417,26 +441,18 @@ public static class MultipleJobConfiguration {
417441
@Bean
418442
public Job job1() {
419443
return this.jobBuilderFactory.get("job1").start(
420-
this.stepBuilderFactory.get("job1step1").tasklet(new Tasklet() {
421-
@Override
422-
public RepeatStatus execute(StepContribution contribution,
423-
ChunkContext chunkContext) throws Exception {
424-
System.out.println("Executed job1");
425-
return RepeatStatus.FINISHED;
426-
}
444+
this.stepBuilderFactory.get("job1step1").tasklet((contribution, chunkContext) -> {
445+
System.out.println("Executed job1");
446+
return RepeatStatus.FINISHED;
427447
}).build()).build();
428448
}
429449

430450
@Bean
431451
public Job job2() {
432452
return this.jobBuilderFactory.get("job2").start(
433-
this.stepBuilderFactory.get("job2step1").tasklet(new Tasklet() {
434-
@Override
435-
public RepeatStatus execute(StepContribution contribution,
436-
ChunkContext chunkContext) throws Exception {
437-
System.out.println("Executed job2");
438-
return RepeatStatus.FINISHED;
439-
}
453+
this.stepBuilderFactory.get("job2step1").tasklet((contribution, chunkContext) -> {
454+
System.out.println("Executed job2");
455+
return RepeatStatus.FINISHED;
440456
}).build()).build();
441457
}
442458

0 commit comments

Comments
 (0)