Skip to content

Commit 4235a11

Browse files
committed
Throw IllegalArgumentException for unsupported Duration values
Closes gh-31210
1 parent 966b0a9 commit 4235a11

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
* "fixedRate", "fixedDelay", or "cron" expression provided via the annotation.
8484
*
8585
* <p>This post-processor is automatically registered by Spring's
86-
* {@code <task:annotation-driven>} XML element, and also by the
86+
* {@code <task:annotation-driven>} XML element and also by the
8787
* {@link EnableScheduling @EnableScheduling} annotation.
8888
*
8989
* <p>Autodetects any {@link SchedulingConfigurer} instances in the container,
@@ -395,8 +395,7 @@ protected void processScheduled(Scheduled scheduled, Method method, Object bean)
395395
try {
396396
Runnable runnable = createRunnable(bean, method);
397397
boolean processedSchedule = false;
398-
String errorMessage =
399-
"Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required";
398+
String errorMessage = "Exactly one of the 'cron', 'fixedDelay' or 'fixedRate' attributes is required";
400399

401400
Set<ScheduledTask> tasks = new LinkedHashSet<>(4);
402401

@@ -455,7 +454,6 @@ protected void processScheduled(Scheduled scheduled, Method method, Object bean)
455454
processedSchedule = true;
456455
tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay)));
457456
}
458-
459457
String fixedDelayString = scheduled.fixedDelayString();
460458
if (StringUtils.hasText(fixedDelayString)) {
461459
if (this.embeddedValueResolver != null) {
@@ -532,7 +530,13 @@ protected Runnable createRunnable(Object target, Method method) {
532530
}
533531

534532
private static Duration toDuration(long value, TimeUnit timeUnit) {
535-
return Duration.of(value, timeUnit.toChronoUnit());
533+
try {
534+
return Duration.of(value, timeUnit.toChronoUnit());
535+
}
536+
catch (Exception ex) {
537+
throw new IllegalArgumentException(
538+
"Unsupported unit " + timeUnit + " for value \"" + value + "\": " + ex.getMessage());
539+
}
536540
}
537541

538542
private static Duration toDuration(String value, TimeUnit timeUnit) {

0 commit comments

Comments
 (0)