I have a @ConfigurationProperties class with Duration fields. I'd like to reference one of these fields when declaring an annotation like @Scheduled(fixedRateString = "${my-prop.rate}").
To do this I have to specify the value for the property using the java.time.Duration syntax. It would be nice to be able to use the flexible values introduced in #11078.
Comment From: wilkinsona
Thanks for the suggestion but it's Spring Framework that parses the String into a Duration and it hardcodes the use of Duration.parse().
In Boot 2.1, the application context's conversion service is configured to be capable of performing String to Duration conversion with support for flexible values so this would work if ScheduledAnnotationBeanPostProcessor used the context's conversion service, but I'm not sure if that's possible. If you'd like to pursue your suggestion, please open a Spring Framework enhancement request in JIRA and comment here with a link to it.
Comment From: manderson23
I've opened https://jira.spring.io/browse/SPR-17481
Comment From: igorbolic
I have a
@ConfigurationPropertiesclass withDurationfields. I'd like to reference one of these fields when declaring an annotation like@Scheduled(fixedRateString = "${my-prop.rate}").To do this I have to specify the value for the property using the
java.time.Durationsyntax. It would be nice to be able to use the flexible values introduced in #11078.
It would be nice to have it supported auto-of-the-box.
I was able to reuse Spring Boot's DurationStyle to have more readable configuration properties e.g.:
@Scheduled(fixedRateString = "#{T(org.springframework.boot.convert.DurationStyle).detectAndParse('${my-prop.rate}')}")
and then in application configuration yaml I have it configured like this for rate of 25 seconds:
my-prop.rate: 25s
It does look a bit nasty using SpEL in the @Scheduled annotation, but it makes the configuration values more readable.
I'm not sure if there any plans of including / moving DurationStyle in Spring Framework. The ScheduledAnnotationBeanPostProcessor could than use this parser, instead of java.time.Duration.parse()
Comment From: wilkinsona
Thanks for noting the SpEL-based workaround.
I'm not sure if there any plans of including / moving DurationStyle in Spring Framework.
As noted above, @manderson23 opened a Framework issue for this: https://github.com/spring-projects/spring-framework/issues/22013.
Comment From: gavenkoa
I'm not sure what has changed since the report, with Boot 3.7 property defaults without PT prefix are not allowed:
@Scheduled(fixedDelayString = "${app.freq:10m}")
tests fail to initialize:
IllegalStateException: Encountered invalid @Scheduled method 'clearJobRepositories': Invalid fixedDelayString value "10m" - cannot parse into long
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:518)
but with PT prefix they pass (I'm not sure about correctness of data conversion though, for me enough: tests are passing):
@Scheduled(fixedDelayString = "${app.freq:PT10m}")
Comment From: snicoll
@gavenkoa please read the comment above yours. This issue has been marked invalid.
Comment From: JavaOPs
It's a pity, that this helpful functional is not realized yet. I've try a little less nasty SPEL workaround, but it doesn't work
@Scheduled(fixedRateString = "'PT'.concat('${app.update-cache}')")
Why?