From 4235a11c4f137e92d48bb20336c1d2323ecdac20 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 13 Sep 2023 17:15:32 +0200 Subject: [PATCH] Throw IllegalArgumentException for unsupported Duration values Closes gh-31210 --- .../ScheduledAnnotationBeanPostProcessor.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java index 1d6f002d09a1..07075d9688a8 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java @@ -83,7 +83,7 @@ * "fixedRate", "fixedDelay", or "cron" expression provided via the annotation. * *

This post-processor is automatically registered by Spring's - * {@code } XML element, and also by the + * {@code } XML element and also by the * {@link EnableScheduling @EnableScheduling} annotation. * *

Autodetects any {@link SchedulingConfigurer} instances in the container, @@ -395,8 +395,7 @@ protected void processScheduled(Scheduled scheduled, Method method, Object bean) try { Runnable runnable = createRunnable(bean, method); boolean processedSchedule = false; - String errorMessage = - "Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required"; + String errorMessage = "Exactly one of the 'cron', 'fixedDelay' or 'fixedRate' attributes is required"; Set tasks = new LinkedHashSet<>(4); @@ -455,7 +454,6 @@ protected void processScheduled(Scheduled scheduled, Method method, Object bean) processedSchedule = true; tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay))); } - String fixedDelayString = scheduled.fixedDelayString(); if (StringUtils.hasText(fixedDelayString)) { if (this.embeddedValueResolver != null) { @@ -532,7 +530,13 @@ protected Runnable createRunnable(Object target, Method method) { } private static Duration toDuration(long value, TimeUnit timeUnit) { - return Duration.of(value, timeUnit.toChronoUnit()); + try { + return Duration.of(value, timeUnit.toChronoUnit()); + } + catch (Exception ex) { + throw new IllegalArgumentException( + "Unsupported unit " + timeUnit + " for value \"" + value + "\": " + ex.getMessage()); + } } private static Duration toDuration(String value, TimeUnit timeUnit) {