Add support for removeOnCancelPolicy
This change removes the recently added SockJsThreadPoolTaskScheduler and instead builds support for the removeOnCancelPolicy property in ThreadPoolTaskScheduler and ScheduledExecutorFactoryBean. Issue: SPR-11918
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,9 +24,11 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.lang.UsesJava7;
|
||||
import org.springframework.scheduling.support.DelegatingErrorHandlingRunnable;
|
||||
import org.springframework.scheduling.support.TaskUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
@@ -56,6 +58,7 @@ import org.springframework.util.ObjectUtils;
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.0
|
||||
* @see #setPoolSize
|
||||
* @see #setRemoveOnCancelPolicy(boolean)
|
||||
* @see #setThreadFactory
|
||||
* @see ScheduledExecutorTask
|
||||
* @see java.util.concurrent.ScheduledExecutorService
|
||||
@@ -65,8 +68,15 @@ import org.springframework.util.ObjectUtils;
|
||||
public class ScheduledExecutorFactoryBean extends ExecutorConfigurationSupport
|
||||
implements FactoryBean<ScheduledExecutorService> {
|
||||
|
||||
// Check for setRemoveOnCancelPolicy method - available on JDK 7 and higher
|
||||
private static boolean hasRemoveOnCancelPolicyMethod = ClassUtils.hasMethod(
|
||||
ScheduledThreadPoolExecutor.class, "setRemoveOnCancelPolicy", boolean.class);
|
||||
|
||||
|
||||
private int poolSize = 1;
|
||||
|
||||
private Boolean removeOnCancelPolicy;
|
||||
|
||||
private ScheduledExecutorTask[] scheduledExecutorTasks;
|
||||
|
||||
private boolean continueScheduledExecutionAfterException = false;
|
||||
@@ -85,6 +95,15 @@ public class ScheduledExecutorFactoryBean extends ExecutorConfigurationSupport
|
||||
this.poolSize = poolSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the same property on ScheduledExecutorService available in JDK 1.7 or
|
||||
* higher. This property is ignored on JDK 1.6.
|
||||
* Default is false.
|
||||
*/
|
||||
public void setRemoveOnCancelPolicy(boolean removeOnCancelPolicy) {
|
||||
this.removeOnCancelPolicy = removeOnCancelPolicy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a list of ScheduledExecutorTask objects with the ScheduledExecutorService
|
||||
* that this FactoryBean creates. Depending on each ScheduledExecutorTask's settings,
|
||||
@@ -130,6 +149,10 @@ public class ScheduledExecutorFactoryBean extends ExecutorConfigurationSupport
|
||||
ScheduledExecutorService executor =
|
||||
createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler);
|
||||
|
||||
if (executor instanceof ScheduledThreadPoolExecutor) {
|
||||
configureRemoveOnCancelPolicy(((ScheduledThreadPoolExecutor) executor));
|
||||
}
|
||||
|
||||
// Register specified ScheduledExecutorTasks, if necessary.
|
||||
if (!ObjectUtils.isEmpty(this.scheduledExecutorTasks)) {
|
||||
registerTasks(this.scheduledExecutorTasks, executor);
|
||||
@@ -199,6 +222,13 @@ public class ScheduledExecutorFactoryBean extends ExecutorConfigurationSupport
|
||||
new DelegatingErrorHandlingRunnable(task.getRunnable(), TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER));
|
||||
}
|
||||
|
||||
@UsesJava7 // guard setting removeOnCancelPolicy (safe with 1.6 due to hasRemoveOnCancelPolicyMethod check)
|
||||
private void configureRemoveOnCancelPolicy(ScheduledThreadPoolExecutor service) {
|
||||
if (hasRemoveOnCancelPolicyMethod && this.removeOnCancelPolicy != null) {
|
||||
service.setRemoveOnCancelPolicy(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ScheduledExecutorService getObject() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,11 +31,13 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.core.task.AsyncListenableTaskExecutor;
|
||||
import org.springframework.core.task.TaskRejectedException;
|
||||
import org.springframework.lang.UsesJava7;
|
||||
import org.springframework.scheduling.SchedulingTaskExecutor;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.Trigger;
|
||||
import org.springframework.scheduling.support.TaskUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ErrorHandler;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
import org.springframework.util.concurrent.ListenableFutureTask;
|
||||
@@ -48,6 +50,7 @@ import org.springframework.util.concurrent.ListenableFutureTask;
|
||||
* @author Mark Fisher
|
||||
* @since 3.0
|
||||
* @see #setPoolSize
|
||||
* @see #setRemoveOnCancelPolicy(boolean)
|
||||
* @see #setThreadFactory
|
||||
* @see #setErrorHandler
|
||||
*/
|
||||
@@ -55,8 +58,15 @@ import org.springframework.util.concurrent.ListenableFutureTask;
|
||||
public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
||||
implements AsyncListenableTaskExecutor, SchedulingTaskExecutor, TaskScheduler {
|
||||
|
||||
// Check for setRemoveOnCancelPolicy method - available on JDK 7 and higher
|
||||
private static boolean hasRemoveOnCancelPolicyMethod = ClassUtils.hasMethod(
|
||||
ScheduledThreadPoolExecutor.class, "setRemoveOnCancelPolicy", boolean.class);
|
||||
|
||||
|
||||
private volatile int poolSize = 1;
|
||||
|
||||
private volatile Boolean removeOnCancelPolicy;
|
||||
|
||||
private volatile ScheduledExecutorService scheduledExecutor;
|
||||
|
||||
private volatile ErrorHandler errorHandler;
|
||||
@@ -75,6 +85,27 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the same property on ScheduledExecutorService available in JDK 1.7 or
|
||||
* higher. This property is ignored on JDK 1.6.
|
||||
* Default is false.
|
||||
* <p><b>This setting can be modified at runtime, for example through JMX.</b>
|
||||
*/
|
||||
@UsesJava7 // guard setting removeOnCancelPolicy (safe with 1.6 due to hasRemoveOnCancelPolicyMethod check)
|
||||
public void setRemoveOnCancelPolicy(boolean removeOnCancelPolicy) {
|
||||
this.removeOnCancelPolicy = removeOnCancelPolicy;
|
||||
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
|
||||
configureRemoveOnCancelPolicy((ScheduledThreadPoolExecutor) this.scheduledExecutor);
|
||||
}
|
||||
}
|
||||
|
||||
@UsesJava7 // guard setting removeOnCancelPolicy (safe with 1.6 due to hasRemoveOnCancelPolicyMethod check)
|
||||
private void configureRemoveOnCancelPolicy(ScheduledThreadPoolExecutor service) {
|
||||
if (hasRemoveOnCancelPolicyMethod && this.removeOnCancelPolicy != null) {
|
||||
service.setRemoveOnCancelPolicy(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom {@link ErrorHandler} strategy.
|
||||
*/
|
||||
@@ -88,6 +119,11 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
||||
ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||
|
||||
this.scheduledExecutor = createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler);
|
||||
|
||||
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
|
||||
configureRemoveOnCancelPolicy(((ScheduledThreadPoolExecutor) this.scheduledExecutor));
|
||||
}
|
||||
|
||||
return this.scheduledExecutor;
|
||||
}
|
||||
|
||||
@@ -145,6 +181,14 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
||||
return getScheduledThreadPoolExecutor().getPoolSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current setting of removeOnCancelPolicy.
|
||||
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor} and JDK 1.7+.
|
||||
*/
|
||||
public boolean isRemoveOnCancelPolicy() {
|
||||
return this.removeOnCancelPolicy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of currently active threads.
|
||||
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor}.
|
||||
|
||||
Reference in New Issue
Block a user