Polish "Add TaskDecorator support for scheduled tasks"

See gh-43190
This commit is contained in:
Stéphane Nicoll 2024-11-18 14:21:06 +01:00
parent f0c5312141
commit ced7c1617c
6 changed files with 66 additions and 77 deletions

View File

@ -68,16 +68,16 @@ class TaskSchedulingConfigurations {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder(TaskSchedulingProperties properties, ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder(TaskSchedulingProperties properties,
ObjectProvider<ThreadPoolTaskSchedulerCustomizer> threadPoolTaskSchedulerCustomizers, ObjectProvider<TaskDecorator> taskDecorator,
ObjectProvider<TaskDecorator> taskDecorator) { ObjectProvider<ThreadPoolTaskSchedulerCustomizer> threadPoolTaskSchedulerCustomizers) {
TaskSchedulingProperties.Shutdown shutdown = properties.getShutdown(); TaskSchedulingProperties.Shutdown shutdown = properties.getShutdown();
ThreadPoolTaskSchedulerBuilder builder = new ThreadPoolTaskSchedulerBuilder(); ThreadPoolTaskSchedulerBuilder builder = new ThreadPoolTaskSchedulerBuilder();
builder = builder.poolSize(properties.getPool().getSize()); builder = builder.poolSize(properties.getPool().getSize());
builder = builder.awaitTermination(shutdown.isAwaitTermination()); builder = builder.awaitTermination(shutdown.isAwaitTermination());
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod()); builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
builder = builder.threadNamePrefix(properties.getThreadNamePrefix()); builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
builder = builder.customizers(threadPoolTaskSchedulerCustomizers);
builder = builder.taskDecorator(taskDecorator.getIfUnique()); builder = builder.taskDecorator(taskDecorator.getIfUnique());
builder = builder.customizers(threadPoolTaskSchedulerCustomizers);
return builder; return builder;
} }
@ -88,16 +88,16 @@ class TaskSchedulingConfigurations {
private final TaskSchedulingProperties properties; private final TaskSchedulingProperties properties;
private final ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers;
private final ObjectProvider<TaskDecorator> taskDecorator; private final ObjectProvider<TaskDecorator> taskDecorator;
private final ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers;
SimpleAsyncTaskSchedulerBuilderConfiguration(TaskSchedulingProperties properties, SimpleAsyncTaskSchedulerBuilderConfiguration(TaskSchedulingProperties properties,
ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers, ObjectProvider<TaskDecorator> taskDecorator,
ObjectProvider<TaskDecorator> taskDecorator) { ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers) {
this.properties = properties; this.properties = properties;
this.taskSchedulerCustomizers = taskSchedulerCustomizers;
this.taskDecorator = taskDecorator; this.taskDecorator = taskDecorator;
this.taskSchedulerCustomizers = taskSchedulerCustomizers;
} }
@Bean @Bean
@ -117,6 +117,7 @@ class TaskSchedulingConfigurations {
private SimpleAsyncTaskSchedulerBuilder builder() { private SimpleAsyncTaskSchedulerBuilder builder() {
SimpleAsyncTaskSchedulerBuilder builder = new SimpleAsyncTaskSchedulerBuilder(); SimpleAsyncTaskSchedulerBuilder builder = new SimpleAsyncTaskSchedulerBuilder();
builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix()); builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix());
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
builder = builder.customizers(this.taskSchedulerCustomizers.orderedStream()::iterator); builder = builder.customizers(this.taskSchedulerCustomizers.orderedStream()::iterator);
TaskSchedulingProperties.Simple simple = this.properties.getSimple(); TaskSchedulingProperties.Simple simple = this.properties.getSimple();
builder = builder.concurrencyLimit(simple.getConcurrencyLimit()); builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
@ -124,7 +125,6 @@ class TaskSchedulingConfigurations {
if (shutdown.isAwaitTermination()) { if (shutdown.isAwaitTermination()) {
builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod()); builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod());
} }
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
return builder; return builder;
} }

View File

