August 23, 2009

Spring Scheduler - ScheduledTimerTask

« Java Twitter | Main | Spring Quartz Example »

The easiest way to realize a scheduler in Spring is to use the standard JDK timer support. The following examples describes a class which pools the database for new orders. First you have to implement your task class which includes the pooling business logic. After that you references the task class at the ScheduledTimerTask bean. The ScheduledTimerTask can be configured with a delay and a period parameter. The delay parameter regulate the first start of the timer task and the period parameter defines the iteration time.

public class PoolDBTask extends TimerTask {
  public void run() {
     //pooling of order db
  }
}
<bean id="poolDBTask" class="org.developers.blog.spring.scheduling.PoolDBTask"/>
<bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <!-- wait 5 seconds -->
    <property name="delay" value="5000" />
    <!-- run every hour -->
    <property name="period" value="3600000" />
    <property name="poolDBTask" ref="poolDBTask" />
</bean>

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 7:47 PM in Spring

 

[Trackback URL for this entry]

Comment: Nesho at Di, 10 Nov 10:18 AM

Hello,
I want to ask is it possible implement with spring this scenario:
repeat{
startTask{
doJob()
sleep for 10 minutes
}
}

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« August »
SunMonTueWedThuFriSat
      1
2345678
9101112131415
16171819202122
23242526272829
3031