Skip to content

Commit 62e9165

Browse files
committed
Introduce remaining policy setters from ScheduledThreadPoolExecutor
Closes gh-26719
1 parent e1c0f3b commit 62e9165

File tree

1 file changed

+64
-23
lines changed

1 file changed

+64
-23
lines changed

spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskScheduler.java

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
* @since 3.0
5454
* @see #setPoolSize
5555
* @see #setRemoveOnCancelPolicy
56+
* @see #setContinueExistingPeriodicTasksAfterShutdownPolicy
57+
* @see #setExecuteExistingDelayedTasksAfterShutdownPolicy
5658
* @see #setThreadFactory
5759
* @see #setErrorHandler
5860
*/
@@ -64,6 +66,10 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
6466

6567
private volatile boolean removeOnCancelPolicy;
6668

69+
private volatile boolean continueExistingPeriodicTasksAfterShutdownPolicy;
70+
71+
private volatile boolean executeExistingDelayedTasksAfterShutdownPolicy = true;
72+
6773
@Nullable
6874
private volatile ErrorHandler errorHandler;
6975

@@ -93,17 +99,45 @@ public void setPoolSize(int poolSize) {
9399
/**
94100
* Set the remove-on-cancel mode on {@link ScheduledThreadPoolExecutor}.
95101
* <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).
97103
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
104+
* @see ScheduledThreadPoolExecutor#setRemoveOnCancelPolicy
98105
*/
99-
public void setRemoveOnCancelPolicy(boolean removeOnCancelPolicy) {
106+
public void setRemoveOnCancelPolicy(boolean flag) {
100107
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
101-
((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(removeOnCancelPolicy);
108+
((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(flag);
102109
}
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);
105124
}
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;
107141
}
108142

109143
/**
@@ -135,12 +169,16 @@ protected ExecutorService initializeExecutor(
135169

136170
this.scheduledExecutor = createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler);
137171

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);
141179
}
142-
else {
143-
logger.debug("Could not apply remove-on-cancel policy - not a ScheduledThreadPoolExecutor");
180+
if (!this.executeExistingDelayedTasksAfterShutdownPolicy) {
181+
scheduledPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
144182
}
145183
}
146184

@@ -201,18 +239,6 @@ public int getPoolSize() {
201239
return getScheduledThreadPoolExecutor().getPoolSize();
202240
}
203241

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-
216242
/**
217243
* Return the number of currently active threads.
218244
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
@@ -227,6 +253,21 @@ public int getActiveCount() {
227253
return getScheduledThreadPoolExecutor().getActiveCount();
228254
}
229255

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+
230271

231272
// SchedulingTaskExecutor implementation
232273

0 commit comments

Comments
 (0)