Skip to content

Commit b78aed9

Browse files
committed
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
1 parent 2eba351 commit b78aed9

File tree

3 files changed

+115
-13
lines changed

3 files changed

+115
-13
lines changed

spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskScheduler.java

+27-13
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
8989
}
9090

9191

92+
@Nullable
9293
private ScheduledExecutorService scheduledExecutor;
9394

9495
private boolean enterpriseConcurrentScheduler = false;
@@ -168,6 +169,13 @@ public void setScheduledExecutor(ScheduledExecutorService scheduledExecutor) {
168169
initScheduledExecutor(scheduledExecutor);
169170
}
170171

172+
private ScheduledExecutorService getScheduledExecutor() {
173+
if (this.scheduledExecutor == null) {
174+
throw new IllegalStateException("No ScheduledExecutor is configured");
175+
}
176+
return this.scheduledExecutor;
177+
}
178+
171179
/**
172180
* Provide an {@link ErrorHandler} strategy.
173181
*/
@@ -195,75 +203,81 @@ public Clock getClock() {
195203
@Override
196204
@Nullable
197205
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
206+
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
198207
try {
199208
if (this.enterpriseConcurrentScheduler) {
200209
return new EnterpriseConcurrentTriggerScheduler().schedule(decorateTask(task, true), trigger);
201210
}
202211
else {
203212
ErrorHandler errorHandler =
204213
(this.errorHandler != null ? this.errorHandler : TaskUtils.getDefaultErrorHandler(true));
205-
return new ReschedulingRunnable(task, trigger, this.clock, this.scheduledExecutor, errorHandler).schedule();
214+
return new ReschedulingRunnable(task, trigger, this.clock, scheduleExecutorToUse, errorHandler).schedule();
206215
}
207216
}
208217
catch (RejectedExecutionException ex) {
209-
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
218+
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
210219
}
211220
}
212221

213222
@Override
214223
public ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
224+
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
215225
Duration delay = Duration.between(this.clock.instant(), startTime);
216226
try {
217-
return this.scheduledExecutor.schedule(decorateTask(task, false), NANO.convert(delay), NANO);
227+
return scheduleExecutorToUse.schedule(decorateTask(task, false), NANO.convert(delay), NANO);
218228
}
219229
catch (RejectedExecutionException ex) {
220-
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
230+
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
221231
}
222232
}
223233

224234
@Override
225235
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period) {
236+
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
226237
Duration initialDelay = Duration.between(this.clock.instant(), startTime);
227238
try {
228-
return this.scheduledExecutor.scheduleAtFixedRate(decorateTask(task, true),
239+
return scheduleExecutorToUse.scheduleAtFixedRate(decorateTask(task, true),
229240
NANO.convert(initialDelay), NANO.convert(period), NANO);
230241
}
231242
catch (RejectedExecutionException ex) {
232-
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
243+
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
233244
}
234245
}
235246

236247
@Override
237248
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
249+
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
238250
try {
239-
return this.scheduledExecutor.scheduleAtFixedRate(decorateTask(task, true),
251+
return scheduleExecutorToUse.scheduleAtFixedRate(decorateTask(task, true),
240252
0, NANO.convert(period), NANO);
241253
}
242254
catch (RejectedExecutionException ex) {
243-
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
255+
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
244256
}
245257
}
246258

247259
@Override
248260
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay) {
261+
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
249262
Duration initialDelay = Duration.between(this.clock.instant(), startTime);
250263
try {
251-
return this.scheduledExecutor.scheduleWithFixedDelay(decorateTask(task, true),
264+
return scheduleExecutorToUse.scheduleWithFixedDelay(decorateTask(task, true),
252265
NANO.convert(initialDelay), NANO.convert(delay), NANO);
253266
}
254267
catch (RejectedExecutionException ex) {
255-
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
268+
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
256269
}
257270
}
258271

259272
@Override
260273
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay) {
274+
ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor();
261275
try {
262-
return this.scheduledExecutor.scheduleWithFixedDelay(decorateTask(task, true),
276+
return scheduleExecutorToUse.scheduleWithFixedDelay(decorateTask(task, true),
263277
0, NANO.convert(delay), NANO);
264278
}
265279
catch (RejectedExecutionException ex) {
266-
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
280+
throw new TaskRejectedException(scheduleExecutorToUse, task, ex);
267281
}
268282
}
269283

@@ -283,7 +297,7 @@ private Runnable decorateTask(Runnable task, boolean isRepeatingTask) {
283297
private class EnterpriseConcurrentTriggerScheduler {
284298

285299
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
286-
ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService) scheduledExecutor;
300+
ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService) getScheduledExecutor();
287301
return executor.schedule(task, new TriggerAdapter(trigger));
288302
}
289303

