Provide a dedicated exception if ScheduledExecutor is not configured
While it is not easily possible to create a ConcurrentTaskScheduler with no scheduled executor, DefaultManagedTaskScheduler has a default constructor that does that as the JNDI lookup happens in the regular afterPropertiesSet callback. If such an instance is created manually, it can throw an unhelpful NullPointerException if one attempts to schedule a task. This commit updates ConcurrentTaskScheduler to flag that the scheduled executor could be `null` and check if that's the case before using it. This now throws a dedicated exception that should hopefully provide better context for those upgrading. See gh-27914 Closes gh-31751
This commit is contained in:
@@ -89,6 +89,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
private ScheduledExecutorService scheduledExecutor;
|
||||
|
||||
private boolean enterpriseConcurrentScheduler = false;
|
||||
@@ -168,6 +169,13 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
|
||||
initScheduledExecutor(scheduledExecutor);
|
||||
}
|
||||
|
||||
private ScheduledExecutorService getScheduledExecutor() {
|
||||
if (this.scheduledExecutor == null) {
|
||||
throw new IllegalStateException("No ScheduledExecutor is configured");
|
||||
}
|
||||
return this.scheduledExecutor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide an {@link ErrorHandler} strategy.
|
||||
*/
|
||||
@@ -195,6 +203,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
|
||||
@Override
|
||||
@Nullable
|
||||
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
|
||||
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
|
||||
try {
|
||||
if (this.enterpriseConcurrentScheduler) {
|
||||
return new EnterpriseConcurrentTriggerScheduler().schedule(decorateTask(task, true), trigger);
|
||||
@@ -202,68 +211,73 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
|
||||
else {
|
||||
ErrorHandler errorHandler =
|
||||
(this.errorHandler != null ? this.errorHandler : TaskUtils.getDefaultErrorHandler(true));
|
||||
return new ReschedulingRunnable(task, trigger, this.clock, this.scheduledExecutor, errorHandler).schedule();
|
||||
return new ReschedulingRunnable(task, trigger, this.clock, scheduleExecutorToUse, errorHandler).schedule();
|
||||
}
|
||||
}
|
||||
catch (RejectedExecutionException ex) {
|
||||
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
|
||||
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
|
||||
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
|
||||
Duration delay = Duration.between(this.clock.instant(), startTime);
|
||||
try {
|
||||
return this.scheduledExecutor.schedule(decorateTask(task, false), NANO.convert(delay), NANO);
|
||||
return scheduleExecutorToUse.schedule(decorateTask(task, false), NANO.convert(delay), NANO);
|
||||
}
|
||||
catch (RejectedExecutionException ex) {
|
||||
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
|
||||
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period) {
|
||||
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
|
||||
Duration initialDelay = Duration.between(this.clock.instant(), startTime);
|
||||
try {
|
||||
return this.scheduledExecutor.scheduleAtFixedRate(decorateTask(task, true),
|
||||
return scheduleExecutorToUse.scheduleAtFixedRate(decorateTask(task, true),
|
||||
NANO.convert(initialDelay), NANO.convert(period), NANO);
|
||||
}
|
||||
catch (RejectedExecutionException ex) {
|
||||
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
|
||||
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
|
||||
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
|
||||
try {
|
||||
return this.scheduledExecutor.scheduleAtFixedRate(decorateTask(task, true),
|
||||
return scheduleExecutorToUse.scheduleAtFixedRate(decorateTask(task, true),
|
||||
0, NANO.convert(period), NANO);
|
||||
}
|
||||
catch (RejectedExecutionException ex) {
|
||||
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
|
||||
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay) {
|
||||
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
|
||||
Duration initialDelay = Duration.between(this.clock.instant(), startTime);
|
||||
try {
|
||||
return this.scheduledExecutor.scheduleWithFixedDelay(decorateTask(task, true),
|
||||
return scheduleExecutorToUse.scheduleWithFixedDelay(decorateTask(task, true),
|
||||
NANO.convert(initialDelay), NANO.convert(delay), NANO);
|
||||
}
|
||||
catch (RejectedExecutionException ex) {
|
||||
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
|
||||
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay) {
|
||||
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
|
||||
try {
|
||||
return this.scheduledExecutor.scheduleWithFixedDelay(decorateTask(task, true),
|
||||
return scheduleExecutorToUse.scheduleWithFixedDelay(decorateTask(task, true),
|
||||
0, NANO.convert(delay), NANO);
|
||||
}
|
||||
catch (RejectedExecutionException ex) {
|
||||
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
|
||||
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,7 +297,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
|
||||
private class EnterpriseConcurrentTriggerScheduler {
|
||||
|
||||
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
|
||||
ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService) scheduledExecutor;
|
||||
ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService) getScheduledExecutor();
|
||||
return executor.schedule(task, new TriggerAdapter(trigger));
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ import org.springframework.jndi.JndiTemplate;
|
||||
/**
|
||||
* JNDI-based variant of {@link ConcurrentTaskScheduler}, performing a default lookup for
|
||||
* JSR-236's "java:comp/DefaultManagedScheduledExecutorService" in a Jakarta EE environment.
|
||||
* Expected to be exposed as a bean, in particular as the default lookup happens in the
|
||||
* standard {@link InitializingBean#afterPropertiesSet()} callback.
|
||||
*
|
||||
* <p>Note: This class is not strictly JSR-236 based; it can work with any regular
|
||||
* {@link java.util.concurrent.ScheduledExecutorService} that can be found in JNDI.
|
||||
|
||||
Reference in New Issue
Block a user