@ -141,21 +141,6 @@ class TaskSchedulingAutoConfigurationTests {
}); });
} }
@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 @Test
void simpleAsyncTaskSchedulerBuilderShouldApplyTaskDecorator() { void simpleAsyncTaskSchedulerBuilderShouldApplyTaskDecorator() {
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class, TaskDecoratorConfig.class) this.contextRunner.withUserConfiguration(SchedulingConfiguration.class, TaskDecoratorConfig.class)
@ -180,6 +165,21 @@ class TaskSchedulingAutoConfigurationTests {
}); });
} }
@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 @Test
void enableSchedulingWithNoTaskExecutorAppliesCustomizers() { void enableSchedulingWithNoTaskExecutorAppliesCustomizers() {
this.contextRunner.withPropertyValues("spring.task.scheduling.thread-name-prefix=scheduling-test-") this.contextRunner.withPropertyValues("spring.task.scheduling.thread-name-prefix=scheduling-test-")

View File

@ -54,11 +54,6 @@ public class SimpleAsyncTaskSchedulerBuilder {
private final Set<SimpleAsyncTaskSchedulerCustomizer> customizers; private final Set<SimpleAsyncTaskSchedulerCustomizer> 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() { public SimpleAsyncTaskSchedulerBuilder() {
this(null, null, null, null, null, null); this(null, null, null, null, null, null);
} }
@ -115,6 +110,17 @@ public class SimpleAsyncTaskSchedulerBuilder {
taskTerminationTimeout, this.taskDecorator, this.customizers); 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 * Set the {@link SimpleAsyncTaskSchedulerCustomizer customizers} that should be
* applied to the {@link SimpleAsyncTaskScheduler}. Customizers are applied in the * applied to the {@link SimpleAsyncTaskScheduler}. Customizers are applied in the
@ -173,17 +179,6 @@ public class SimpleAsyncTaskSchedulerBuilder {
this.taskTerminationTimeout, this.taskDecorator, append(this.customizers, customizers)); 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 * Build a new {@link SimpleAsyncTaskScheduler} instance and configure it using this
* builder. * builder.

View File

@ -53,12 +53,6 @@ public class ThreadPoolTaskSchedulerBuilder {
private final Set<ThreadPoolTaskSchedulerCustomizer> customizers; private final Set<ThreadPoolTaskSchedulerCustomizer> 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() { public ThreadPoolTaskSchedulerBuilder() {
this(null, null, null, null, null, null); this(null, null, null, null, null, null);
} }
@ -79,18 +73,18 @@ public class ThreadPoolTaskSchedulerBuilder {
@Deprecated(since = "3.5.0", forRemoval = true) @Deprecated(since = "3.5.0", forRemoval = true)
public ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTermination, Duration awaitTerminationPeriod, public ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTermination, Duration awaitTerminationPeriod,
String threadNamePrefix, Set<ThreadPoolTaskSchedulerCustomizer> taskSchedulerCustomizers) { String threadNamePrefix, Set<ThreadPoolTaskSchedulerCustomizer> taskSchedulerCustomizers) {
this(poolSize, awaitTermination, awaitTerminationPeriod, threadNamePrefix, taskSchedulerCustomizers, null); this(poolSize, awaitTermination, awaitTerminationPeriod, threadNamePrefix, null, taskSchedulerCustomizers);
} }
private ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTermination, Duration awaitTerminationPeriod, private ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTermination, Duration awaitTerminationPeriod,
String threadNamePrefix, Set<ThreadPoolTaskSchedulerCustomizer> taskSchedulerCustomizers, String threadNamePrefix, TaskDecorator taskDecorator,
TaskDecorator taskDecorator) { Set<ThreadPoolTaskSchedulerCustomizer> taskSchedulerCustomizers) {
this.poolSize = poolSize; this.poolSize = poolSize;
this.awaitTermination = awaitTermination; this.awaitTermination = awaitTermination;
this.awaitTerminationPeriod = awaitTerminationPeriod; this.awaitTerminationPeriod = awaitTerminationPeriod;
this.threadNamePrefix = threadNamePrefix; this.threadNamePrefix = threadNamePrefix;
this.customizers = taskSchedulerCustomizers;
this.taskDecorator = taskDecorator; this.taskDecorator = taskDecorator;
this.customizers = taskSchedulerCustomizers;
} }
/** /**
@ -100,7 +94,7 @@ public class ThreadPoolTaskSchedulerBuilder {
*/ */
public ThreadPoolTaskSchedulerBuilder poolSize(int poolSize) { public ThreadPoolTaskSchedulerBuilder poolSize(int poolSize) {
return new ThreadPoolTaskSchedulerBuilder(poolSize, this.awaitTermination, this.awaitTerminationPeriod, 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 class ThreadPoolTaskSchedulerBuilder {
*/ */
public ThreadPoolTaskSchedulerBuilder awaitTermination(boolean awaitTermination) { public ThreadPoolTaskSchedulerBuilder awaitTermination(boolean awaitTermination) {
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, awaitTermination, this.awaitTerminationPeriod, 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 class ThreadPoolTaskSchedulerBuilder {
*/ */
public ThreadPoolTaskSchedulerBuilder awaitTerminationPeriod(Duration awaitTerminationPeriod) { public ThreadPoolTaskSchedulerBuilder awaitTerminationPeriod(Duration awaitTerminationPeriod) {
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, 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 class ThreadPoolTaskSchedulerBuilder {
*/ */
public ThreadPoolTaskSchedulerBuilder threadNamePrefix(String threadNamePrefix) { public ThreadPoolTaskSchedulerBuilder threadNamePrefix(String threadNamePrefix) {
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod, return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod,
threadNamePrefix, this.customizers, this.taskDecorator); threadNamePrefix, this.taskDecorator, this.customizers);
} }
/** /**
@ -148,7 +142,7 @@ public class ThreadPoolTaskSchedulerBuilder {
*/ */
public ThreadPoolTaskSchedulerBuilder taskDecorator(TaskDecorator taskDecorator) { public ThreadPoolTaskSchedulerBuilder taskDecorator(TaskDecorator taskDecorator) {
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod, return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod,
this.threadNamePrefix, this.customizers, taskDecorator); this.threadNamePrefix, taskDecorator, this.customizers);
} }
/** /**
@ -180,7 +174,7 @@ public class ThreadPoolTaskSchedulerBuilder {
Iterable<? extends ThreadPoolTaskSchedulerCustomizer> customizers) { Iterable<? extends ThreadPoolTaskSchedulerCustomizer> customizers) {
Assert.notNull(customizers, "Customizers must not be null"); Assert.notNull(customizers, "Customizers must not be null");
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod, 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 class ThreadPoolTaskSchedulerBuilder {
Iterable<? extends ThreadPoolTaskSchedulerCustomizer> customizers) { Iterable<? extends ThreadPoolTaskSchedulerCustomizer> customizers) {
Assert.notNull(customizers, "Customizers must not be null"); Assert.notNull(customizers, "Customizers must not be null");
return new ThreadPoolTaskSchedulerBuilder(this.poolSize, this.awaitTermination, this.awaitTerminationPeriod, 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));
} }
/** /**

View File

@ -62,6 +62,19 @@ class SimpleAsyncTaskSchedulerBuilderTests {
assertThat(scheduler).extracting("virtualThreadDelegate").isNotNull(); 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 @Test
void customizersWhenCustomizersAreNullShouldThrowException() { void customizersWhenCustomizersAreNullShouldThrowException() {
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
@ -129,17 +142,4 @@ class SimpleAsyncTaskSchedulerBuilderTests {
then(customizer2).should().customize(scheduler); 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);
}
} }

View File

@ -65,6 +65,13 @@ class ThreadPoolTaskSchedulerBuilderTests {
assertThat(scheduler.getThreadNamePrefix()).isEqualTo("test-"); 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 @Test
void customizersWhenCustomizersAreNullShouldThrowException() { void customizersWhenCustomizersAreNullShouldThrowException() {
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
@ -132,11 +139,4 @@ class ThreadPoolTaskSchedulerBuilderTests {
then(customizer2).should().customize(scheduler); 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);
}
} }