Skip to content

Commit 6744cc2

Browse files
committed
Apply awaitTerminationPeriod to SimpleAsyncTaskScheduler
Closes gh-38530
1 parent e454470 commit 6744cc2

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingConfigurations.java

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ private SimpleAsyncTaskSchedulerBuilder builder() {
152152
builder = builder.customizers(this.taskSchedulerCustomizers.orderedStream()::iterator);
153153
TaskSchedulingProperties.Simple simple = this.properties.getSimple();
154154
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
155+
TaskSchedulingProperties.Shutdown shutdown = this.properties.getShutdown();
156+
if (shutdown.isAwaitTermination()) {
157+
builder = builder.taskTerminationTimeout(shutdown.getAwaitTerminationPeriod());
158+
}
155159
return builder;
156160
}
157161

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfigurationTests.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,16 @@ void enableSchedulingWithNoTaskExecutorAutoConfiguresOne() {
119119
void simpleAsyncTaskSchedulerBuilderShouldReadProperties() {
120120
this.contextRunner
121121
.withPropertyValues("spring.task.scheduling.simple.concurrency-limit=1",
122-
"spring.task.scheduling.thread-name-prefix=scheduling-test-")
122+
"spring.task.scheduling.thread-name-prefix=scheduling-test-",
123+
"spring.task.scheduling.shutdown.await-termination=true",
124+
"spring.task.scheduling.shutdown.await-termination-period=30s")
123125
.withUserConfiguration(SchedulingConfiguration.class)
124126
.run((context) -> {
125127
assertThat(context).hasSingleBean(SimpleAsyncTaskSchedulerBuilder.class);
126128
SimpleAsyncTaskSchedulerBuilder builder = context.getBean(SimpleAsyncTaskSchedulerBuilder.class);
127129
assertThat(builder).hasFieldOrPropertyWithValue("threadNamePrefix", "scheduling-test-");
128130
assertThat(builder).hasFieldOrPropertyWithValue("concurrencyLimit", 1);
131+
assertThat(builder).hasFieldOrPropertyWithValue("taskTerminationTimeout", Duration.ofSeconds(30));
129132
});
130133
}
131134

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/SimpleAsyncTaskSchedulerBuilder.java

+23-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.task;
1818

19+
import java.time.Duration;
1920
import java.util.Arrays;
2021
import java.util.Collections;
2122
import java.util.LinkedHashSet;
@@ -48,16 +49,19 @@ public class SimpleAsyncTaskSchedulerBuilder {
4849

4950
private final Set<SimpleAsyncTaskSchedulerCustomizer> customizers;
5051

52+
private final Duration taskTerminationTimeout;
53+
5154
public SimpleAsyncTaskSchedulerBuilder() {
52-
this(null, null, null, null);
55+
this(null, null, null, null, null);
5356
}
5457

5558
private SimpleAsyncTaskSchedulerBuilder(String threadNamePrefix, Integer concurrencyLimit, Boolean virtualThreads,
56-
Set<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers) {
59+
Set<SimpleAsyncTaskSchedulerCustomizer> taskSchedulerCustomizers, Duration taskTerminationTimeout) {
5760
this.threadNamePrefix = threadNamePrefix;
5861
this.concurrencyLimit = concurrencyLimit;
5962
this.virtualThreads = virtualThreads;
6063
this.customizers = taskSchedulerCustomizers;
64+
this.taskTerminationTimeout = taskTerminationTimeout;
6165
}
6266

6367
/**
@@ -67,7 +71,7 @@ private SimpleAsyncTaskSchedulerBuilder(String threadNamePrefix, Integer concurr
6771
*/
6872
public SimpleAsyncTaskSchedulerBuilder threadNamePrefix(String threadNamePrefix) {
6973
return new SimpleAsyncTaskSchedulerBuilder(threadNamePrefix, this.concurrencyLimit, this.virtualThreads,
70-
this.customizers);
74+
this.customizers, this.taskTerminationTimeout);
7175
}
7276

