Skip to content

Commit 9530569

Browse files
quaffsnicoll
authored andcommitted
Add support for SimpleTaskExecutor#cancel-remaining-tasks-on-close
See spring-projects/spring-framework#35372 See gh-47244 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent bc60749 commit 9530569

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public void setThreadNamePrefix(String threadNamePrefix) {
7979

8080
public static class Simple {
8181

82+
/**
83+
* Whether to cancel remaining tasks on close.
84+
*/
85+
private boolean cancelRemainingTasksOnClose;
86+
8287
/**
8388
* Whether to reject tasks when the concurrency limit has been reached.
8489
*/
@@ -90,6 +95,14 @@ public static class Simple {
9095
*/
9196
private @Nullable Integer concurrencyLimit;
9297

98+
public boolean isCancelRemainingTasksOnClose() {
99+
return this.cancelRemainingTasksOnClose;
100+
}
101+
102+
public void setCancelRemainingTasksOnClose(boolean cancelRemainingTasksOnClose) {
103+
this.cancelRemainingTasksOnClose = cancelRemainingTasksOnClose;
104+
}
105+
93106
public boolean isRejectTasksWhenLimitReached() {
94107
return this.rejectTasksWhenLimitReached;
95108
}

core/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutorConfigurations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ private SimpleAsyncTaskExecutorBuilder builder() {
148148
builder = builder.customizers(this.taskExecutorCustomizers.orderedStream()::iterator);
149149
builder = builder.taskDecorator(getTaskDecorator(this.taskDecorator));
150150
TaskExecutionProperties.Simple simple = this.properties.getSimple();
151+
builder = builder.cancelRemainingTasksOnClose(simple.isCancelRemainingTasksOnClose());
151152
builder = builder.rejectTasksWhenLimitReached(simple.isRejectTasksWhenLimitReached());
152153
builder = builder.concurrencyLimit(simple.getConcurrencyLimit());
153154
TaskExecutionProperties.Shutdown shutdown = this.properties.getShutdown();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ void shouldSupplyBeans() {
8383
void simpleAsyncTaskExecutorBuilderShouldReadProperties() {
8484
this.contextRunner
8585
.withPropertyValues("spring.task.execution.thread-name-prefix=mytest-",
86+
"spring.task.execution.simple.cancel-remaining-tasks-on-close=true",
8687
"spring.task.execution.simple.reject-tasks-when-limit-reached=true",
8788
"spring.task.execution.simple.concurrency-limit=1",
8889
"spring.task.execution.shutdown.await-termination=true",
8990
"spring.task.execution.shutdown.await-termination-period=30s")
9091
.run(assertSimpleAsyncTaskExecutor((taskExecutor) -> {
92+
assertThat(taskExecutor).hasFieldOrPropertyWithValue("cancelRemainingTasksOnClose", true);
9193
assertThat(taskExecutor).hasFieldOrPropertyWithValue("rejectTasksWhenLimitReached", true);
9294
assertThat(taskExecutor.getConcurrencyLimit()).isEqualTo(1);
9395
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");

core/spring-boot/src/main/java/org/springframework/boot/task/SimpleAsyncTaskExecutorBuilder.java

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public class SimpleAsyncTaskExecutorBuilder {
5252

5353
private final @Nullable String threadNamePrefix;
5454

55+
private final boolean cancelRemainingTasksOnClose;
56+
5557
private final boolean rejectTasksWhenLimitReached;
5658

5759
private final @Nullable Integer concurrencyLimit;
@@ -63,15 +65,16 @@ public class SimpleAsyncTaskExecutorBuilder {
6365
private final @Nullable Duration taskTerminationTimeout;
6466

6567
public SimpleAsyncTaskExecutorBuilder() {
66-
this(null, null, false, null, null, null, null);
68+
this(null, null, false, false, null, null, null, null);
6769
}
6870

6971
private SimpleAsyncTaskExecutorBuilder(@Nullable Boolean virtualThreads, @Nullable String threadNamePrefix,
70-
boolean rejectTasksWhenLimitReached, @Nullable Integer concurrencyLimit,
71-
@Nullable TaskDecorator taskDecorator, @Nullable Set<SimpleAsyncTaskExecutorCustomizer> customizers,
72-
@Nullable Duration taskTerminationTimeout) {
72+
boolean cancelRemainingTasksOnClose, boolean rejectTasksWhenLimitReached,
73+
@Nullable Integer concurrencyLimit, @Nullable TaskDecorator taskDecorator,
74+
@Nullable Set<SimpleAsyncTaskExecutorCustomizer> customizers, @Nullable Duration taskTerminationTimeout) {
7375
this.virtualThreads = virtualThreads;
7476
this.threadNamePrefix = threadNamePrefix;
77+
this.cancelRemainingTasksOnClose = cancelRemainingTasksOnClose;
7578
this.rejectTasksWhenLimitReached = rejectTasksWhenLimitReached;
7679
this.concurrencyLimit = concurrencyLimit;
7780
this.taskDecorator = taskDecorator;
@@ -86,8 +89,8 @@ private SimpleAsyncTaskExecutorBuilder(@Nullable Boolean virtualThreads, @Nullab
8689
*/
8790
public SimpleAsyncTaskExecutorBuilder threadNamePrefix(@Nullable String threadNamePrefix) {
8891
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, threadNamePrefix,
89-
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
90-
this.taskTerminationTimeout);
92+
this.cancelRemainingTasksOnClose, this.rejectTasksWhenLimitReached, this.concurrencyLimit,
93+
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
9194
}
9295

9396
/**
@@ -97,8 +100,26 @@ public SimpleAsyncTaskExecutorBuilder threadNamePrefix(@Nullable String threadNa
97100
*/
98101
public SimpleAsyncTaskExecutorBuilder virtualThreads(@Nullable Boolean virtualThreads) {
99102
return new SimpleAsyncTaskExecutorBuilder(virtualThreads, this.threadNamePrefix,
100-
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
101-
this.taskTerminationTimeout);
103+
this.cancelRemainingTasksOnClose, this.rejectTasksWhenLimitReached, this.concurrencyLimit,
104+
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
105+
}
106+
107+
/**
108+
* Set whether to cancel remaining tasks on close. By default {@code false} not
109+
* tracking active threads at all or just interrupting any remaining threads that
110+
* still have not finished after the specified {@link #taskTerminationTimeout
111+
* taskTerminationTimeout}. Switch this to {@code true} for immediate interruption on
112+
* close, either in combination with a subsequent termination timeout or without any
113+
* waiting at all, depending on whether a {@code taskTerminationTimeout} has been
114+
* specified as well.
115+
* @param cancelRemainingTasksOnClose whether to cancel remaining tasks on close
116+
* @return a new builder instance
117+
* @since 4.0.0
118+
*/
119+
public SimpleAsyncTaskExecutorBuilder cancelRemainingTasksOnClose(boolean cancelRemainingTasksOnClose) {
120+
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
121+
cancelRemainingTasksOnClose, this.rejectTasksWhenLimitReached, this.concurrencyLimit,
122+
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
102123
}
103124

104125
/**
@@ -112,8 +133,8 @@ public SimpleAsyncTaskExecutorBuilder virtualThreads(@Nullable Boolean virtualTh
112133
*/
113134
public SimpleAsyncTaskExecutorBuilder rejectTasksWhenLimitReached(boolean rejectTasksWhenLimitReached) {
114135
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
115-
rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
116-
this.taskTerminationTimeout);
136+
this.cancelRemainingTasksOnClose, rejectTasksWhenLimitReached, this.concurrencyLimit,
137+
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
117138
}
118139

119140
/**
@@ -123,8 +144,8 @@ public SimpleAsyncTaskExecutorBuilder rejectTasksWhenLimitReached(boolean reject
123144
*/
124145
public SimpleAsyncTaskExecutorBuilder concurrencyLimit(@Nullable Integer concurrencyLimit) {
125146
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
126-
this.rejectTasksWhenLimitReached, concurrencyLimit, this.taskDecorator, this.customizers,
127-
this.taskTerminationTimeout);
147+
this.cancelRemainingTasksOnClose, this.rejectTasksWhenLimitReached, concurrencyLimit,
148+
this.taskDecorator, this.customizers, this.taskTerminationTimeout);
128149
}
129150

130151
/**
@@ -134,8 +155,8 @@ public SimpleAsyncTaskExecutorBuilder concurrencyLimit(@Nullable Integer concurr
134155
*/
135156
public SimpleAsyncTaskExecutorBuilder taskDecorator(@Nullable TaskDecorator taskDecorator) {
136157
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
137-
this.rejectTasksWhenLimitReached, this.concurrencyLimit, taskDecorator, this.customizers,
138-
this.taskTerminationTimeout);
158+
this.cancelRemainingTasksOnClose, this.rejectTasksWhenLimitReached, this.concurrencyLimit,
159+
taskDecorator, this.customizers, this.taskTerminationTimeout);
139160
}
140161

141162
/**
@@ -146,8 +167,8 @@ public SimpleAsyncTaskExecutorBuilder taskDecorator(@Nullable TaskDecorator task
146167
*/
147168
public SimpleAsyncTaskExecutorBuilder taskTerminationTimeout(@Nullable Duration taskTerminationTimeout) {
148169
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
149-
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, this.customizers,
150-
taskTerminationTimeout);
170+
this.cancelRemainingTasksOnClose, this.rejectTasksWhenLimitReached, this.concurrencyLimit,
171+
this.taskDecorator, this.customizers, taskTerminationTimeout);
151172
}
152173

153174
/**
@@ -177,8 +198,8 @@ public SimpleAsyncTaskExecutorBuilder customizers(
177198
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
178199
Assert.notNull(customizers, "'customizers' must not be null");
179200
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
180-
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator, append(null, customizers),
181-
this.taskTerminationTimeout);
201+
this.cancelRemainingTasksOnClose, this.rejectTasksWhenLimitReached, this.concurrencyLimit,
202+
this.taskDecorator, append(null, customizers), this.taskTerminationTimeout);
182203
}
183204

184205
/**
@@ -206,8 +227,8 @@ public SimpleAsyncTaskExecutorBuilder additionalCustomizers(
206227
Iterable<? extends SimpleAsyncTaskExecutorCustomizer> customizers) {
207228
Assert.notNull(customizers, "'customizers' must not be null");
208229
return new SimpleAsyncTaskExecutorBuilder(this.virtualThreads, this.threadNamePrefix,
209-
this.rejectTasksWhenLimitReached, this.concurrencyLimit, this.taskDecorator,
210-
append(this.customizers, customizers), this.taskTerminationTimeout);
230+
this.cancelRemainingTasksOnClose, this.rejectTasksWhenLimitReached, this.concurrencyLimit,
231+
this.taskDecorator, append(this.customizers, customizers), this.taskTerminationTimeout);
211232
}
212233

213234
/**
@@ -246,6 +267,7 @@ public <T extends SimpleAsyncTaskExecutor> T configure(T taskExecutor) {
246267
PropertyMapper map = PropertyMapper.get();
247268
map.from(this.virtualThreads).to(taskExecutor::setVirtualThreads);
248269
map.from(this.threadNamePrefix).whenHasText().to(taskExecutor::setThreadNamePrefix);
270+
map.from(this.cancelRemainingTasksOnClose).to(taskExecutor::setCancelRemainingTasksOnClose);
249271
map.from(this.rejectTasksWhenLimitReached).to(taskExecutor::setRejectTasksWhenLimitReached);
250272
map.from(this.concurrencyLimit).to(taskExecutor::setConcurrencyLimit);
251273
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);

core/spring-boot/src/test/java/org/springframework/boot/task/SimpleAsyncTaskExecutorBuilderTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ void virtualThreadsShouldApply() {
5959
SimpleAsyncTaskExecutorAssert.assertThat(executor).usesVirtualThreads();
6060
}
6161

62+
@Test
63+
void cancelRemainingTasksOnCloseShouldApply() {
64+
SimpleAsyncTaskExecutor executor = this.builder.cancelRemainingTasksOnClose(true).build();
65+
assertThat(executor).extracting("cancelRemainingTasksOnClose").isEqualTo(true);
66+
}
67+
6268
@Test
6369
void rejectTasksWhenLimitReachedShouldApply() {
6470
SimpleAsyncTaskExecutor executor = this.builder.rejectTasksWhenLimitReached(true).build();

0 commit comments

Comments
 (0)