Skip to content

Commit

Permalink
Remove JDK 1.7 guard for removeOnCancelPolicy property
Browse files Browse the repository at this point in the history
Issue: SPR-11918
  • Loading branch information
rstoyanchev committed Jun 29, 2014
1 parent 7441f23 commit 9880d2b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@
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;
Expand All @@ -96,9 +91,8 @@ public void setPoolSize(int 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.
* Set the same property on ScheduledExecutorService (JDK 1.7+).
* There is no default. If not set, the executor property is not set.
*/
public void setRemoveOnCancelPolicy(boolean removeOnCancelPolicy) {
this.removeOnCancelPolicy = removeOnCancelPolicy;
Expand Down Expand Up @@ -149,8 +143,8 @@ protected ExecutorService initializeExecutor(
ScheduledExecutorService executor =
createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler);

if (executor instanceof ScheduledThreadPoolExecutor) {
configureRemoveOnCancelPolicy(((ScheduledThreadPoolExecutor) executor));
if (executor instanceof ScheduledThreadPoolExecutor && this.removeOnCancelPolicy != null) {
((ScheduledThreadPoolExecutor) executor).setRemoveOnCancelPolicy(this.removeOnCancelPolicy);
}

// Register specified ScheduledExecutorTasks, if necessary.
Expand Down Expand Up @@ -222,14 +216,6 @@ protected Runnable getRunnableToSchedule(ScheduledExecutorTask task) {
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() {
return this.exposedExecutor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@

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;
Expand All @@ -50,19 +48,14 @@
* @author Mark Fisher
* @since 3.0
* @see #setPoolSize
* @see #setRemoveOnCancelPolicy(boolean)
* @see #setRemoveOnCancelPolicy
* @see #setThreadFactory
* @see #setErrorHandler
*/
@SuppressWarnings("serial")
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;
Expand All @@ -86,23 +79,14 @@ public void setPoolSize(int 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.
* Set the same property on ScheduledExecutorService (JDK 1.7+).
* There is no default. If not set, the executor property is not set.
* <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);
((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(removeOnCancelPolicy);
}
}

Expand All @@ -120,8 +104,8 @@ protected ExecutorService initializeExecutor(

this.scheduledExecutor = createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler);

if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
configureRemoveOnCancelPolicy(((ScheduledThreadPoolExecutor) this.scheduledExecutor));
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor && this.removeOnCancelPolicy != null) {
((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(this.removeOnCancelPolicy);
}

return this.scheduledExecutor;
Expand Down Expand Up @@ -186,7 +170,11 @@ public int getPoolSize() {
* <p>Requires an underlying {@link ScheduledThreadPoolExecutor} and JDK 1.7+.
*/
public boolean isRemoveOnCancelPolicy() {
return this.removeOnCancelPolicy;
if (this.scheduledExecutor == null) {
// Not initialized yet: return false (the default of the executor)
return false;
}
return getScheduledThreadPoolExecutor().getRemoveOnCancelPolicy();
}

/**
Expand Down

0 comments on commit 9880d2b

Please sign in to comment.