Polish "Add TaskDecorator support for scheduled tasks"
See gh-43190
This commit is contained in:
parent
f0c5312141
commit
ced7c1617c
|
@ -68,16 +68,16 @@ class TaskSchedulingConfigurations {
|
|||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder(TaskSchedulingProperties properties,
|
||||
ObjectProvider<ThreadPoolTaskSchedulerCustomizer> threadPoolTaskSchedulerCustomizers,
|
||||
ObjectProvider<TaskDecorator> taskDecorator) {
|
||||
ObjectProvider<TaskDecorator> taskDecorator,
|
||||
ObjectProvider<ThreadPoolTaskSchedulerCustomizer> 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 @@ class TaskSchedulingConfigurations {
|
|||
|
||||
private final TaskSchedulingProperties properties;
|
||||
|
||||
private final ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers;
|
||||
|
||||
private final ObjectProvider<TaskDecorator> taskDecorator;
|
||||
|
||||
private final ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers;
|
||||
|
||||
SimpleAsyncTaskSchedulerBuilderConfiguration(TaskSchedulingProperties properties,
|
||||
ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers,
|
||||
ObjectProvider<TaskDecorator> taskDecorator) {
|
||||
ObjectProvider<TaskDecorator> taskDecorator,
|
||||
ObjectProvider<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers) {
|
||||
this.properties = properties;
|
||||
this.taskSchedulerCustomizers = taskSchedulerCustomizers;
|
||||
this.taskDecorator = taskDecorator;
|
||||
this.taskSchedulerCustomizers = taskSchedulerCustomizers;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -117,6 +117,7 @@ class TaskSchedulingConfigurations {
|
|||
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 @@ class TaskSchedulingConfigurations {
|
|||
if (shutdown.isAwaitTermination()) {
|
||||
builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod());
|
||||
}
|
||||
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
void simpleAsyncTaskSchedulerBuilderShouldApplyTaskDecorator() {
|
||||
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
|
||||
void enableSchedulingWithNoTaskExecutorAppliesCustomizers() {
|
||||
this.contextRunner.withPropertyValues("spring.task.scheduling.thread-name-prefix=scheduling-test-")
|
||||
|
|
|
@ -54,11 +54,6 @@ public class SimpleAsyncTaskSchedulerBuilder {
|
|||
|
||||
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() {
|
||||
this(null, null, null, null, null, null);
|
||||
}
|
||||
|
@ -115,6 +110,17 @@ public class SimpleAsyncTaskSchedulerBuilder {
|
|||
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 class SimpleAsyncTaskSchedulerBuilder {
|
|||
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.
|
||||
|
|
|
@ -53,12 +53,6 @@ public class ThreadPoolTaskSchedulerBuilder {
|
|||
|
||||
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() {
|
||||
this(null, null, null, null, null, null);
|
||||
}
|
||||
|
@ -79,18 +73,18 @@ public class ThreadPoolTaskSchedulerBuilder {
|
|||
@Deprecated(since = "3.5.0", forRemoval = true)
|
||||
public ThreadPoolTaskSchedulerBuilder(Integer poolSize, Boolean awaitTermination, Duration awaitTerminationPeriod,
|
||||
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,
|
||||
String threadNamePrefix, Set<ThreadPoolTaskSchedulerCustomizer> taskSchedulerCustomizers,
|
||||
TaskDecorator taskDecorator) {
|
||||
String threadNamePrefix, TaskDecorator taskDecorator,
|
||||
Set<ThreadPoolTaskSchedulerCustomizer> 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 @@ public class ThreadPoolTaskSchedulerBuilder {
|
|||
*/
|
||||
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 class ThreadPoolTaskSchedulerBuilder {
|
|||
*/
|
||||
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 class ThreadPoolTaskSchedulerBuilder {
|
|||
*/
|
||||
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 class ThreadPoolTaskSchedulerBuilder {
|
|||
*/
|
||||
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 class ThreadPoolTaskSchedulerBuilder {
|
|||
*/
|
||||
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 class ThreadPoolTaskSchedulerBuilder {
|
|||
Iterable<? extends ThreadPoolTaskSchedulerCustomizer> 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 class ThreadPoolTaskSchedulerBuilder {
|
|||
Iterable<? extends ThreadPoolTaskSchedulerCustomizer> 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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -62,6 +62,19 @@ class SimpleAsyncTaskSchedulerBuilderTests {
|
|||
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 @@ class SimpleAsyncTaskSchedulerBuilderTests {
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -65,6 +65,13 @@ class ThreadPoolTaskSchedulerBuilderTests {
|
|||
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 @@ class ThreadPoolTaskSchedulerBuilderTests {
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue