53
53
* @since 3.0
54
54
* @see #setPoolSize
55
55
* @see #setRemoveOnCancelPolicy
56
+ * @see #setContinueExistingPeriodicTasksAfterShutdownPolicy
57
+ * @see #setExecuteExistingDelayedTasksAfterShutdownPolicy
56
58
* @see #setThreadFactory
57
59
* @see #setErrorHandler
58
60
*/
@@ -64,6 +66,10 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
64
66
65
67
private volatile boolean removeOnCancelPolicy ;
66
68
69
+ private volatile boolean continueExistingPeriodicTasksAfterShutdownPolicy ;
70
+
71
+ private volatile boolean executeExistingDelayedTasksAfterShutdownPolicy = true ;
72
+
67
73
@ Nullable
68
74
private volatile ErrorHandler errorHandler ;
69
75
@@ -93,17 +99,45 @@ public void setPoolSize(int poolSize) {
93
99
/**
94
100
* Set the remove-on-cancel mode on {@link ScheduledThreadPoolExecutor}.
95
101
* <p>Default is {@code false}. If set to {@code true}, the target executor will be
96
- * switched into remove-on-cancel mode (if possible, with a soft fallback otherwise ).
102
+ * switched into remove-on-cancel mode (if possible).
97
103
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
104
+ * @see ScheduledThreadPoolExecutor#setRemoveOnCancelPolicy
98
105
*/
99
- public void setRemoveOnCancelPolicy (boolean removeOnCancelPolicy ) {
106
+ public void setRemoveOnCancelPolicy (boolean flag ) {
100
107
if (this .scheduledExecutor instanceof ScheduledThreadPoolExecutor ) {
101
- ((ScheduledThreadPoolExecutor ) this .scheduledExecutor ).setRemoveOnCancelPolicy (removeOnCancelPolicy );
108
+ ((ScheduledThreadPoolExecutor ) this .scheduledExecutor ).setRemoveOnCancelPolicy (flag );
102
109
}
103
- else if (removeOnCancelPolicy && this .scheduledExecutor != null ) {
104
- logger .debug ("Could not apply remove-on-cancel policy - not a ScheduledThreadPoolExecutor" );
110
+ this .removeOnCancelPolicy = flag ;
111
+ }
112
+
113
+ /**
114
+ * Set whether to continue existing periodic tasks even when this executor has been shutdown.
115
+ * <p>Default is {@code false}. If set to {@code true}, the target executor will be
116
+ * switched into continuing periodic tasks (if possible).
117
+ * <p><b>This setting can be modified at runtime, for example through JMX.</b>
118
+ * @since 5.3.9
119
+ * @see ScheduledThreadPoolExecutor#setContinueExistingPeriodicTasksAfterShutdownPolicy
120
+ */
121
+ public void setContinueExistingPeriodicTasksAfterShutdownPolicy (boolean flag ) {
122
+ if (this .scheduledExecutor instanceof ScheduledThreadPoolExecutor ) {
123
+ ((ScheduledThreadPoolExecutor ) this .scheduledExecutor ).setContinueExistingPeriodicTasksAfterShutdownPolicy (flag );
105
124
}
106
- this .removeOnCancelPolicy = removeOnCancelPolicy ;
125
+ this .continueExistingPeriodicTasksAfterShutdownPolicy = flag ;
126
+ }
127
+
128
+ /**
129
+ * Set whether to execute existing delayed tasks even when this executor has been shutdown.
130
+ * <p>Default is {@code true}. If set to {@code false}, the target executor will be
131
+ * switched into dropping remaining tasks (if possible).
132
+ * <p><b>This setting can be modified at runtime, for example through JMX.</b>
133
+ * @since 5.3.9
134
+ * @see ScheduledThreadPoolExecutor#setExecuteExistingDelayedTasksAfterShutdownPolicy
135
+ */
136
+ public void setExecuteExistingDelayedTasksAfterShutdownPolicy (boolean flag ) {
137
+ if (this .scheduledExecutor instanceof ScheduledThreadPoolExecutor ) {
138
+ ((ScheduledThreadPoolExecutor ) this .scheduledExecutor ).setExecuteExistingDelayedTasksAfterShutdownPolicy (flag );
139
+ }
140
+ this .executeExistingDelayedTasksAfterShutdownPolicy = flag ;
107
141
}
108
142
109
143
/**
@@ -135,12 +169,16 @@ protected ExecutorService initializeExecutor(
135
169
136
170
this .scheduledExecutor = createExecutor (this .poolSize , threadFactory , rejectedExecutionHandler );
137
171
138
- if (this .removeOnCancelPolicy ) {
139
- if (this .scheduledExecutor instanceof ScheduledThreadPoolExecutor ) {
140
- ((ScheduledThreadPoolExecutor ) this .scheduledExecutor ).setRemoveOnCancelPolicy (true );
172
+ if (this .scheduledExecutor instanceof ScheduledThreadPoolExecutor ) {
173
+ ScheduledThreadPoolExecutor scheduledPoolExecutor = (ScheduledThreadPoolExecutor ) this .scheduledExecutor ;
174
+ if (this .removeOnCancelPolicy ) {
175
+ scheduledPoolExecutor .setRemoveOnCancelPolicy (true );
176
+ }
177
+ if (this .continueExistingPeriodicTasksAfterShutdownPolicy ) {
178
+ scheduledPoolExecutor .setContinueExistingPeriodicTasksAfterShutdownPolicy (true );
141
179
}
142
- else {
143
- logger . debug ( "Could not apply remove-on-cancel policy - not a ScheduledThreadPoolExecutor" );
180
+ if (! this . executeExistingDelayedTasksAfterShutdownPolicy ) {
181
+ scheduledPoolExecutor . setExecuteExistingDelayedTasksAfterShutdownPolicy ( false );
144
182
}
145
183
}
146
184
@@ -201,18 +239,6 @@ public int getPoolSize() {
201
239
return getScheduledThreadPoolExecutor ().getPoolSize ();
202
240
}
203
241
204
- /**
205
- * Return the current setting for the remove-on-cancel mode.
206
- * <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
207
- */
208
- public boolean isRemoveOnCancelPolicy () {
209
- if (this .scheduledExecutor == null ) {
210
- // Not initialized yet: return our setting for the time being.
211
- return this .removeOnCancelPolicy ;
212
- }
213
- return getScheduledThreadPoolExecutor ().getRemoveOnCancelPolicy ();
214
- }
215
-
216
242
/**
217
243
* Return the number of currently active threads.
218
244
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
@@ -227,6 +253,21 @@ public int getActiveCount() {
227
253
return getScheduledThreadPoolExecutor ().getActiveCount ();
228
254
}
229
255
256
+ /**
257
+ * Return the current setting for the remove-on-cancel mode.
258
+ * <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
259
+ * @deprecated as of 5.3.9, in favor of direct
260
+ * {@link #getScheduledThreadPoolExecutor()} access
261
+ */
262
+ @ Deprecated
263
+ public boolean isRemoveOnCancelPolicy () {
264
+ if (this .scheduledExecutor == null ) {
265
+ // Not initialized yet: return our setting for the time being.
266
+ return this .removeOnCancelPolicy ;
267
+ }
268
+ return getScheduledThreadPoolExecutor ().getRemoveOnCancelPolicy ();
269
+ }
270
+
230
271
231
272
// SchedulingTaskExecutor implementation
232
273
0 commit comments