Skip to content

Commit

Permalink
Throw IllegalArgumentException for unsupported Duration values
Browse files Browse the repository at this point in the history
Closes gh-31210
  • Loading branch information
jhoeller committed Sep 13, 2023
1 parent 966b0a9 commit 4235a11
Showing 1 changed file with 9 additions and 5 deletions.
Expand Up @@ -83,7 +83,7 @@
* "fixedRate", "fixedDelay", or "cron" expression provided via the annotation.
*
* <p>This post-processor is automatically registered by Spring's
* {@code <task:annotation-driven>} XML element, and also by the
* {@code <task:annotation-driven>} XML element and also by the
* {@link EnableScheduling @EnableScheduling} annotation.
*
* <p>Autodetects any {@link SchedulingConfigurer} instances in the container,
Expand Down Expand Up @@ -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<ScheduledTask> tasks = new LinkedHashSet<>(4);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4235a11

Please sign in to comment.