Skip to content

Commit

Permalink
No TaskBatchExecutionListener without EnableTask
Browse files Browse the repository at this point in the history
If SCT is on the classpath but EnableTask has not been used, the TaskBatchExecutionListener will no longer be registered with the jobs.

Issue spring-cloud#651
  • Loading branch information
hpoettker authored and cppwfs committed Oct 20, 2021
1 parent 3d785ff commit a785fb0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,6 +25,7 @@
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;
Expand All @@ -39,7 +40,7 @@
* @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" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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;
Expand All @@ -64,6 +65,7 @@

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
Expand Down Expand Up @@ -151,6 +153,16 @@ private void validateContext() {

}

@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
Expand Down Expand Up @@ -297,18 +309,36 @@ public static class JobConfiguration {
@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
Expand All @@ -325,18 +355,12 @@ public static class JobFactoryBeanConfiguration {
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();
}
Expand Down Expand Up @@ -417,26 +441,18 @@ public static class MultipleJobConfiguration {
@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();
}

Expand Down

0 comments on commit a785fb0

Please sign in to comment.