Deprecate default constructor on ConcurrentTaskExecutor/Scheduler

Includes revision of null Executor configuration.
Respects TaskDecorator configuration on DefaultManagedTaskExecutor.

Closes gh-27914
This commit is contained in:
Juergen Hoeller
2023-05-08 11:21:29 +02:00
parent 73d30dd875
commit d8d7e0a762
5 changed files with 95 additions and 42 deletions

View File

@@ -65,6 +65,10 @@ import org.springframework.util.concurrent.ListenableFuture;
@SuppressWarnings("deprecation")
public class ConcurrentTaskExecutor implements AsyncListenableTaskExecutor, SchedulingTaskExecutor {
private static final Executor STUB_EXECUTOR = (task -> {
throw new IllegalStateException("Executor not configured");
});
@Nullable
private static Class<?> managedExecutorServiceClass;
@@ -80,15 +84,22 @@ public class ConcurrentTaskExecutor implements AsyncListenableTaskExecutor, Sche
}
}
private Executor concurrentExecutor;
private TaskExecutorAdapter adaptedExecutor;
private Executor concurrentExecutor = STUB_EXECUTOR;
private TaskExecutorAdapter adaptedExecutor = new TaskExecutorAdapter(STUB_EXECUTOR);
@Nullable
private TaskDecorator taskDecorator;
/**
* Create a new ConcurrentTaskExecutor, using a single thread executor as default.
* @see java.util.concurrent.Executors#newSingleThreadExecutor()
* @deprecated in favor of {@link #ConcurrentTaskExecutor(Executor)} with an
* externally provided Executor
*/
@Deprecated(since = "6.1")
public ConcurrentTaskExecutor() {
this.concurrentExecutor = Executors.newSingleThreadExecutor();
this.adaptedExecutor = new TaskExecutorAdapter(this.concurrentExecutor);
@@ -101,8 +112,9 @@ public class ConcurrentTaskExecutor implements AsyncListenableTaskExecutor, Sche
* @param executor the {@link java.util.concurrent.Executor} to delegate to
*/
public ConcurrentTaskExecutor(@Nullable Executor executor) {
this.concurrentExecutor = (executor != null ? executor : Executors.newSingleThreadExecutor());
this.adaptedExecutor = getAdaptedExecutor(this.concurrentExecutor);
if (executor != null) {
setConcurrentExecutor(executor);
}
}
@@ -111,8 +123,8 @@ public class ConcurrentTaskExecutor implements AsyncListenableTaskExecutor, Sche
* <p>Autodetects a JSR-236 {@link jakarta.enterprise.concurrent.ManagedExecutorService}
* in order to expose {@link jakarta.enterprise.concurrent.ManagedTask} adapters for it.
*/
public final void setConcurrentExecutor(@Nullable Executor executor) {
this.concurrentExecutor = (executor != null ? executor : Executors.newSingleThreadExecutor());
public final void setConcurrentExecutor(Executor executor) {
this.concurrentExecutor = executor;
this.adaptedExecutor = getAdaptedExecutor(this.concurrentExecutor);
}
@@ -139,6 +151,7 @@ public class ConcurrentTaskExecutor implements AsyncListenableTaskExecutor, Sche
* @since 4.3
*/
public final void setTaskDecorator(TaskDecorator taskDecorator) {
this.taskDecorator = taskDecorator;
this.adaptedExecutor.setTaskDecorator(taskDecorator);
}
@@ -175,11 +188,15 @@ public class ConcurrentTaskExecutor implements AsyncListenableTaskExecutor, Sche
}
private static TaskExecutorAdapter getAdaptedExecutor(Executor concurrentExecutor) {
private TaskExecutorAdapter getAdaptedExecutor(Executor concurrentExecutor) {
if (managedExecutorServiceClass != null && managedExecutorServiceClass.isInstance(concurrentExecutor)) {
return new ManagedTaskExecutorAdapter(concurrentExecutor);
}
return new TaskExecutorAdapter(concurrentExecutor);
TaskExecutorAdapter adapter = new TaskExecutorAdapter(concurrentExecutor);
if (this.taskDecorator != null) {
adapter.setTaskDecorator(this.taskDecorator);
}
return adapter;
}

View File

@@ -100,10 +100,14 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
* Create a new ConcurrentTaskScheduler,
* using a single thread executor as default.
* @see java.util.concurrent.Executors#newSingleThreadScheduledExecutor()
* @deprecated in favor of {@link #ConcurrentTaskScheduler(ScheduledExecutorService)}
* with an externally provided Executor
*/
@Deprecated(since = "6.1")
public ConcurrentTaskScheduler() {
super();
this.scheduledExecutor = initScheduledExecutor(null);
this.scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
this.enterpriseConcurrentScheduler = false;
}
/**
@@ -116,9 +120,11 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
* to delegate to for {@link org.springframework.scheduling.SchedulingTaskExecutor}
* as well as {@link TaskScheduler} invocations
*/
public ConcurrentTaskScheduler(ScheduledExecutorService scheduledExecutor) {
public ConcurrentTaskScheduler(@Nullable ScheduledExecutorService scheduledExecutor) {
super(scheduledExecutor);
this.scheduledExecutor = initScheduledExecutor(scheduledExecutor);
if (scheduledExecutor != null) {
initScheduledExecutor(scheduledExecutor);
}
}
/**
@@ -134,21 +140,14 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
*/
public ConcurrentTaskScheduler(Executor concurrentExecutor, ScheduledExecutorService scheduledExecutor) {
super(concurrentExecutor);
this.scheduledExecutor = initScheduledExecutor(scheduledExecutor);
initScheduledExecutor(scheduledExecutor);
}
private ScheduledExecutorService initScheduledExecutor(@Nullable ScheduledExecutorService scheduledExecutor) {
if (scheduledExecutor != null) {
this.scheduledExecutor = scheduledExecutor;
this.enterpriseConcurrentScheduler = (managedScheduledExecutorServiceClass != null &&
managedScheduledExecutorServiceClass.isInstance(scheduledExecutor));
}
else {
this.scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
this.enterpriseConcurrentScheduler = false;
}
return this.scheduledExecutor;
private void initScheduledExecutor(ScheduledExecutorService scheduledExecutor) {
this.scheduledExecutor = scheduledExecutor;
this.enterpriseConcurrentScheduler = (managedScheduledExecutorServiceClass != null &&
managedScheduledExecutorServiceClass.isInstance(scheduledExecutor));
}
/**
@@ -162,7 +161,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
* as well, pass the same executor reference to {@link #setConcurrentExecutor}.
* @see #setConcurrentExecutor
*/
public void setScheduledExecutor(@Nullable ScheduledExecutorService scheduledExecutor) {
public void setScheduledExecutor(ScheduledExecutorService scheduledExecutor) {
initScheduledExecutor(scheduledExecutor);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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,7 +24,6 @@ import javax.naming.NamingException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jndi.JndiLocatorDelegate;
import org.springframework.jndi.JndiTemplate;
import org.springframework.lang.Nullable;
/**
* JNDI-based variant of {@link ConcurrentTaskExecutor}, performing a default lookup for
@@ -43,10 +42,15 @@ public class DefaultManagedTaskExecutor extends ConcurrentTaskExecutor implement
private final JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate();
@Nullable
private String jndiName = "java:comp/DefaultManagedExecutorService";
public DefaultManagedTaskExecutor() {
// Executor initialization happens in afterPropertiesSet
super(null);
}
/**
* Set the JNDI template to use for JNDI lookups.
* @see org.springframework.jndi.JndiAccessor#setJndiTemplate
@@ -87,9 +91,7 @@ public class DefaultManagedTaskExecutor extends ConcurrentTaskExecutor implement
@Override
public void afterPropertiesSet() throws NamingException {
if (this.jndiName != null) {
setConcurrentExecutor(this.jndiLocator.lookup(this.jndiName, Executor.class));
}
setConcurrentExecutor(this.jndiLocator.lookup(this.jndiName, Executor.class));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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,7 +24,6 @@ import javax.naming.NamingException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jndi.JndiLocatorDelegate;
import org.springframework.jndi.JndiTemplate;
import org.springframework.lang.Nullable;
/**
* JNDI-based variant of {@link ConcurrentTaskScheduler}, performing a default lookup for
@@ -43,10 +42,15 @@ public class DefaultManagedTaskScheduler extends ConcurrentTaskScheduler impleme
private final JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate();
@Nullable
private String jndiName = "java:comp/DefaultManagedScheduledExecutorService";
public DefaultManagedTaskScheduler() {
// Executor initialization happens in afterPropertiesSet
super(null);
}
/**
* Set the JNDI template to use for JNDI lookups.
* @see org.springframework.jndi.JndiAccessor#setJndiTemplate
@@ -87,11 +91,9 @@ public class DefaultManagedTaskScheduler extends ConcurrentTaskScheduler impleme
@Override
public void afterPropertiesSet() throws NamingException {
if (this.jndiName != null) {
ScheduledExecutorService executor = this.jndiLocator.lookup(this.jndiName, ScheduledExecutorService.class);
setConcurrentExecutor(executor);
setScheduledExecutor(executor);
}
ScheduledExecutorService executor = this.jndiLocator.lookup(this.jndiName, ScheduledExecutorService.class);
setConcurrentExecutor(executor);
setScheduledExecutor(executor);
}
}