From a785fb00b1830906d2e55c42eeaec116f8a128c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henning=20P=C3=B6ttker?= Date: Sun, 4 Jul 2021 23:31:13 +0200 Subject: [PATCH] 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 --- .../TaskBatchAutoConfiguration.java | 5 +- .../TaskBatchExecutionListenerTests.java | 78 +++++++++++-------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/configuration/TaskBatchAutoConfiguration.java b/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/configuration/TaskBatchAutoConfiguration.java index ad11fb211..a2a83ecbf 100644 --- a/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/configuration/TaskBatchAutoConfiguration.java +++ b/spring-cloud-task-batch/src/main/java/org/springframework/cloud/task/batch/configuration/TaskBatchAutoConfiguration.java @@ -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.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 @@ * @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" }, diff --git a/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/TaskBatchExecutionListenerTests.java b/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/TaskBatchExecutionListenerTests.java index a1d5363b4..628632d74 100644 --- a/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/TaskBatchExecutionListenerTests.java +++ b/spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/TaskBatchExecutionListenerTests.java @@ -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; @@ -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 @@ -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 @@ -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 @@ -325,18 +355,12 @@ public static class JobFactoryBeanConfiguration { public FactoryBean job() { return new FactoryBean() { @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 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(); }