Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay task:scheduler first execution in Spring 3

Tags:

java

spring

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 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?

like image 497
Gerco Dries Avatar asked Jan 17 '10 10:01

Gerco Dries


People also ask

What is initial delay in spring scheduler?

Initial delay is a period of time that Scheduler is waiting for before the configured task is executed. InitialDelay is a element in @Scheduled annotation that is added to a method.

What is fixed delay in spring scheduler?

Schedule a Task at Fixed Delay In this case, the duration between the end of the last execution and the start of the next execution is fixed. The task always waits until the previous one is finished. This option should be used when it's mandatory that the previous execution is completed before running again.

What is @scheduled in spring?

Spring Core. Spring provides excellent support for both task scheduling and asynchronous method execution based on cron expression using @Scheduled annotation. The @Scheduled annotation can be added to a method along with trigger metadata.


1 Answers

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.

like image 182
skaffman Avatar answered Sep 26 '22 02:09

skaffman