Java Reference
In-Depth Information
Then you define this task in the bean configuration file referring to the file replicator
documentReplicator .
<bean id="documentReplicationTask"
<property name="fileReplicator" ref="documentReplicator" />
Using JDK Timer Without Spring's Support
After creating a timer task, you can schedule it by calling one of the overloaded schedule() methods of
Timer . For example, the following timer runs the file replication task every 60 seconds with a 5-second
delay for the first time of execution.
package com.apress.springenterpriserecipes.replicator;
import java.util.Timer;
import java.util.TimerTask;
import org.springframework.context.ApplicationContext;
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
TimerTask documentReplicationTask =
(TimerTask) context.getBean("documentReplicationTask");
Timer timer = new Timer();
timer.schedule(documentReplicationTask, 5000, 60000);
Using JDK Timer with Spring's Support
First, Spring offers MethodInvokingTimerTaskFactoryBean for you to define a timer task that executes a
single method of a particular object. This saves you the trouble of extending TimerTask . You can use the
following task definition to replace the previous:
<bean id="documentReplicationTask" class="org.springframework.scheduling.
<property name="targetObject" ref="documentReplicator" />
<property name="targetMethod" value="replicate" />
Next, Spring allows you to configure the scheduling details of your task in the bean configuration
file, through a ScheduledTimerTask instance that wraps a TimerTask instance.
Search WWH ::

Custom Search