40
40
* bean and can be injected whenever a {@link TaskExecutor} is needed.
41
41
*
42
42
* @author Stephane Nicoll
43
+ * @author Filip Hrisafov
43
44
* @since 2.1.0
44
45
*/
45
46
public class TaskExecutorBuilder {
@@ -56,6 +57,10 @@ public class TaskExecutorBuilder {
56
57
57
58
private final String threadNamePrefix ;
58
59
60
+ private final Duration awaitTermination ;
61
+
62
+ private final Boolean waitForTasksToCompleteOnShutdown ;
63
+
59
64
private final TaskDecorator taskDecorator ;
60
65
61
66
private final Set <TaskExecutorCustomizer > customizers ;
@@ -67,20 +72,25 @@ public TaskExecutorBuilder() {
67
72
this .allowCoreThreadTimeOut = null ;
68
73
this .keepAlive = null ;
69
74
this .threadNamePrefix = null ;
75
+ this .awaitTermination = null ;
76
+ this .waitForTasksToCompleteOnShutdown = null ;
70
77
this .taskDecorator = null ;
71
78
this .customizers = null ;
72
79
}
73
80
74
81
private TaskExecutorBuilder (Integer queueCapacity , Integer corePoolSize ,
75
82
Integer maxPoolSize , Boolean allowCoreThreadTimeOut , Duration keepAlive ,
76
- String threadNamePrefix , TaskDecorator taskDecorator ,
83
+ String threadNamePrefix , Duration awaitTermination ,
84
+ Boolean waitForTasksToCompleteOnShutdown , TaskDecorator taskDecorator ,
77
85
Set <TaskExecutorCustomizer > customizers ) {
78
86
this .queueCapacity = queueCapacity ;
79
87
this .corePoolSize = corePoolSize ;
80
88
this .maxPoolSize = maxPoolSize ;
81
89
this .allowCoreThreadTimeOut = allowCoreThreadTimeOut ;
82
90
this .keepAlive = keepAlive ;
83
91
this .threadNamePrefix = threadNamePrefix ;
92
+ this .awaitTermination = awaitTermination ;
93
+ this .waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown ;
84
94
this .taskDecorator = taskDecorator ;
85
95
this .customizers = customizers ;
86
96
}
@@ -94,6 +104,7 @@ private TaskExecutorBuilder(Integer queueCapacity, Integer corePoolSize,
94
104
public TaskExecutorBuilder queueCapacity (int queueCapacity ) {
95
105
return new TaskExecutorBuilder (queueCapacity , this .corePoolSize , this .maxPoolSize ,
96
106
this .allowCoreThreadTimeOut , this .keepAlive , this .threadNamePrefix ,
107
+ this .awaitTermination , this .waitForTasksToCompleteOnShutdown ,
97
108
this .taskDecorator , this .customizers );
98
109
}
99
110
@@ -109,6 +120,7 @@ public TaskExecutorBuilder queueCapacity(int queueCapacity) {
109
120
public TaskExecutorBuilder corePoolSize (int corePoolSize ) {
110
121
return new TaskExecutorBuilder (this .queueCapacity , corePoolSize , this .maxPoolSize ,
111
122
this .allowCoreThreadTimeOut , this .keepAlive , this .threadNamePrefix ,
123
+ this .awaitTermination , this .waitForTasksToCompleteOnShutdown ,
112
124
this .taskDecorator , this .customizers );
113
125
}
114
126
@@ -124,6 +136,7 @@ public TaskExecutorBuilder corePoolSize(int corePoolSize) {
124
136
public TaskExecutorBuilder maxPoolSize (int maxPoolSize ) {
125
137
return new TaskExecutorBuilder (this .queueCapacity , this .corePoolSize , maxPoolSize ,
126
138
this .allowCoreThreadTimeOut , this .keepAlive , this .threadNamePrefix ,
139
+ this .awaitTermination , this .waitForTasksToCompleteOnShutdown ,
127
140
this .taskDecorator , this .customizers );
128
141
}
129
142
@@ -136,7 +149,9 @@ public TaskExecutorBuilder maxPoolSize(int maxPoolSize) {
136
149
public TaskExecutorBuilder allowCoreThreadTimeOut (boolean allowCoreThreadTimeOut ) {
137
150
return new TaskExecutorBuilder (this .queueCapacity , this .corePoolSize ,
138
151
this .maxPoolSize , allowCoreThreadTimeOut , this .keepAlive ,
139
- this .threadNamePrefix , this .taskDecorator , this .customizers );
152
+ this .threadNamePrefix , this .awaitTermination ,
153
+ this .waitForTasksToCompleteOnShutdown , this .taskDecorator ,
154
+ this .customizers );
140
155
}
141
156
142
157
/**
@@ -147,7 +162,9 @@ public TaskExecutorBuilder allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut
147
162
public TaskExecutorBuilder keepAlive (Duration keepAlive ) {
148
163
return new TaskExecutorBuilder (this .queueCapacity , this .corePoolSize ,
149
164
this .maxPoolSize , this .allowCoreThreadTimeOut , keepAlive ,
150
- this .threadNamePrefix , this .taskDecorator , this .customizers );
165
+ this .threadNamePrefix , this .awaitTermination ,
166
+ this .waitForTasksToCompleteOnShutdown , this .taskDecorator ,
167
+ this .customizers );
151
168
}
152
169
153
170
/**
@@ -158,7 +175,41 @@ public TaskExecutorBuilder keepAlive(Duration keepAlive) {
158
175
public TaskExecutorBuilder threadNamePrefix (String threadNamePrefix ) {
159
176
return new TaskExecutorBuilder (this .queueCapacity , this .corePoolSize ,
160
177
this .maxPoolSize , this .allowCoreThreadTimeOut , this .keepAlive ,
161
- threadNamePrefix , this .taskDecorator , this .customizers );
178
+ threadNamePrefix , this .awaitTermination ,
179
+ this .waitForTasksToCompleteOnShutdown , this .taskDecorator ,
180
+ this .customizers );
181
+ }
182
+
183
+ /**
184
+ * Set the maximum number of time that the executor is supposed to block on shutdown
185
+ * in order to wait for remaining tasks to complete their execution before the rest of
186
+ * the container continues to shut down. This is particularly useful if your remaining
187
+ * tasks are likely to need access to other resources that are also managed by the
188
+ * container.
189
+ * @param awaitTermination the await termination to set
190
+ * @return a new builder instance
191
+ */
192
+ public TaskExecutorBuilder awaitTermination (Duration awaitTermination ) {
193
+ return new TaskExecutorBuilder (this .queueCapacity , this .corePoolSize ,
194
+ this .maxPoolSize , this .allowCoreThreadTimeOut , this .keepAlive ,
195
+ this .threadNamePrefix , awaitTermination ,
196
+ this .waitForTasksToCompleteOnShutdown , this .taskDecorator ,
197
+ this .customizers );
198
+ }
199
+
200
+ /**
201
+ * Set whether the executor should wait for scheduled tasks to complete on shutdown,
202
+ * not interrupting running tasks and executing all tasks in the queue.
203
+ * @param waitForTasksToCompleteOnShutdown if executor needs to wait for the tasks to
204
+ * complete on shutdown
205
+ * @return a new builder instance
206
+ */
207
+ public TaskExecutorBuilder waitForTasksToCompleteOnShutdown (
208
+ boolean waitForTasksToCompleteOnShutdown ) {
209
+ return new TaskExecutorBuilder (this .queueCapacity , this .corePoolSize ,
210
+ this .maxPoolSize , this .allowCoreThreadTimeOut , this .keepAlive ,
211
+ this .threadNamePrefix , this .awaitTermination ,
212
+ waitForTasksToCompleteOnShutdown , this .taskDecorator , this .customizers );
162
213
}
163
214
164
215
/**
@@ -169,7 +220,8 @@ public TaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
169
220
public TaskExecutorBuilder taskDecorator (TaskDecorator taskDecorator ) {
170
221
return new TaskExecutorBuilder (this .queueCapacity , this .corePoolSize ,
171
222
this .maxPoolSize , this .allowCoreThreadTimeOut , this .keepAlive ,
172
- this .threadNamePrefix , taskDecorator , this .customizers );
223
+ this .threadNamePrefix , this .awaitTermination ,
224
+ this .waitForTasksToCompleteOnShutdown , taskDecorator , this .customizers );
173
225
}
174
226
175
227
/**
@@ -199,7 +251,9 @@ public TaskExecutorBuilder customizers(Iterable<TaskExecutorCustomizer> customiz
199
251
Assert .notNull (customizers , "Customizers must not be null" );
200
252
return new TaskExecutorBuilder (this .queueCapacity , this .corePoolSize ,
201
253
this .maxPoolSize , this .allowCoreThreadTimeOut , this .keepAlive ,
202
- this .threadNamePrefix , this .taskDecorator , append (null , customizers ));
254
+ this .threadNamePrefix , this .awaitTermination ,
255
+ this .waitForTasksToCompleteOnShutdown , this .taskDecorator ,
256
+ append (null , customizers ));
203
257
}
204
258
205
259
/**
@@ -229,7 +283,8 @@ public TaskExecutorBuilder additionalCustomizers(
229
283
Assert .notNull (customizers , "Customizers must not be null" );
230
284
return new TaskExecutorBuilder (this .queueCapacity , this .corePoolSize ,
231
285
this .maxPoolSize , this .allowCoreThreadTimeOut , this .keepAlive ,
232
- this .threadNamePrefix , this .taskDecorator ,
286
+ this .threadNamePrefix , this .awaitTermination ,
287
+ this .waitForTasksToCompleteOnShutdown , this .taskDecorator ,
233
288
append (this .customizers , customizers ));
234
289
}
235
290
@@ -275,6 +330,10 @@ public <T extends ThreadPoolTaskExecutor> T configure(T taskExecutor) {
275
330
map .from (this .allowCoreThreadTimeOut ).to (taskExecutor ::setAllowCoreThreadTimeOut );
276
331
map .from (this .threadNamePrefix ).whenHasText ()
277
332
.to (taskExecutor ::setThreadNamePrefix );
333
+ map .from (this .awaitTermination ).asInt (Duration ::getSeconds )
334
+ .to (taskExecutor ::setAwaitTerminationSeconds );
335
+ map .from (this .waitForTasksToCompleteOnShutdown )
336
+ .to (taskExecutor ::setWaitForTasksToCompleteOnShutdown );
278
337
map .from (this .taskDecorator ).to (taskExecutor ::setTaskDecorator );
279
338
if (!CollectionUtils .isEmpty (this .customizers )) {
280
339
this .customizers .forEach ((customizer ) -> customizer .customize (taskExecutor ));
0 commit comments