spring-context/src/main/java/org/springframework/scheduling/concurrent/DefaultManagedTaskScheduler.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
/**
2929
* JNDI-based variant of {@link ConcurrentTaskScheduler}, performing a default lookup for
3030
* JSR-236's "java:comp/DefaultManagedScheduledExecutorService" in a Jakarta EE environment.
31+
* Expected to be exposed as a bean, in particular as the default lookup happens in the
32+
* standard {@link InitializingBean#afterPropertiesSet()} callback.
3133
*
3234
* <p>Note: This class is not strictly JSR-236 based; it can work with any regular
3335
* {@link java.util.concurrent.ScheduledExecutorService} that can be found in JNDI.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scheduling.concurrent;
18+
19+
import java.time.Duration;
20+
import java.time.Instant;
21+
import java.time.temporal.ChronoUnit;
22+
23+
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.scheduling.Trigger;
27+
28+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
29+
import static org.mockito.Mockito.mock;
30+
31+
/**
32+
* Tests for {@link DefaultManagedTaskScheduler}.
33+
*
34+
* @author Stephane Nicoll
35+
*/
36+
class DefaultManagedTaskSchedulerTests {
37+
38+
private final Runnable NO_OP = () -> {};
39+
40+
@Test
41+
void scheduleWithTriggerAndNoScheduledExecutorProvidesDedicatedException() {
42+
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler();
43+
assertNoExecutorException(() -> scheduler.schedule(NO_OP, mock(Trigger.class)));
44+
}
45+
46+
@Test
47+
void scheduleWithInstantAndNoScheduledExecutorProvidesDedicatedException() {
48+
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler();
49+
assertNoExecutorException(() -> scheduler.schedule(NO_OP, Instant.now()));
50+
}
51+
52+
@Test
53+
void scheduleAtFixedRateWithStartTimeAndDurationAndNoScheduledExecutorProvidesDedicatedException() {
54+
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler();
55+
assertNoExecutorException(() -> scheduler.scheduleAtFixedRate(
56+
NO_OP, Instant.now(), Duration.of(1, ChronoUnit.MINUTES)));
57+
}
58+
59+
@Test
60+
void scheduleAtFixedRateWithDurationAndNoScheduledExecutorProvidesDedicatedException() {
61+
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler();
62+
assertNoExecutorException(() -> scheduler.scheduleAtFixedRate(
63+
NO_OP, Duration.of(1, ChronoUnit.MINUTES)));
64+
}
65+
66+
@Test
67+
void scheduleWithFixedDelayWithStartTimeAndDurationAndNoScheduledExecutorProvidesDedicatedException() {
68+
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler();
69+
assertNoExecutorException(() -> scheduler.scheduleWithFixedDelay(
70+
NO_OP, Instant.now(), Duration.of(1, ChronoUnit.MINUTES)));
71+
}
72+
73+
@Test
74+
void scheduleWithFixedDelayWithDurationAndNoScheduledExecutorProvidesDedicatedException() {
75+
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler();
76+
assertNoExecutorException(() -> scheduler.scheduleWithFixedDelay(
77+
NO_OP, Duration.of(1, ChronoUnit.MINUTES)));
78+
}
79+
80+
private void assertNoExecutorException(ThrowingCallable callable) {
81+
assertThatThrownBy(callable)
82+
.isInstanceOf(IllegalStateException.class)
83+
.hasMessage("No ScheduledExecutor is configured");
84+
}
85+
86+
}

0 commit comments

Comments
 (0)