How to parameterize @Scheduled(fixedDelay) with Spring 3.0 expression language?

ngeek picture ngeek · Apr 8, 2010 · Viewed 90.5k times · Source

When using the Spring 3.0 capability to annotate a scheduled task, I would like to set the fixedDelay as parameter from my configuration file, instead of hard-wiring it into my task class, like currently...

@Scheduled(fixedDelay = 5000)
public void readLog() {
        ...
}

Unfortunately it seems that with the means of the Spring Expression Language (SpEL) @Value returns a String object which in turn is not able to be auto-boxed to a long value as required by the fixedDelay parameter.

Answer

Mark-A picture Mark-A · May 9, 2013

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() {
        ...
}