views:

369

answers:

2

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 (EL) @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.

Thanks in advance for your help on this.

A: 

I guess you can convert the value yourself by defining a bean. I haven't tried that, but I guess the approach similar to the following might be useful for you:

<bean id="FixedDelayLongValue" class="java.lang.Long"
      factory-method="valueOf">
    <constructor-arg value="#{YourConfigurationBean.stringValue}"/>
</bean>

where:

<bean id="YourConfigurationBean" class="...">
         <property name="stringValue" value="5000"/>
</bean>
Grzegorz Oledzki
Thanks, that sounds like one way, but to be honest I was hoping for a more elegant ("springish") solution :-)
ngeek
Unfortunately this will not work, since the @Scheduled annotation attribute fixedDelay requires a (long) constant to be assigned.
ngeek
Ah, that's right. So I guess you can't do it with the `@Scheduled` annotation then.
Grzegorz Oledzki
+1  A: 

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.

Grzegorz Oledzki
Thanks, the XML configuration did the trick. I am bit surprised that the annotation seems so bound to string values, anyways, I go with the old-school way ;-)
ngeek