How to parameterize @Scheduled(fixedDelay) with Spring 3.0 expression language?
Solution 1:
Spring v3.2.2 has added String parameters to the original 3 long parameters to handle this. fixedDelayString
, fixedRateString
and initialDelayString
are now available too.
@Scheduled(fixedDelayString = "${my.fixed.delay.prop}")
public void readLog() {
...
}
Solution 2:
You can use the @Scheduled
annotation, but together with the cron
parameter only:
@Scheduled(cron = "${yourConfiguration.cronExpression}")
Your 5 seconds interval could be expressed as "*/5 * * * * *"
. However as I understand you cannot provide less than 1 second precision.
Solution 3:
I guess the @Scheduled
annotation is out of question. So maybe a solution for you would be to use task-scheduled
XML configuration. Let's consider this example (copied from Spring doc):
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="someObject" method="readLog"
fixed-rate="#{YourConfigurationBean.stringValue}"/>
</task:scheduled-tasks>
... or if the cast from String to Long didn't work, something like this would:
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="someObject" method="readLog"
fixed-rate="#{T(java.lang.Long).valueOf(YourConfigurationBean.stringValue)}"/>
</task:scheduled-tasks>
Again, I haven't tried any of these setups, but I hope it might help you a bit.