-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
115 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...test/java/org/springframework/scheduling/concurrent/DefaultManagedTaskSchedulerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.scheduling.concurrent; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
import org.assertj.core.api.ThrowableAssert.ThrowingCallable; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.scheduling.Trigger; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* Tests for {@link DefaultManagedTaskScheduler}. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
class DefaultManagedTaskSchedulerTests { | ||
|
||
private final Runnable NO_OP = () -> {}; | ||
|
||
@Test | ||
void scheduleWithTriggerAndNoScheduledExecutorProvidesDedicatedException() { | ||
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler(); | ||
assertNoExecutorException(() -> scheduler.schedule(NO_OP, mock(Trigger.class))); | ||
} | ||
|
||
@Test | ||
void scheduleWithInstantAndNoScheduledExecutorProvidesDedicatedException() { | ||
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler(); | ||
assertNoExecutorException(() -> scheduler.schedule(NO_OP, Instant.now())); | ||
} | ||
|
||
@Test | ||
void scheduleAtFixedRateWithStartTimeAndDurationAndNoScheduledExecutorProvidesDedicatedException() { | ||
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler(); | ||
assertNoExecutorException(() -> scheduler.scheduleAtFixedRate( | ||
NO_OP, Instant.now(), Duration.of(1, ChronoUnit.MINUTES))); | ||
} | ||
|
||
@Test | ||
void scheduleAtFixedRateWithDurationAndNoScheduledExecutorProvidesDedicatedException() { | ||
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler(); | ||
assertNoExecutorException(() -> scheduler.scheduleAtFixedRate( | ||
NO_OP, Duration.of(1, ChronoUnit.MINUTES))); | ||
} | ||
|
||
@Test | ||
void scheduleWithFixedDelayWithStartTimeAndDurationAndNoScheduledExecutorProvidesDedicatedException() { | ||
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler(); | ||
assertNoExecutorException(() -> scheduler.scheduleWithFixedDelay( | ||
NO_OP, Instant.now(), Duration.of(1, ChronoUnit.MINUTES))); | ||
} | ||
|
||
@Test | ||
void scheduleWithFixedDelayWithDurationAndNoScheduledExecutorProvidesDedicatedException() { | ||
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler(); | ||
assertNoExecutorException(() -> scheduler.scheduleWithFixedDelay( | ||
NO_OP, Duration.of(1, ChronoUnit.MINUTES))); | ||
} | ||
|
||
private void assertNoExecutorException(ThrowingCallable callable) { | ||
assertThatThrownBy(callable) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("No ScheduledExecutor is configured"); | ||
} | ||
|
||
} |