This is similar as https://github.com/spring-projects/spring-framework/issues/30754
Note I can confirm the original issue is solved in Spring 6.0.11 as this works:
@Scheduled(initialDelay = 5, fixedDelay = Long.MAX_VALUE)
but using:
@Scheduled(initialDelay = 5, fixedDelay = Long.MAX_VALUE, timeUnit = TimeUnit.MINUTES)
still gives the ArithmeticException:
Caused by: java.lang.ArithmeticException: long overflow
at java.base/java.lang.Math.multiplyExact(Math.java:1004) ~[na:na]
at java.base/java.time.Duration.plus(Duration.java:729) ~[na:na]
at java.base/java.time.Duration.of(Duration.java:312) ~[na:na]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.toDuration(ScheduledAnnotationBeanPostProcessor.java:535) ~[spring-context-6.0.11.jar:6.0.11]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:452) ~[spring-context-6.0.11.jar:6.0.11]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$1(ScheduledAnnotationBeanPostProcessor.java:377) ~[spring-context-6.0.11.jar:6.0.11]
at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:377) ~[spring-context-6.0.11.jar:6.0.11]
at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:721) ~[na:na]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:376) ~[spring-context-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:434) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1773) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.11.jar:6.0.11]
Comment From: jhoeller
That's an earlier arithmetic exception in the Duration.of
factory method, so it is not directly related to our nanoseconds revision. I suppose we could catch the exception and throw a better-worded IllegalArgumentException
there.
Comment From: marceloverdijk
Indeed, similar as Duration.of(Long.MAX_VALUE, ChronoUnit.MINUTES)
gives the same ArithmeticException
.
Maybe it would be more interesting to have the possibility to run @Scheduled
only once (with an initial delay)?
Comment From: jhoeller
That's a good point. Rather than insisting on cron/fixedDelay/fixedRate to be specified, we could accept just an initial delay for a one-time task, delegating to TaskScheduler.schedule(Runnable task, Instant startTime)
. Please create a separate ticket for that, I'll try to roll this into 6.1 then.