Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier policy setters for ThreadPoolTaskScheduler. #26813

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport

private volatile boolean removeOnCancelPolicy;

private volatile boolean executeExistingDelayedTasksAfterShutdownPolicy = true;

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