Skip to content

Commit 35fc929

Browse files
committed
Merge pull request #15951 from filiphr
* pr/15951: Add support for task scheduling shutdown related properties Polish "Add support for task executor shutdown related properties" Add support for task executor shutdown related properties
2 parents 9540905 + fa49dfc commit 35fc929

File tree

10 files changed

+253
-28
lines changed

10 files changed

+253
-28
lines changed

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

Lines changed: 4 additions & 0 deletions
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,6 +75,9 @@ 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());
7882
builder = builder.customizers(this.taskExecutorCustomizers);
7983
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());

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

Lines changed: 38 additions & 1 deletion
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.
@@ -24,13 +24,16 @@
2424
* Configuration properties for task execution.
2525
*
2626
* @author Stephane Nicoll
27+
* @author Filip Hrisafov
2728
* @since 2.1.0
2829
*/
2930
@ConfigurationProperties("spring.task.execution")
3031
public class TaskExecutionProperties {
3132

3233
private final Pool pool = new Pool();
3334

35+
private final Shutdown shutdown = new Shutdown();
36+
3437
/**
3538
* Prefix to use for the names of newly created threads.
3639
*/
@@ -40,6 +43,10 @@ public Pool getPool() {
4043
return this.pool;
4144
}
4245

46+
public Shutdown getShutdown() {
47+
return this.shutdown;
48+
}
49+
4350
public String getThreadNamePrefix() {
4451
return this.threadNamePrefix;
4552
}
@@ -121,4 +128,34 @@ public void setKeepAlive(Duration keepAlive) {
121128

122129
}
123130

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+
124161
}

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

Lines changed: 5 additions & 1 deletion
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.
@@ -23,6 +23,7 @@
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.autoconfigure.task.TaskSchedulingProperties.Shutdown;
2627
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2728
import org.springframework.boot.task.TaskSchedulerBuilder;
2829
import org.springframework.boot.task.TaskSchedulerCustomizer;
@@ -58,6 +59,9 @@ public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties proper
5859
ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
5960
TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
6061
builder = builder.poolSize(properties.getPool().getSize());
62+
Shutdown shutdown = properties.getShutdown();
63+
builder = builder.awaitTermination(shutdown.isAwaitTermination());
64+
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
6165
builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
6266
builder = builder.customizers(taskSchedulerCustomizers);
6367
return builder;

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

Lines changed: 39 additions & 1 deletion
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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.task;
1818

19+
import java.time.Duration;
20+
1921
import org.springframework.boot.context.properties.ConfigurationProperties;
2022

2123
/**
@@ -29,6 +31,8 @@ public class TaskSchedulingProperties {
2931

3032
private final Pool pool = new Pool();
3133

34+
private final Shutdown shutdown = new Shutdown();
35+
3236
/**
3337
* Prefix to use for the names of newly created threads.
3438
*/
@@ -38,6 +42,10 @@ public Pool getPool() {
3842
return this.pool;
3943
}
4044

45+
public Shutdown getShutdown() {
46+
return this.shutdown;
47+
}
48+
4149
public String getThreadNamePrefix() {
4250
return this.threadNamePrefix;
4351
}
@@ -63,4 +71,34 @@ public void setSize(int size) {
6371

6472
}
6573

74+
public static class Shutdown {
75+
76+
/**
77+
* Whether the executor should wait for scheduled tasks to complete on shutdown.
78+
*/
79+
private boolean awaitTermination;
80+
81+
/**
82+
* Maximum time the executor should wait for remaining tasks to complete.
83+
*/
84+
private Duration awaitTerminationPeriod;
85+
86+
public boolean isAwaitTermination() {
87+
return this.awaitTermination;
88+
}
89+
90+
public void setAwaitTermination(boolean awaitTermination) {
91+
this.awaitTermination = awaitTermination;
92+
}
93+
94+
public Duration getAwaitTerminationPeriod() {
95+
return this.awaitTerminationPeriod;
96+
}
97+
98+
public void setAwaitTerminationPeriod(Duration awaitTerminationPeriod) {
99+
this.awaitTerminationPeriod = awaitTerminationPeriod;
100+
}
101+
102+
}
103+
66104
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public void taskExecutorBuilderShouldApplyCustomSettings() {
6969
"spring.task.execution.pool.max-size=4",
7070
"spring.task.execution.pool.allow-core-thread-timeout=true",
7171
"spring.task.execution.pool.keep-alive=5s",
72+
"spring.task.execution.shutdown.await-termination=true",
73+
"spring.task.execution.shutdown.await-termination-period=30s",
7274
"spring.task.execution.thread-name-prefix=mytest-")
7375
.run(assertTaskExecutor((taskExecutor) -> {
7476
assertThat(taskExecutor).hasFieldOrPropertyWithValue("queueCapacity",
@@ -78,6 +80,10 @@ public void taskExecutorBuilderShouldApplyCustomSettings() {
7880
assertThat(taskExecutor)
7981
.hasFieldOrPropertyWithValue("allowCoreThreadTimeOut", true);
8082
assertThat(taskExecutor.getKeepAliveSeconds()).isEqualTo(5);
83+
assertThat(taskExecutor).hasFieldOrPropertyWithValue(
84+
"waitForTasksToCompleteOnShutdown", true);
85+
assertThat(taskExecutor)
86+
.hasFieldOrPropertyWithValue("awaitTerminationSeconds", 30);
8187
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
8288
}));
8389
}

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

Lines changed: 8 additions & 1 deletion
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.
@@ -59,11 +59,18 @@ public void noSchedulingDoesNotExposeTaskScheduler() {
5959
public void enableSchedulingWithNoTaskExecutorAutoConfiguresOne() {
6060
this.contextRunner
6161
.withPropertyValues(
62+
"spring.task.scheduling.shutdown.await-termination=true",
63+
"spring.task.scheduling.shutdown.await-termination-period=30s",
6264
"spring.task.scheduling.thread-name-prefix=scheduling-test-")
6365
.withUserConfiguration(SchedulingConfiguration.class).run((context) -> {
6466
assertThat(context).hasSingleBean(TaskExecutor.class);
67+
TaskExecutor taskExecutor = context.getBean(TaskExecutor.class);
6568
TestBean bean = context.getBean(TestBean.class);
6669
Thread.sleep(15);
70+
assertThat(taskExecutor).hasFieldOrPropertyWithValue(
71+
"waitForTasksToCompleteOnShutdown", true);
72+
assertThat(taskExecutor)
73+
.hasFieldOrPropertyWithValue("awaitTerminationSeconds", 30);
6774
assertThat(bean.threadNames)
6875
.allMatch((name) -> name.contains("scheduling-test-"));
6976
});

0 commit comments

Comments
 (0)