Spring scheduled fixedRateString as Duration
Asked Answered
I

2

7

The @Scheduled documentation here states that the fixedRateString value can be the delay in milliseconds as a String value, e.g. a placeholder or a java.time.Duration compliant value. Meaning I can either write

@Scheduled(fixedRateString = "45s")

OR

@Scheduled(fixedRateString = "45000")

And it should be the same. However when I try to run it I get

Encountered invalid @Scheduled method 'updateWarmupInstances': Invalid fixedRateString value "45s" - cannot parse into long

So it this a mistake on Spring's part or am I doing something wrong ?

Indigested answered 12/12, 2018 at 0:19 Comment(1)
FYI an issue was opened recently requesting flexible duration parsing of the kind you and I both expected. Turns out it's a Spring framework issue, not Spring Boot: github.com/spring-projects/spring-framework/issues/22013Fitts
G
4

You are looking at the return value of the method, not the input. The input can only be a String in milliseconds, but the return value is a value compliant with Duration.

Goldarn answered 12/12, 2018 at 0:31 Comment(1)
Return value of… what method? The fixedRateString attribute of the annotation? Or are you suggesting that the annotated method can return either a String or Duration to describe the frequency with which it will be called?Romney
S
11

To use the method @Scheduled(fixedRateString) for durations, you could use a String with the standard duration:

@Scheduled(fixedRateString = "PT45S")

The prefix PT is for ISO-8601 standard, and in this example, it means 45 seconds.

Another example could be a duration of 1h:

@Scheduled(fixedRateString = "PT1H")
Shortsighted answered 25/2, 2021 at 14:30 Comment(0)
G
4

You are looking at the return value of the method, not the input. The input can only be a String in milliseconds, but the return value is a value compliant with Duration.

Goldarn answered 12/12, 2018 at 0:31 Comment(1)
Return value of… what method? The fixedRateString attribute of the annotation? Or are you suggesting that the annotated method can return either a String or Duration to describe the frequency with which it will be called?Romney

© 2022 - 2024 — McMap. All rights reserved.