Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support flexible duration parsing in placeholders for @Scheduled annotations #15126

Closed
manderson23 opened this issue Nov 7, 2018 · 7 comments
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid

Comments

@manderson23
Copy link

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 7, 2018
@wilkinsona
Copy link
Member

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.

@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 7, 2018
@manderson23
Copy link
Author

I've opened https://jira.spring.io/browse/SPR-17481

@igorbolic
Copy link

igorbolic commented Feb 19, 2020

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.

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()

@wilkinsona
Copy link
Member

wilkinsona commented Feb 19, 2020

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: spring-projects/spring-framework#22013.

@gavenkoa
Copy link

gavenkoa commented Sep 20, 2022

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}")

@snicoll
Copy link
Member

snicoll commented Sep 20, 2022

@gavenkoa please read the comment above yours. This issue has been marked invalid.

@JavaOPs
Copy link

JavaOPs commented Jan 27, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

7 participants