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());
|
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
|
||||||
TaskExecutionProperties.Simple simple = this.properties.getSimple();
|
TaskExecutionProperties.Simple simple = this.properties.getSimple();
|
||||||
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
|
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
|
||||||
|
Shutdown shutdown = this.properties.getShutdown();
|
||||||
|
builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod());
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,10 +112,12 @@ class TaskExecutionAutoConfigurationTests {
|
||||||
void simpleAsyncTaskExecutorBuilderShouldReadProperties() {
|
void simpleAsyncTaskExecutorBuilderShouldReadProperties() {
|
||||||
this.contextRunner
|
this.contextRunner
|
||||||
.withPropertyValues("spring.task.execution.thread-name-prefix=mytest-",
|
.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) -> {
|
.run(assertSimpleAsyncTaskExecutor((taskExecutor) -> {
|
||||||
assertThat(taskExecutor.getConcurrencyLimit()).isEqualTo(1);
|
assertThat(taskExecutor.getConcurrencyLimit()).isEqualTo(1);
|
||||||
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
|
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
|
||||||
|
assertThat(taskExecutor).hasFieldOrPropertyWithValue("taskTerminationTimeout", 30000L);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.task;
|
package org.springframework.boot.task;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
|
@ -54,17 +55,21 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||||
|
|
||||||
private final Set<SimpleAsyncTaskExecutorCustomizer> customizers;
|
private final Set<SimpleAsyncTaskExecutorCustomizer> customizers;
|
||||||
|
|
||||||
|
private final Duration taskTerminationTimeout;
|
||||||
|
|
||||||
public SimpleAsyncTaskExecutorBuilder() {
|
public SimpleAsyncTaskExecutorBuilder() {
|
||||||
this(null, null, null, null, null);
|
this(null, null, null, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private SimpleAsyncTaskExecutorBuilder(Boolean virtualThreads, String threadNamePrefix, Integer concurrencyLimit,
|
private SimpleAsyncTaskExecutorBuilder(Boolean virtualThreads, String threadNamePrefix, Integer concurrencyLimit,
|
||||||
TaskDecorator taskDecorator, Set<SimpleAsyncTaskExecutorCustomizer> customizers) {
|
TaskDecorator taskDecorator, Set<SimpleAsyncTaskExecutorCustomizer> customizers,
|
||||||
|
Duration taskTerminationTimeout) {
|
||||||
this.virtualThreads = virtualThreads;
|
this.virtualThreads = virtualThreads;
|
||||||
this.threadNamePrefix = threadNamePrefix;
|
this.threadNamePrefix = threadNamePrefix;
|
||||||
this.concurrencyLimit = concurrencyLimit;
|
this.concurrencyLimit = concurrencyLimit;
|
||||||
this.taskDecorator = taskDecorator;
|
this.taskDecorator = taskDecorator;
|
||||||
this.customizers = customizers;
|
this.customizers = customizers;
|
||||||
|
this.taskTerminationTimeout = taskTerminationTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,7 +79,7 @@ public class SimpleAsyncTaskExecutorBuilder {
|
||||||
*/
|
*/
|
||||||
public SimpleAsyncTaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
|
public SimpleAsyncTaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
|
||||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, threadNamePrefix, this.concurrencyLimit,
|
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) {
|
public SimpleAsyncTaskExecutorBuilder virtualThreads(Boolean virtualThreads) {
|
||||||
return new SimpleAsyncTaskExecutorBuilder(virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
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) {
|
public SimpleAsyncTaskExecutorBuilder concurrencyLimit(Integer concurrencyLimit) {
|
||||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, 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) {
|
public SimpleAsyncTaskExecutorBuilder taskDecorator(TaskDecorator taskDecorator) {
|
||||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
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) {
|
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
|
||||||
Assert.notNull(customizers, "Customizers must not be null");
|
Assert.notNull(customizers, "Customizers must not be null");
|
||||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
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) {
|
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
|
||||||
Assert.notNull(customizers, "Customizers must not be null");
|
Assert.notNull(customizers, "Customizers must not be null");
|
||||||
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
|
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.threadNamePrefix).whenHasText().to(taskExecutor::setThreadNamePrefix);
|
||||||
map.from(this.concurrencyLimit).to(taskExecutor::setConcurrencyLimit);
|
map.from(this.concurrencyLimit).to(taskExecutor::setConcurrencyLimit);
|
||||||
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
|
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
|
||||||
|
map.from(this.taskTerminationTimeout).as(Duration::toMillis).to(taskExecutor::setTaskTerminationTimeout);
|
||||||
if (!CollectionUtils.isEmpty(this.customizers)) {
|
if (!CollectionUtils.isEmpty(this.customizers)) {
|
||||||
this.customizers.forEach((customizer) -> customizer.customize(taskExecutor));
|
this.customizers.forEach((customizer) -> customizer.customize(taskExecutor));
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.task;
|
package org.springframework.boot.task;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -144,4 +145,10 @@ class SimpleAsyncTaskExecutorBuilderTests {
|
||||||
then(customizer2).should().customize(executor);
|
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