From 956f0d8f80c48d03b88e508a330c59bcf1a42e49 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 | 93 ++++++++++--------- 2 files changed, 54 insertions(+), 44 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 9ac7b2113..7ffc357f0 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 -@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 3ac598862..238b8e733 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 @@ -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. @@ -28,16 +28,14 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.Job; -import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.job.SimpleJob; -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 +62,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 +150,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 +306,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 +352,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(); } @@ -370,13 +391,9 @@ public static class JobConfigurationMultipleDataSources { @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(); } @@ -422,26 +439,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(); }