7377
/**
@@ -77,7 +81,7 @@ public SimpleAsyncTaskSchedulerBuilder threadNamePrefix(String threadNamePrefix)
7781
*/
7882
public SimpleAsyncTaskSchedulerBuilder concurrencyLimit(Integer concurrencyLimit) {
7983
return new SimpleAsyncTaskSchedulerBuilder(this.threadNamePrefix, concurrencyLimit, this.virtualThreads,
80-
this.customizers);
84+
this.customizers, this.taskTerminationTimeout);
8185
}
8286

8387
/**
@@ -87,7 +91,18 @@ public SimpleAsyncTaskSchedulerBuilder concurrencyLimit(Integer concurrencyLimit
8791
*/
8892
public SimpleAsyncTaskSchedulerBuilder virtualThreads(Boolean virtualThreads) {
8993
return new SimpleAsyncTaskSchedulerBuilder(this.threadNamePrefix, this.concurrencyLimit, virtualThreads,
90-
this.customizers);
94+
this.customizers, this.taskTerminationTimeout);
95+
}
96+
97+
/**
98+
* Set the task termination timeout.
99+
* @param taskTerminationTimeout the task termination timeout
100+
* @return a new builder instance
101+
* @since 3.2.1
102+
*/
103+
public SimpleAsyncTaskSchedulerBuilder taskTerminationTimeout(Duration taskTerminationTimeout) {
104+
return new SimpleAsyncTaskSchedulerBuilder(this.threadNamePrefix, this.concurrencyLimit, this.virtualThreads,
105+
this.customizers, taskTerminationTimeout);
91106
}
92107

93108
/**
@@ -117,7 +132,7 @@ public SimpleAsyncTaskSchedulerBuilder customizers(
117132
Iterable<? extends SimpleAsyncTaskSchedulerCustomizer> customizers) {
118133
Assert.notNull(customizers, "Customizers must not be null");
119134
return new SimpleAsyncTaskSchedulerBuilder(this.threadNamePrefix, this.concurrencyLimit, this.virtualThreads,
120-
append(null, customizers));
135+
append(null, customizers), this.taskTerminationTimeout);
121136
}
122137

123138
/**
@@ -145,7 +160,7 @@ public SimpleAsyncTaskSchedulerBuilder additionalCustomizers(
145160
Iterable<? extends SimpleAsyncTaskSchedulerCustomizer> customizers) {
146161
Assert.notNull(customizers, "Customizers must not be null");
147162
return new SimpleAsyncTaskSchedulerBuilder(this.threadNamePrefix, this.concurrencyLimit, this.virtualThreads,
148-
append(this.customizers, customizers));
163+
append(this.customizers, customizers), this.taskTerminationTimeout);
149164
}
150165

151166
/**
@@ -171,6 +186,7 @@ public <T extends SimpleAsyncTaskScheduler> T configure(T taskScheduler) {
171186
map.from(this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix);
172187
map.from(this.concurrencyLimit).to(taskScheduler::setConcurrencyLimit);
173188
map.from(this.virtualThreads).to(taskScheduler::setVirtualThreads);
189+
map.from(this.taskTerminationTimeout).as(Duration::toMillis).to(taskScheduler::setTaskTerminationTimeout);
174190
if (!CollectionUtils.isEmpty(this.customizers)) {
175191
this.customizers.forEach((customizer) -> customizer.customize(taskScheduler));
176192
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/SimpleAsyncTaskSchedulerBuilderTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.task;
1818

19+
import java.time.Duration;
1920
import java.util.Collections;
2021
import java.util.Set;
2122

@@ -127,4 +128,10 @@ void additionalCustomizersShouldAddToExisting() {
127128
then(customizer2).should().customize(scheduler);
128129
}
129130

131+
@Test
132+
void taskTerminationTimeoutShouldApply() {
133+
SimpleAsyncTaskScheduler scheduler = this.builder.taskTerminationTimeout(Duration.ofSeconds(1)).build();
134+
assertThat(scheduler).extracting("taskTerminationTimeout").isEqualTo(1000L);
135+
}
136+
130137
}

0 commit comments

Comments
 (0)