mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 03:43:21 +08:00
Apply awaitTerminationPeriod to SimpleAsyncTaskExecutor
Closes gh-38528
This commit is contained in:
parent
6cb9af11e8
commit
e454470bf9
@ -177,6 +177,8 @@ class TaskExecutorConfigurations {
|
||||
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
|
||||
TaskExecutionProperties.Simple simple = this.properties.getSimple();
|
||||
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
|
||||
Shutdown shutdown = this.properties.getShutdown();
|
||||
builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod());
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
@ -112,10 +112,12 @@ class TaskExecutionAutoConfigurationTests {
|
||||
void simpleAsyncTaskExecutorBuilderShouldReadProperties() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.task.execution.thread-name-prefix=mytest-",
|
||||
"spring.task.execution.simple.concurrency-limit=1")
|
||||
"spring.task.execution.simple.concurrency-limit=1",
|
||||
"spring.task.execution.shutdown.await-termination-period=30s")
|
||||
.run(assertSimpleAsyncTaskExecutor((taskExecutor) -> {
|
||||
assertThat(taskExecutor.getConcurrencyLimit()).isEqualTo(1);
|
||||
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
|
||||
assertThat(taskExecutor).hasFieldOrPropertyWithValue("taskTerminationTimeout", 30000L);
|
||||
}));
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.task;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
@ -54,17 +55,21 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
|
||||
private final Set<SimpleAsyncTaskExecutorCustomizer> customizers;
|
||||
|
||||
private final Duration taskTerminationTimeout;
|
||||
|
||||
public SimpleAsyncTaskExecutorBuilder() {
|
||||
this(null, null, null, null, null);
|
||||
this(null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
private SimpleAsyncTaskExecutorBuilder(Boolean virtualThreads, String threadNamePrefix, Integer concurrencyLimit,
|
||||
TaskDecorator taskDecorator, Set<SimpleAsyncTaskExecutorCustomizer> customizers) {
|
||||
TaskDecorator taskDecorator, Set<SimpleAsyncTaskExecutorCustomizer> customizers,
|
||||
Duration taskTerminationTimeout) {
|
||||
this.virtualThreads = virtualThreads;
|
||||
this.threadNamePrefix = threadNamePrefix;
|
||||
this.concurrencyLimit = concurrencyLimit;
|
||||
this.taskDecorator = taskDecorator;
|
||||
this.customizers = customizers;
|
||||
this.taskTerminationTimeout = taskTerminationTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,7 +79,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, threadNamePrefix, this.concurrencyLimit,
|
||||
this.taskDecorator, this.customizers);
|
||||
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -84,7 +89,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder virtualThreads(Boolean virtualThreads) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
this.taskDecorator, this.customizers);
|
||||
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,7 +99,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder concurrencyLimit(Integer concurrencyLimit) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, concurrencyLimit,
|
||||
this.taskDecorator, this.customizers);
|
||||
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -104,7 +109,18 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder taskDecorator(TaskDecorator taskDecorator) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
taskDecorator, this.customizers);
|
||||
taskDecorator, this.customizers, this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the task termination timeout.
|
||||
* @param taskTerminationTimeout the task termination timeout
|
||||
* @return a new builder instance
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public SimpleAsyncTaskExecutorBuilder taskTerminationTimeout(Duration taskTerminationTimeout) {
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
this.taskDecorator, this.customizers, taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,7 +150,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
|
||||
Assert.notNull(customizers, "Customizers must not be null");
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
this.taskDecorator, append(null, customizers));
|
||||
this.taskDecorator, append(null, customizers), this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -162,7 +178,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
|
||||
Assert.notNull(customizers, "Customizers must not be null");
|
||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
||||
this.taskDecorator, append(this.customizers, customizers));
|
||||
this.taskDecorator, append(this.customizers, customizers), this.taskTerminationTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,6 +219,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||
map.from(this.threadNamePrefix).whenHasText().to(taskExecutor::setThreadNamePrefix);
|
||||
map.from(this.concurrencyLimit).to(taskExecutor::setConcurrencyLimit);
|
||||
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
|
||||
map.from(this.taskTerminationTimeout).as(Duration::toMillis).to(taskExecutor::setTaskTerminationTimeout);
|
||||
if (!CollectionUtils.isEmpty(this.customizers)) {
|
||||
this.customizers.forEach((customizer) -> customizer.customize(taskExecutor));
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.task;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
@ -144,4 +145,10 @@ class SimpleAsyncTaskExecutorBuilderTests {
|
||||
then(customizer2).should().customize(executor);
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskTerminationTimeoutShouldApply() {
|
||||
SimpleAsyncTaskExecutor executor = this.builder.taskTerminationTimeout(Duration.ofSeconds(1)).build();
|
||||
assertThat(executor).extracting("taskTerminationTimeout").isEqualTo(1000L);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user