开发者

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 ...
</task:scheduled-tasks>

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

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 th开发者_开发问答e 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?


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.


I've had the same problem and came back to TimerTask as it is in 25.7.1 point in http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html

<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <!--  wait 25 seconds before starting repeated execution --> 
    <property name="delay" value="25000" />
    <!--  run every 50 seconds -->
    <property name="period" value="50000" />
    <property name="timerTask" ref="task" />
</bean>

<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref bean="scheduledTask" />
        </list>
    </property>
</bean>

I hope in Spring 3.1 will be initialDelay attribute in <task:scheduled>, since in Spring 3.0 TimerFactoryBean is Deprecated. You can vote for this issue: jira.springframework.org/browse/SPR-7022


This has been introduced by the way in spring 3.2 so if you use the 3.2 schema it's available again -- e.g.:

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

....

the above allows you to do this:

<task:scheduler id="scheduler" pool-size="1"/>
<task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="synchronizer" method="incrementalSync" fixed-delay="600000" initial-delay="initial delay needed for the app to start"/>
    ... more tasks ...
</task:scheduled-tasks>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