Search code examples
springscheduled-tasksjndi

Spring task scheduler delay from jndi


I'm configuring a Spring task scheduler.

<task:scheduled-tasks scheduler="myScheduler">
        <task:scheduled ref="servicesConfigurationBean" method="loadResources" fixed-delay="300000" />
</task:scheduled-tasks>

It's working properly. Now I would like to set the delay value from a JNDI lookup. So I tried the following:

<task:scheduled ref="servicesConfigurationBean" method="loadResources">
    <property name="fixed-delay"><ref local="servicesRefreshRate"/></property>
</task:scheduled>

But now I obtain the following exception:

[/WEB-INF/spring/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.1: Element 'task:scheduled' must have no character or element information item [children], because the type's content type is empty.[/WEB-INF/spring/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.1: Element 'task:scheduled' must have no character or element information item [children], because the type's content type is empty.

I understand the cause of the exception, so, is there a viable solution to my problem? Thanks.


Solution

  • Using <property> won't work, because <task:scheduled> is a macro, not a bean definition. The two work very differently.

    Try using Spring-EL instead:

    <task:scheduled ref="servicesConfigurationBean" 
                    method="loadResources" 
                    fixed-delay="#servicesRefreshRate" />
    

    I haven't tried it, but give it a go.