|
37 | 37 | import org.springframework.scheduling.Trigger;
|
38 | 38 | import org.springframework.scheduling.support.TaskUtils;
|
39 | 39 | import org.springframework.util.Assert;
|
| 40 | +import org.springframework.util.ClassUtils; |
40 | 41 | import org.springframework.util.ErrorHandler;
|
41 | 42 | import org.springframework.util.concurrent.ListenableFuture;
|
42 | 43 | import org.springframework.util.concurrent.ListenableFutureTask;
|
|
57 | 58 | public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
|
58 | 59 | implements AsyncListenableTaskExecutor, SchedulingTaskExecutor, TaskScheduler {
|
59 | 60 |
|
| 61 | + // ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy(boolean) only available on JDK 1.7+ |
| 62 | + private static final boolean setRemoveOnCancelPolicyAvailable = |
| 63 | + ClassUtils.hasMethod(ScheduledThreadPoolExecutor.class, "setRemoveOnCancelPolicy", boolean.class); |
| 64 | + |
| 65 | + |
60 | 66 | private volatile int poolSize = 1;
|
61 | 67 |
|
62 |
| - private volatile Boolean removeOnCancelPolicy; |
| 68 | + private volatile boolean removeOnCancelPolicy; |
63 | 69 |
|
64 | 70 | private volatile ScheduledExecutorService scheduledExecutor;
|
65 | 71 |
|
@@ -87,28 +93,36 @@ public void setPoolSize(int poolSize) {
|
87 | 93 | @UsesJava7
|
88 | 94 | public void setRemoveOnCancelPolicy(boolean removeOnCancelPolicy) {
|
89 | 95 | this.removeOnCancelPolicy = removeOnCancelPolicy;
|
90 |
| - if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) { |
| 96 | + if (setRemoveOnCancelPolicyAvailable && this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) { |
91 | 97 | ((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(removeOnCancelPolicy);
|
92 | 98 | }
|
| 99 | + else if (removeOnCancelPolicy && this.scheduledExecutor != null) { |
| 100 | + logger.info("Could not apply remove-on-cancel policy - not a Java 7+ ScheduledThreadPoolExecutor"); |
| 101 | + } |
93 | 102 | }
|
94 | 103 |
|
95 | 104 | /**
|
96 | 105 | * Set a custom {@link ErrorHandler} strategy.
|
97 | 106 | */
|
98 | 107 | public void setErrorHandler(ErrorHandler errorHandler) {
|
99 |
| - Assert.notNull(errorHandler, "'errorHandler' must not be null"); |
100 | 108 | this.errorHandler = errorHandler;
|
101 | 109 | }
|
102 | 110 |
|
| 111 | + |
103 | 112 | @UsesJava7
|
104 | 113 | @Override
|
105 | 114 | protected ExecutorService initializeExecutor(
|
106 | 115 | ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
107 | 116 |
|
108 | 117 | this.scheduledExecutor = createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler);
|
109 | 118 |
|
110 |
| - if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor && this.removeOnCancelPolicy != null) { |
111 |
| - ((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(this.removeOnCancelPolicy); |
| 119 | + if (this.removeOnCancelPolicy) { |
| 120 | + if (setRemoveOnCancelPolicyAvailable && this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) { |
| 121 | + ((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(true); |
| 122 | + } |
| 123 | + else { |
| 124 | + logger.info("Could not apply remove-on-cancel policy - not a Java 7+ ScheduledThreadPoolExecutor"); |
| 125 | + } |
112 | 126 | }
|
113 | 127 |
|
114 | 128 | return this.scheduledExecutor;
|
@@ -175,8 +189,8 @@ public int getPoolSize() {
|
175 | 189 | @UsesJava7
|
176 | 190 | public boolean isRemoveOnCancelPolicy() {
|
177 | 191 | if (this.scheduledExecutor == null) {
|
178 |
| - // Not initialized yet: return false (the default of the executor) |
179 |
| - return false; |
| 192 | + // Not initialized yet: return our setting for the time being. |
| 193 | + return (setRemoveOnCancelPolicyAvailable && this.removeOnCancelPolicy); |
180 | 194 | }
|
181 | 195 | return getScheduledThreadPoolExecutor().getRemoveOnCancelPolicy();
|
182 | 196 | }
|
|
0 commit comments