Skip to content

Commit

Permalink
Added an easier way to set ExecuteExistingDelayedTasksAfterShutdown a…
Browse files Browse the repository at this point in the history
…nd ContinueExistingPeriodicTasksAfterShutdown policies. This closes an existing issue: spring-projects#26719"
  • Loading branch information
aelykto committed Apr 15, 2021
1 parent af06b80 commit 567b1f3
Showing 1 changed file with 62 additions and 0 deletions.
Expand Up @@ -64,6 +64,10 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport

private volatile boolean removeOnCancelPolicy;

private volatile boolean executeExistingDelayedTasksAfterShutdownPolicy;

private volatile boolean continueExistingPeriodicTasksAfterShutdownPolicy;

@Nullable
private volatile ErrorHandler errorHandler;

Expand Down Expand Up @@ -106,6 +110,40 @@ else if (removeOnCancelPolicy && this.scheduledExecutor != null) {
}
}

/**
* Set the execute-existing-delayed-tasks-after-shutdown mode on {@link ScheduledThreadPoolExecutor}.
* <p>Default is {@code true}. If set to {@code false}, the target executor will be
* switched into stop-existing-delayed-tasks-on-shutdown mode.
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
*/
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean executeExistingDelayedTasksAfterShutdownPolicy) {
this.executeExistingDelayedTasksAfterShutdownPolicy = executeExistingDelayedTasksAfterShutdownPolicy;
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
((ScheduledThreadPoolExecutor) this.scheduledExecutor)
.setExecuteExistingDelayedTasksAfterShutdownPolicy(executeExistingDelayedTasksAfterShutdownPolicy);
}
else if (executeExistingDelayedTasksAfterShutdownPolicy && this.scheduledExecutor != null) {
logger.debug("Could not apply execute-existing-delayed-tasks-after-shutdown policy - not a ScheduledThreadPoolExecutor");
}
}

/**
* Set the continue-existing-periodic-tasks-after-shutdown mode on {@link ScheduledThreadPoolExecutor}.
* <p>Default is {@code false}. If set to {@code true}, the target executor will be
* switched into continue-existing-periodic-tasks-after-shutdown.
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
*/
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean continueExistingPeriodicTasksAfterShutdownPolicy) {
this.continueExistingPeriodicTasksAfterShutdownPolicy = continueExistingPeriodicTasksAfterShutdownPolicy;
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
((ScheduledThreadPoolExecutor) this.scheduledExecutor)
.setContinueExistingPeriodicTasksAfterShutdownPolicy(continueExistingPeriodicTasksAfterShutdownPolicy);
}
else if (continueExistingPeriodicTasksAfterShutdownPolicy && this.scheduledExecutor != null) {
logger.debug("Could not apply continue-existing-periodic-tasks-after-shutdown policy - not a ScheduledThreadPoolExecutor");
}
}

/**
* Set a custom {@link ErrorHandler} strategy.
*/
Expand Down Expand Up @@ -213,6 +251,30 @@ public boolean isRemoveOnCancelPolicy() {
return getScheduledThreadPoolExecutor().getRemoveOnCancelPolicy();
}

/**
* Return the current setting for the execute-existing-delayed-tasks-after-shutdown mode.
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
*/
public boolean isExecuteExistingDelayedTasksAfterShutdownPolicy() {
if (this.scheduledExecutor == null) {
// Not initialized yet: return our setting for the time being.
return this.executeExistingDelayedTasksAfterShutdownPolicy;
}
return getScheduledThreadPoolExecutor().getExecuteExistingDelayedTasksAfterShutdownPolicy();
}

/**
* Return the current setting for the continue-existing-periodic-tasks-after-shutdown mode.
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
*/
public boolean isContinueExistingPeriodicTasksAfterShutdownPolicy() {
if (this.scheduledExecutor == null) {
// Not initialized yet: return our setting for the time being.
return this.continueExistingPeriodicTasksAfterShutdownPolicy;
}
return getScheduledThreadPoolExecutor().getContinueExistingPeriodicTasksAfterShutdownPolicy();
}

/**
* Return the number of currently active threads.
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
Expand Down

0 comments on commit 567b1f3

Please sign in to comment.