Thursday, April 21, 2011

Delay task:scheduler first execution in Spring 3

I have a simple application that uses Spring 3 for dependency injection. I have a JFrame for the user to look at and some background tasks for synchronizing with a back-end server and local database maintenance.

This is the relevant part of my application context:

<task:scheduler id="scheduler" pool-size="1"/>
<task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="synchronizer" method="incrementalSync" fixed-delay="600000"/>
    ... more tasks ...

<bean id="mainFrame" class="nl.gdries.myapp.client.ui.MainFrame">
    ... properties and such ...

When I start this applicationContext the scheduler immediately starts executing the background tasks even while my UI is loading. Because the first task is a rather heavy one at the start I want it to wait for the UI to fully load and display before it starts execution.

Does anyone know how to tell Spring to delay executing the scheduled tasks until a moment of my choosing?

From stackoverflow
  • This seems to have been left out of the <task:scheduled> bean definition, something I only just noticed last week.

    Remember, though, that the <task:...> definitions are just shortcuts, you can always use the explicit approach, by defining a ScheduledExecutorFactoryBean, with nested ScheduledExecutorTask beans. This gives you much finer control, including initialDelay.


Post a Comment