Introduce remaining policy setters from ScheduledThreadPoolExecutor
Closes gh-26719
This commit is contained in:
parent
e1c0f3b067
commit
62e916534f
|
@ -53,6 +53,8 @@ import org.springframework.util.concurrent.ListenableFutureTask;
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
* @see #setPoolSize
|
* @see #setPoolSize
|
||||||
* @see #setRemoveOnCancelPolicy
|
* @see #setRemoveOnCancelPolicy
|
||||||
|
* @see #setContinueExistingPeriodicTasksAfterShutdownPolicy
|
||||||
|
* @see #setExecuteExistingDelayedTasksAfterShutdownPolicy
|
||||||
* @see #setThreadFactory
|
* @see #setThreadFactory
|
||||||
* @see #setErrorHandler
|
* @see #setErrorHandler
|
||||||
*/
|
*/
|
||||||
|
@ -64,6 +66,10 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
||||||
|
|
||||||
private volatile boolean removeOnCancelPolicy;
|
private volatile boolean removeOnCancelPolicy;
|
||||||
|
|
||||||
|
private volatile boolean continueExistingPeriodicTasksAfterShutdownPolicy;
|
||||||
|
|
||||||
|
private volatile boolean executeExistingDelayedTasksAfterShutdownPolicy = true;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private volatile ErrorHandler errorHandler;
|
private volatile ErrorHandler errorHandler;
|
||||||
|
|
||||||
|
@ -93,17 +99,45 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
||||||
/**
|
/**
|
||||||
* Set the remove-on-cancel mode on {@link ScheduledThreadPoolExecutor}.
|
* Set the remove-on-cancel mode on {@link ScheduledThreadPoolExecutor}.
|
||||||
* <p>Default is {@code false}. If set to {@code true}, the target executor will be
|
* <p>Default is {@code false}. If set to {@code true}, the target executor will be
|
||||||
* switched into remove-on-cancel mode (if possible, with a soft fallback otherwise).
|
* switched into remove-on-cancel mode (if possible).
|
||||||
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
|
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
|
||||||
|
* @see ScheduledThreadPoolExecutor#setRemoveOnCancelPolicy
|
||||||
*/
|
*/
|
||||||
public void setRemoveOnCancelPolicy(boolean removeOnCancelPolicy) {
|
public void setRemoveOnCancelPolicy(boolean flag) {
|
||||||
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
|
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
|
||||||
((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(removeOnCancelPolicy);
|
((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(flag);
|
||||||
}
|
}
|
||||||
else if (removeOnCancelPolicy && this.scheduledExecutor != null) {
|
this.removeOnCancelPolicy = flag;
|
||||||
logger.debug("Could not apply remove-on-cancel policy - not a ScheduledThreadPoolExecutor");
|
|
||||||
}
|
}
|
||||||
this.removeOnCancelPolicy = removeOnCancelPolicy;
|
|
||||||
|
/**
|
||||||
|
* Set whether to continue existing periodic tasks even when this executor has been shutdown.
|
||||||
|
* <p>Default is {@code false}. If set to {@code true}, the target executor will be
|
||||||
|
* switched into continuing periodic tasks (if possible).
|
||||||
|
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
|
||||||
|
* @since 5.3.9
|
||||||
|
* @see ScheduledThreadPoolExecutor#setContinueExistingPeriodicTasksAfterShutdownPolicy
|
||||||
|
*/
|
||||||
|
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean flag) {
|
||||||
|
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
|
||||||
|
((ScheduledThreadPoolExecutor) this.scheduledExecutor).setContinueExistingPeriodicTasksAfterShutdownPolicy(flag);
|
||||||
|
}
|
||||||
|
this.continueExistingPeriodicTasksAfterShutdownPolicy = flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set whether to execute existing delayed tasks even when this executor has been shutdown.
|
||||||
|
* <p>Default is {@code true}. If set to {@code false}, the target executor will be
|
||||||
|
* switched into dropping remaining tasks (if possible).
|
||||||
|
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
|
||||||
|
* @since 5.3.9
|
||||||
|
* @see ScheduledThreadPoolExecutor#setExecuteExistingDelayedTasksAfterShutdownPolicy
|
||||||
|
*/
|
||||||
|
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean flag) {
|
||||||
|
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
|
||||||
|
((ScheduledThreadPoolExecutor) this.scheduledExecutor).setExecuteExistingDelayedTasksAfterShutdownPolicy(flag);
|
||||||
|
}
|
||||||
|
this.executeExistingDelayedTasksAfterShutdownPolicy = flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -135,12 +169,16 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
||||||
|
|
||||||
this.scheduledExecutor = createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler);
|
this.scheduledExecutor = createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler);
|
||||||
|
|
||||||
if (this.removeOnCancelPolicy) {
|
|
||||||
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
|
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
|
||||||
((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(true);
|
ScheduledThreadPoolExecutor scheduledPoolExecutor = (ScheduledThreadPoolExecutor) this.scheduledExecutor;
|
||||||
|
if (this.removeOnCancelPolicy) {
|
||||||
|
scheduledPoolExecutor.setRemoveOnCancelPolicy(true);
|
||||||
}
|
}
|
||||||
else {
|
if (this.continueExistingPeriodicTasksAfterShutdownPolicy) {
|
||||||
logger.debug("Could not apply remove-on-cancel policy - not a ScheduledThreadPoolExecutor");
|
scheduledPoolExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
|
||||||
|
}
|
||||||
|
if (!this.executeExistingDelayedTasksAfterShutdownPolicy) {
|
||||||
|
scheduledPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,18 +239,6 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
||||||
return getScheduledThreadPoolExecutor().getPoolSize();
|
return getScheduledThreadPoolExecutor().getPoolSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the current setting for the remove-on-cancel mode.
|
|
||||||
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
|
|
||||||
*/
|
|
||||||
public boolean isRemoveOnCancelPolicy() {
|
|
||||||
if (this.scheduledExecutor == null) {
|
|
||||||
// Not initialized yet: return our setting for the time being.
|
|
||||||
return this.removeOnCancelPolicy;
|
|
||||||
}
|
|
||||||
return getScheduledThreadPoolExecutor().getRemoveOnCancelPolicy();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the number of currently active threads.
|
* Return the number of currently active threads.
|
||||||
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
|
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
|
||||||
|
@ -227,6 +253,21 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
||||||
return getScheduledThreadPoolExecutor().getActiveCount();
|
return getScheduledThreadPoolExecutor().getActiveCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the current setting for the remove-on-cancel mode.
|
||||||
|
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
|
||||||
|
* @deprecated as of 5.3.9, in favor of direct
|
||||||
|
* {@link #getScheduledThreadPoolExecutor()} access
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public boolean isRemoveOnCancelPolicy() {
|
||||||
|
if (this.scheduledExecutor == null) {
|
||||||
|
// Not initialized yet: return our setting for the time being.
|
||||||
|
return this.removeOnCancelPolicy;
|
||||||
|
}
|
||||||
|
return getScheduledThreadPoolExecutor().getRemoveOnCancelPolicy();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// SchedulingTaskExecutor implementation
|
// SchedulingTaskExecutor implementation
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue