Skip to content

Commit d2cbf08

Browse files
committed
Polish "Add support for task executor shutdown related properties"
Closes gh-15951
1 parent 3b47ba2 commit d2cbf08

File tree

5 files changed

+122
-125
lines changed

5 files changed

+122
-125
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25+
import org.springframework.boot.autoconfigure.task.TaskExecutionProperties.Shutdown;
2526
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2627
import org.springframework.boot.task.TaskExecutorBuilder;
2728
import org.springframework.boot.task.TaskExecutorCustomizer;
@@ -74,10 +75,10 @@ public TaskExecutorBuilder taskExecutorBuilder() {
7475
builder = builder.maxPoolSize(pool.getMaxSize());
7576
builder = builder.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout());
7677
builder = builder.keepAlive(pool.getKeepAlive());
78+
Shutdown shutdown = this.properties.getShutdown();
79+
builder = builder.awaitTermination(shutdown.isAwaitTermination());
80+
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
7781
builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix());
78-
builder = builder.awaitTermination(this.properties.getAwaitTermination());
79-
builder = builder.waitForTasksToCompleteOnShutdown(
80-
this.properties.isWaitForTasksToCompleteOnShutdown());
8182
builder = builder.customizers(this.taskExecutorCustomizers);
8283
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
8384
return builder;

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

+37-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,10 +17,8 @@
1717
package org.springframework.boot.autoconfigure.task;
1818

1919
import java.time.Duration;
20-
import java.time.temporal.ChronoUnit;
2120

2221
import org.springframework.boot.context.properties.ConfigurationProperties;
23-
import org.springframework.boot.convert.DurationUnit;
2422

2523
/**
2624
* Configuration properties for task execution.
@@ -34,29 +32,21 @@ public class TaskExecutionProperties {
3432

3533
private final Pool pool = new Pool();
3634

35+
private final Shutdown shutdown = new Shutdown();
36+
3737
/**
3838
* Prefix to use for the names of newly created threads.
3939
*/
4040
private String threadNamePrefix = "task-";
4141

42-
/**
43-
* Maximum number of time that the executor is supposed to block on shutdown waiting
44-
* for remaining tasks to complete. This is particularly useful if your remaining
45-
* tasks are likely to need access to other resources that are also managed by the
46-
* container. If a duration suffix is not specified, seconds will be used.
47-
*/
48-
@DurationUnit(ChronoUnit.SECONDS)
49-
private Duration awaitTermination;
50-
51-
/**
52-
* Whether the executor should wait for scheduled tasks to complete on shutdown.
53-
*/
54-
private boolean waitForTasksToCompleteOnShutdown = false;
55-
5642
public Pool getPool() {
5743
return this.pool;
5844
}
5945

46+
public Shutdown getShutdown() {
47+
return this.shutdown;
48+
}
49+
6050
public String getThreadNamePrefix() {
6151
return this.threadNamePrefix;
6252
}
@@ -65,23 +55,6 @@ public void setThreadNamePrefix(String threadNamePrefix) {
6555
this.threadNamePrefix = threadNamePrefix;
6656
}
6757

68-
public Duration getAwaitTermination() {
69-
return this.awaitTermination;
70-
}
71-
72-
public void setAwaitTermination(Duration awaitTermination) {
73-
this.awaitTermination = awaitTermination;
74-
}
75-
76-
public boolean isWaitForTasksToCompleteOnShutdown() {
77-
return this.waitForTasksToCompleteOnShutdown;
78-
}
79-
80-
public void setWaitForTasksToCompleteOnShutdown(
81-
boolean waitForTasksToCompleteOnShutdown) {
82-
this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown;
83-
}
84-
8558
public static class Pool {
8659

8760
/**
@@ -155,4 +128,34 @@ public void setKeepAlive(Duration keepAlive) {
155128

156129
}
157130

131+
public static class Shutdown {
132+
133+
/**
134+
* Whether the executor should wait for scheduled tasks to complete on shutdown.
135+
*/
136+
private boolean awaitTermination;
137+
138+
/**
139+
* Maximum time the executor should wait for remaining tasks to complete.
140+
*/
141+
private Duration awaitTerminationPeriod;
142+
143+
public boolean isAwaitTermination() {
144+
return this.awaitTermination;
145+
}
146+
147+
public void setAwaitTermination(boolean awaitTermination) {
148+
this.awaitTermination = awaitTermination;
149+
}
150+
151+
public Duration getAwaitTerminationPeriod() {
152+
return this.awaitTerminationPeriod;
153+
}
154+
155+
public void setAwaitTerminationPeriod(Duration awaitTerminationPeriod) {
156+
this.awaitTerminationPeriod = awaitTerminationPeriod;
157+
}
158+
159+
}
160+
158161
}

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ public class TaskExecutionAutoConfigurationTests {
6363

6464
@Test
6565
public void taskExecutorBuilderShouldApplyCustomSettings() {
66-
this.contextRunner.withPropertyValues(
67-
"spring.task.execution.pool.queue-capacity=10",
68-
"spring.task.execution.pool.core-size=2",
69-
"spring.task.execution.pool.max-size=4",
70-
"spring.task.execution.pool.allow-core-thread-timeout=true",
71-
"spring.task.execution.pool.keep-alive=5s",
72-
"spring.task.execution.thread-name-prefix=mytest-",
73-
"spring.task.execution.await-termination=30s",
74-
"spring.task.execution.wait-for-tasks-to-complete-on-shutdown=true")
66+
this.contextRunner
67+
.withPropertyValues("spring.task.execution.pool.queue-capacity=10",
68+
"spring.task.execution.pool.core-size=2",
69+
"spring.task.execution.pool.max-size=4",
70+
"spring.task.execution.pool.allow-core-thread-timeout=true",
71+
"spring.task.execution.pool.keep-alive=5s",
72+
"spring.task.execution.shutdown.await-termination=true",
73+
"spring.task.execution.shutdown.await-termination-period=30s",
74+
"spring.task.execution.thread-name-prefix=mytest-")
7575
.run(assertTaskExecutor((taskExecutor) -> {
7676
assertThat(taskExecutor).hasFieldOrPropertyWithValue("queueCapacity",
7777
10);
@@ -80,11 +80,11 @@ public void taskExecutorBuilderShouldApplyCustomSettings() {
8080
assertThat(taskExecutor)
8181
.hasFieldOrPropertyWithValue("allowCoreThreadTimeOut", true);
8282
assertThat(taskExecutor.getKeepAliveSeconds()).isEqualTo(5);
83-
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
84-
assertThat(taskExecutor)
85-
.hasFieldOrPropertyWithValue("awaitTerminationSeconds", 30);
8683
assertThat(taskExecutor).hasFieldOrPropertyWithValue(
8784
"waitForTasksToCompleteOnShutdown", true);
85+
assertThat(taskExecutor)
86+
.hasFieldOrPropertyWithValue("awaitTerminationSeconds", 30);
87+
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
8888
}));
8989
}
9090

0 commit comments

Comments
 (0)