From 3f3df72b7886f5cac6bc6b0e9be1ba736e58d873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Mon, 18 Nov 2024 14:21:06 +0100 Subject: [PATCH] Polish "Add TaskDecorator support for scheduled tasks" See gh-43190 --- .../task/TaskSchedulingConfigurations.java | 18 +++++------ .../TaskSchedulingAutoConfigurationTests.java | 30 +++++++++---------- .../task/SimpleAsyncTaskSchedulerBuilder.java | 27 +++++++---------- .../task/ThreadPoolTaskSchedulerBuilder.java | 28 +++++++---------- .../SimpleAsyncTaskSchedulerBuilderTests.java | 26 ++++++++-------- .../ThreadPoolTaskSchedulerBuilderTests.java | 14 ++++----- 6 files changed, 66 insertions(+), 77 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingConfigurations.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingConfigurations.java index b0cb0d187c20..dfe9c5977117 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingConfigurations.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingConfigurations.java @@ -68,16 +68,16 @@ static class ThreadPoolTaskSchedulerBuilderConfiguration { @Bean @ConditionalOnMissingBean ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder(TaskSchedulingProperties properties, - ObjectProvider threadPoolTaskSchedulerCustomizers, - ObjectProvider taskDecorator) { + ObjectProvider taskDecorator, + ObjectProvider threadPoolTaskSchedulerCustomizers) { TaskSchedulingProperties.Shutdown shutdown = properties.getShutdown(); ThreadPoolTaskSchedulerBuilder builder = new ThreadPoolTaskSchedulerBuilder(); builder = builder.poolSize(properties.getPool().getSize()); builder = builder.awaitTermination(shutdown.isAwaitTermination()); builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod()); builder = builder.threadNamePrefix(properties.getThreadNamePrefix()); - builder = builder.customizers(threadPoolTaskSchedulerCustomizers); builder = builder.taskDecorator(taskDecorator.getIfUnique()); + builder = builder.customizers(threadPoolTaskSchedulerCustomizers); return builder; } @@ -88,16 +88,16 @@ static class SimpleAsyncTaskSchedulerBuilderConfiguration { private final TaskSchedulingProperties properties; - private final ObjectProvider taskSchedulerCustomizers; - private final ObjectProvider taskDecorator; + private final ObjectProvider taskSchedulerCustomizers; + SimpleAsyncTaskSchedulerBuilderConfiguration(TaskSchedulingProperties properties, - ObjectProvider taskSchedulerCustomizers, - ObjectProvider taskDecorator) { + ObjectProvider taskDecorator, + ObjectProvider taskSchedulerCustomizers) { this.properties = properties; - this.taskSchedulerCustomizers = taskSchedulerCustomizers; this.taskDecorator = taskDecorator; + this.taskSchedulerCustomizers = taskSchedulerCustomizers; } @Bean @@ -117,6 +117,7 @@ SimpleAsyncTaskSchedulerBuilder simpleAsyncTaskSchedulerBuilderVirtualThreads() private SimpleAsyncTaskSchedulerBuilder builder() { SimpleAsyncTaskSchedulerBuilder builder = new SimpleAsyncTaskSchedulerBuilder(); builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix()); + builder = builder.taskDecorator(this.taskDecorator.getIfUnique()); builder = builder.customizers(this.taskSchedulerCustomizers.orderedStream()::iterator); TaskSchedulingProperties.Simple simple = this.properties.getSimple(); builder = builder.concurrencyLimit(simple.getConcurrencyLimit()); @@ -124,7 +125,6 @@ private SimpleAsyncTaskSchedulerBuilder builder() { if (shutdown.isAwaitTermination()) { builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod()); } - builder = builder.taskDecorator(this.taskDecorator.getIfUnique()); return builder; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java index 62963d383229..b7ae6bb0011b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java @@ -141,21 +141,6 @@ void simpleAsyncTaskSchedulerBuilderShouldUsePlatformThreadsByDefault() { }); } - @Test - void simpleAsyncTaskSchedulerBuilderShouldApplyCustomizers() { - SimpleAsyncTaskSchedulerCustomizer customizer = (scheduler) -> { - }; - this.contextRunner.withBean(SimpleAsyncTaskSchedulerCustomizer.class, () -> customizer) - .withUserConfiguration(SchedulingConfiguration.class) - .run((context) -> { - assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class); - SimpleAsyncTaskSchedulerBuilder builder = context.getBean(SimpleAsyncTaskSchedulerBuilder.class); - assertThat(builder).extracting("customizers") - .asInstanceOf(InstanceOfAssertFactories.collection(SimpleAsyncTaskSchedulerCustomizer.class)) - .containsExactly(customizer); - }); - } - @Test void simpleAsyncTaskSchedulerBuilderShouldApplyTaskDecorator() { this.contextRunner.withUserConfiguration(SchedulingConfiguration.class, TaskDecoratorConfig.class) @@ -180,6 +165,21 @@ void threadPoolTaskSchedulerBuilderShouldApplyTaskDecorator() { }); } + @Test + void simpleAsyncTaskSchedulerBuilderShouldApplyCustomizers() { + SimpleAsyncTaskSchedulerCustomizer customizer = (scheduler) -> { + }; + this.contextRunner.withBean(SimpleAsyncTaskSchedulerCustomizer.class, () -> customizer) + .withUserConfiguration(SchedulingConfiguration.class) + .run((context) -> { + assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class); + SimpleAsyncTaskSchedulerBuilder builder = context.getBean(SimpleAsyncTaskSchedulerBuilder.class); + assertThat(builder).extracting("customizers") + .asInstanceOf(InstanceOfAssertFactories.collection(SimpleAsyncTaskSchedulerCustomizer.class)) + .containsExactly(customizer); + }); + } + @Test void enableSchedulingWithNoTaskExecutorAppliesCustomizers() { this.contextRunner.withPropertyValues("spring.task.scheduling.thread-name-prefix=scheduling-test-") diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/SimpleAsyncTaskSchedulerBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/SimpleAsyncTaskSchedulerBuilder.java index 4cd498a65ea4..24a537f36d2e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/SimpleAsyncTaskSchedulerBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/SimpleAsyncTaskSchedulerBuilder.java @@ -54,11 +54,6 @@ public class SimpleAsyncTaskSchedulerBuilder { private final Set customizers; - /** - * Constructs a new {@code SimpleAsyncTaskSchedulerBuilder} with default settings. - * Initializes a builder instance with all fields set to {@code null}, allowing for - * further customization through its fluent API methods. - */ public SimpleAsyncTaskSchedulerBuilder() { this(null, null, null, null, null, null); } @@ -115,6 +110,17 @@ public SimpleAsyncTaskSchedulerBuilder taskTerminationTimeout(Duration taskTermi taskTerminationTimeout, this.taskDecorator, this.customizers); } + /** + * Set the task decorator to be used by the {@link SimpleAsyncTaskScheduler}. + * @param taskDecorator the task decorator to set + * @return a new builder instance + * @since 3.5.0 + */ + public SimpleAsyncTaskSchedulerBuilder taskDecorator(TaskDecorator taskDecorator) { + return new SimpleAsyncTaskSchedulerBuilder(this.threadNamePrefix, this.concurrencyLimit, this.virtualThreads, + this.taskTerminationTimeout, taskDecorator, this.customizers); + } + /** * Set the {@link SimpleAsyncTaskSchedulerCustomizer customizers} that should be * applied to the {@link SimpleAsyncTaskScheduler}. Customizers are applied in the @@ -173,17 +179,6 @@ public SimpleAsyncTaskSchedulerBuilder additionalCustomizers( this.taskTerminationTimeout, this.taskDecorator, append(this.customizers, customizers)); } - /** - * Set the task decorator to be used by the {@link SimpleAsyncTaskScheduler}. - * @param taskDecorator the task decorator to set - * @return a new builder instance - * @since 3.5.0 - */ - public SimpleAsyncTaskSchedulerBuilder taskDecorator(TaskDecorator taskDecorator) { - return new SimpleAsyncTaskSchedulerBuilder(this.threadNamePrefix, this.concurrencyLimit, this.virtualThreads, - this.taskTerminationTimeout, taskDecorator, this.customizers); - } - /** * Build a new {@link SimpleAsyncTaskScheduler} instance and configure it using this * builder. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/ThreadPoolTaskSchedulerBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/ThreadPoolTaskSchedulerBuilder.java index 9815554056e3..86ffef67c8e6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/ThreadPoolTaskSchedulerBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/ThreadPoolTaskSchedulerBuilder.java @@ -53,12 +53,6 @@ public class ThreadPoolTaskSchedulerBuilder { private final Set customizers; - /** - * Default constructor for creating a new instance of - * {@code ThreadPoolTaskSchedulerBuilder}. Initializes a builder instance with all - * fields set to {@code null}, allowing for further customization through its fluent - * API methods. - */ public ThreadPoolTaskSchedulerBuilder() { this(null, null, null, null, null, null); } @@ -79,18 +73,18 @@ public ThreadPoolTaskSchedulerBuilder() { @Deprecated(since = "3.5.0", forRemoval = true) public ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTermination, Duration awaitTerminationPeriod, String threadNamePrefix, Set taskSchedulerCustomizers) { - this(poolSize, awaitTermination, awaitTerminationPeriod, threadNamePrefix, taskSchedulerCustomizers, null); + this(poolSize, awaitTermination, awaitTerminationPeriod, threadNamePrefix, null, taskSchedulerCustomizers); } private ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTermination, Duration awaitTerminationPeriod, - String threadNamePrefix, Set taskSchedulerCustomizers, - TaskDecorator taskDecorator) { + String threadNamePrefix, TaskDecorator taskDecorator, + Set taskSchedulerCustomizers) { this.poolSize = poolSize; this.awaitTermination = awaitTermination; this.awaitTerminationPeriod = awaitTerminationPeriod; this.threadNamePrefix = threadNamePrefix; - this.customizers = taskSchedulerCustomizers; this.taskDecorator = taskDecorator; + this.customizers = taskSchedulerCustomizers; } /** @@ -100,7 +94,7 @@ private ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTerminatio */ public ThreadPoolTaskSchedulerBuilder poolSize(int poolSize) { return new ThreadPoolTaskSchedulerBuilder(poolSize, this.awaitTermination, this.awaitTerminationPeriod, - this.threadNamePrefix, this.customizers, this.taskDecorator); + this.threadNamePrefix, this.taskDecorator, this.customizers); } /** @@ -113,7 +107,7 @@ public ThreadPoolTaskSchedulerBuilder poolSize(int poolSize) { */ public ThreadPoolTaskSchedulerBuilder awaitTermination(boolean awaitTermination) { return new ThreadPoolTaskSchedulerBuilder(this.poolSize, awaitTermination, this.awaitTerminationPeriod, - this.threadNamePrefix, this.customizers, this.taskDecorator); + this.threadNamePrefix, this.taskDecorator, this.customizers); } /** @@ -127,7 +121,7 @@ public ThreadPoolTaskSchedulerBuilder awaitTermination(boolean awaitTermination) */ public ThreadPoolTaskSchedulerBuilder awaitTerminationPeriod(Duration awaitTerminationPeriod) { return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, awaitTerminationPeriod, - this.threadNamePrefix, this.customizers, this.taskDecorator); + this.threadNamePrefix, this.taskDecorator, this.customizers); } /** @@ -137,7 +131,7 @@ public ThreadPoolTaskSchedulerBuilder awaitTerminationPeriod(Duration awaitTermi */ public ThreadPoolTaskSchedulerBuilder threadNamePrefix(String threadNamePrefix) { return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod, - threadNamePrefix, this.customizers, this.taskDecorator); + threadNamePrefix, this.taskDecorator, this.customizers); } /** @@ -148,7 +142,7 @@ public ThreadPoolTaskSchedulerBuilder threadNamePrefix(String threadNamePrefix) */ public ThreadPoolTaskSchedulerBuilder taskDecorator(TaskDecorator taskDecorator) { return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod, - this.threadNamePrefix, this.customizers, taskDecorator); + this.threadNamePrefix, taskDecorator, this.customizers); } /** @@ -180,7 +174,7 @@ public ThreadPoolTaskSchedulerBuilder customizers( Iterable customizers) { Assert.notNull(customizers, "Customizers must not be null"); return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod, - this.threadNamePrefix, append(null, customizers), this.taskDecorator); + this.threadNamePrefix, this.taskDecorator, append(null, customizers)); } /** @@ -210,7 +204,7 @@ public ThreadPoolTaskSchedulerBuilder additionalCustomizers( Iterable customizers) { Assert.notNull(customizers, "Customizers must not be null"); return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod, - this.threadNamePrefix, append(this.customizers, customizers), this.taskDecorator); + this.threadNamePrefix, this.taskDecorator, append(this.customizers, customizers)); } /** diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/SimpleAsyncTaskSchedulerBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/SimpleAsyncTaskSchedulerBuilderTests.java index f2ac75112a8e..bd951b2ec9cd 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/SimpleAsyncTaskSchedulerBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/SimpleAsyncTaskSchedulerBuilderTests.java @@ -62,6 +62,19 @@ void virtualThreadsShouldApply() { assertThat(scheduler).extracting("virtualThreadDelegate").isNotNull(); } + @Test + void taskTerminationTimeoutShouldApply() { + SimpleAsyncTaskScheduler scheduler = this.builder.taskTerminationTimeout(Duration.ofSeconds(1)).build(); + assertThat(scheduler).extracting("taskTerminationTimeout").isEqualTo(1000L); + } + + @Test + void taskDecoratorShouldApply() { + TaskDecorator taskDecorator = mock(TaskDecorator.class); + SimpleAsyncTaskScheduler scheduler = this.builder.taskDecorator(taskDecorator).build(); + assertThat(scheduler).extracting("taskDecorator").isSameAs(taskDecorator); + } + @Test void customizersWhenCustomizersAreNullShouldThrowException() { assertThatIllegalArgumentException() @@ -129,17 +142,4 @@ void additionalCustomizersShouldAddToExisting() { then(customizer2).should().customize(scheduler); } - @Test - void taskTerminationTimeoutShouldApply() { - SimpleAsyncTaskScheduler scheduler = this.builder.taskTerminationTimeout(Duration.ofSeconds(1)).build(); - assertThat(scheduler).extracting("taskTerminationTimeout").isEqualTo(1000L); - } - - @Test - void taskDecoratorShouldApply() { - TaskDecorator taskDecorator = mock(TaskDecorator.class); - SimpleAsyncTaskScheduler scheduler = this.builder.taskDecorator(taskDecorator).build(); - assertThat(scheduler).extracting("taskDecorator").isSameAs(taskDecorator); - } - } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/ThreadPoolTaskSchedulerBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/ThreadPoolTaskSchedulerBuilderTests.java index 9411700bfb15..83b65ab9faaf 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/ThreadPoolTaskSchedulerBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/ThreadPoolTaskSchedulerBuilderTests.java @@ -65,6 +65,13 @@ void threadNamePrefixShouldApply() { assertThat(scheduler.getThreadNamePrefix()).isEqualTo("test-"); } + @Test + void taskDecoratorShouldApply() { + TaskDecorator taskDecorator = mock(TaskDecorator.class); + ThreadPoolTaskScheduler scheduler = this.builder.taskDecorator(taskDecorator).build(); + assertThat(scheduler).extracting("taskDecorator").isSameAs(taskDecorator); + } + @Test void customizersWhenCustomizersAreNullShouldThrowException() { assertThatIllegalArgumentException() @@ -132,11 +139,4 @@ void additionalCustomizersShouldAddToExisting() { then(customizer2).should().customize(scheduler); } - @Test - void taskDecoratorShouldApply() { - TaskDecorator taskDecorator = mock(TaskDecorator.class); - ThreadPoolTaskScheduler scheduler = this.builder.taskDecorator(taskDecorator).build(); - assertThat(scheduler).extracting("taskDecorator").isSameAs(taskDecorator); - } - }