@@ -52,6 +52,8 @@ public class SimpleAsyncTaskExecutorBuilder {
52
52
53
53
private final @ Nullable String threadNamePrefix ;
54
54
55
+ private final boolean cancelRemainingTasksOnClose ;
56
+
55
57
private final boolean rejectTasksWhenLimitReached ;
56
58
57
59
private final @ Nullable Integer concurrencyLimit ;
@@ -63,15 +65,16 @@ public class SimpleAsyncTaskExecutorBuilder {
63
65
private final @ Nullable Duration taskTerminationTimeout ;
64
66
65
67
public SimpleAsyncTaskExecutorBuilder () {
66
- this (null , null , false , null , null , null , null );
68
+ this (null , null , false , false , null , null , null , null );
67
69
}
68
70
69
71
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 ) {
73
75
this .virtualThreads = virtualThreads ;
74
76
this .threadNamePrefix = threadNamePrefix ;
77
+ this .cancelRemainingTasksOnClose = cancelRemainingTasksOnClose ;
75
78
this .rejectTasksWhenLimitReached = rejectTasksWhenLimitReached ;
76
79
this .concurrencyLimit = concurrencyLimit ;
77
80
this .taskDecorator = taskDecorator ;
@@ -86,8 +89,8 @@ private SimpleAsyncTaskExecutorBuilder(@Nullable Boolean virtualThreads, @Nullab
86
89
*/
87
90
public SimpleAsyncTaskExecutorBuilder threadNamePrefix (@ Nullable String threadNamePrefix ) {
88
91
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 );
91
94
}
92
95
93
96
/**
@@ -97,8 +100,26 @@ public SimpleAsyncTaskExecutorBuilder threadNamePrefix(@Nullable String threadNa
97
100
*/
98
101
public SimpleAsyncTaskExecutorBuilder virtualThreads (@ Nullable Boolean virtualThreads ) {
99
102
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 );
102
123
}
103
124
104
125
/**
@@ -112,8 +133,8 @@ public SimpleAsyncTaskExecutorBuilder virtualThreads(@Nullable Boolean virtualTh
112
133
*/
113
134
public SimpleAsyncTaskExecutorBuilder rejectTasksWhenLimitReached (boolean rejectTasksWhenLimitReached ) {
114
135
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 );
117
138
}
118
139
119
140
/**
@@ -123,8 +144,8 @@ public SimpleAsyncTaskExecutorBuilder rejectTasksWhenLimitReached(boolean reject
123
144
*/
124
145
public SimpleAsyncTaskExecutorBuilder concurrencyLimit (@ Nullable Integer concurrencyLimit ) {
125
146
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 );
128
149
}
129
150
130
151
/**
@@ -134,8 +155,8 @@ public SimpleAsyncTaskExecutorBuilder concurrencyLimit(@Nullable Integer concurr
134
155
*/
135
156
public SimpleAsyncTaskExecutorBuilder taskDecorator (@ Nullable TaskDecorator taskDecorator ) {
136
157
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 );
139
160
}
140
161
141
162
/**
@@ -146,8 +167,8 @@ public SimpleAsyncTaskExecutorBuilder taskDecorator(@Nullable TaskDecorator task
146
167
*/
147
168
public SimpleAsyncTaskExecutorBuilder taskTerminationTimeout (@ Nullable Duration taskTerminationTimeout ) {
148
169
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 );
151
172
}
152
173
153
174
/**
@@ -177,8 +198,8 @@ public SimpleAsyncTaskExecutorBuilder customizers(
177
198
Iterable <? extends SimpleAsyncTaskExecutorCustomizer > customizers ) {
178
199
Assert .notNull (customizers , "'customizers' must not be null" );
179
200
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 );
182
203
}
183
204
184
205
/**
@@ -206,8 +227,8 @@ public SimpleAsyncTaskExecutorBuilder additionalCustomizers(
206
227
Iterable <? extends SimpleAsyncTaskExecutorCustomizer > customizers ) {
207
228
Assert .notNull (customizers , "'customizers' must not be null" );
208
229
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 );
211
232
}
212
233
213
234
/**
@@ -246,6 +267,7 @@ public <T extends SimpleAsyncTaskExecutor> T configure(T taskExecutor) {
246
267
PropertyMapper map = PropertyMapper .get ();
247
268
map .from (this .virtualThreads ).to (taskExecutor ::setVirtualThreads );
248
269
map .from (this .threadNamePrefix ).whenHasText ().to (taskExecutor ::setThreadNamePrefix );
270
+ map .from (this .cancelRemainingTasksOnClose ).to (taskExecutor ::setCancelRemainingTasksOnClose );
249
271
map .from (this .rejectTasksWhenLimitReached ).to (taskExecutor ::setRejectTasksWhenLimitReached );
250
272
map .from (this .concurrencyLimit ).to (taskExecutor ::setConcurrencyLimit );
251
273
map .from (this .taskDecorator ).to (taskExecutor ::setTaskDecorator );
0 commit comments