Skip to content

Commit

Permalink
Apply awaitTerminationPeriod to SimpleAsyncTaskExecutor
Browse files Browse the repository at this point in the history
Closes gh-38528
  • Loading branch information
mhalbritter committed Nov 30, 2023
1 parent 6cb9af1 commit e454470
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ private SimpleAsyncTaskExecutorBuilder builder() {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ void taskExecutorBuilderShouldApplyCustomSettings() {
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);
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -74,7 +79,7 @@ private SimpleAsyncTaskExecutorBuilder(Boolean virtualThreads, String threadName
*/
public SimpleAsyncTaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, threadNamePrefix, this.concurrencyLimit,
this.taskDecorator, this.customizers);
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
}

/**
Expand All @@ -84,7 +89,7 @@ public SimpleAsyncTaskExecutorBuilder threadNamePrefix(String threadNamePrefix)
*/
public SimpleAsyncTaskExecutorBuilder virtualThreads(Boolean virtualThreads) {
return new SimpleAsyncTaskExecutorBuilder(virtualThreads, this.threadNamePrefix, this.concurrencyLimit,
this.taskDecorator, this.customizers);
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
}

/**
Expand All @@ -94,7 +99,7 @@ public SimpleAsyncTaskExecutorBuilder virtualThreads(Boolean virtualThreads) {
*/
public SimpleAsyncTaskExecutorBuilder concurrencyLimit(Integer concurrencyLimit) {
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix, concurrencyLimit,
this.taskDecorator, this.customizers);
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
}

/**
Expand All @@ -104,7 +109,18 @@ public SimpleAsyncTaskExecutorBuilder concurrencyLimit(Integer concurrencyLimit)
*/
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);
}

/**
Expand Down Expand Up @@ -134,7 +150,7 @@ public SimpleAsyncTaskExecutorBuilder customizers(
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);
}

/**
Expand Down Expand Up @@ -162,7 +178,7 @@ public SimpleAsyncTaskExecutorBuilder additionalCustomizers(
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);
}

/**
Expand Down Expand Up @@ -203,6 +219,7 @@ public <T extends SimpleAsyncTaskExecutor> T configure(T taskExecutor) {
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.task;

import java.time.Duration;
import java.util.Collections;
import java.util.Set;

Expand Down Expand Up @@ -144,4 +145,10 @@ void additionalCustomizersShouldAddToExisting() {
then(customizer2).should().customize(executor);
}

@Test
void taskTerminationTimeoutShouldApply() {
SimpleAsyncTaskExecutor executor = this.builder.taskTerminationTimeout(Duration.ofSeconds(1)).build();
assertThat(executor).extracting("taskTerminationTimeout").isEqualTo(1000L);
}

}

0 comments on commit e454470

Please sign in to comment.