This article address: http://blog.csdn.net/kongxx/article/details/6751151
Some of the integration features of task scheduling are provided in spring, the simplest of which is to implement simple task scheduling with the timer and TimerTask classes that the JDK comes with. Look at the following small example:
A simple task class that does not implement any interfaces that contain a run method to run this task
Package Org.garbagecan.springstudy.schedule.timer;
public class MyTask {
private String name;
public void Run () {
System.out.println ("Run Task:" + name + ".");
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
}
Create a spring configuration file, such as Spring.xml, that reads
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "default-lazy-init=" true "> <bean id=" timer Factory "class=" Org.springframework.scheduling.timer.TimerFactoryBean "lazy-init=" false "> <property name="
Scheduledtimertasks "> <list> <ref local=" ScheduledTask1 "/> <ref local=" ScheduledTask2 "/> </list> </property> </bean> <bean id= "ScheduledTask1" class= "Org.springframework.scheduling.ti Mer. Scheduledtimertask "> <property name=" delay "value=" 0 "/>" <property name= "period" value= "10000"/> ;p roperty name= "TimerTask" > <ref bean= "methodInvokingTask1"/> </property> </bean> <bean ID = "ScheduledTask2" class= "Org.spriNgframework.scheduling.timer.ScheduledTimerTask "> <property name= Delay" value= "0"/> <property name= " Period "value=" 10000 "/> <property name=" TimerTask "> <ref bean=" methodInvokingTask2 "/> </property > </bean> <bean id= "MethodInvokingTask1" class= " Org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean "> <property name=" TargetObject "ref = "MyTask1"/> <property name= "Targetmethod" value= "Run"/> </bean> <bean id= "MethodInvokingTask2" CLA ss= "Org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean" > <property name= "targetobject "ref=" MyTask2 "/> <property name=" Targetmethod "value=" Run "/> </bean> <bean id=" myTask1 "class=" org . garbagecan.springstudy.schedule.timer.MyTask "> <property name=" name "value=" Task1 "/> </bean> <be An id= "MyTask2" class= "Org.garbagecan.springstudy.schedule.timer.MyTask" > <property name= "name"Value=" Task2 "/> </bean> </beans>
1. The definition of two task,task1 and Task2;
2. Use the Methodinvokingtimertaskfactorybean class provided by spring to implement the declaration of the Task class and method, declare the target object and method, so that spring knows the method to run that class;
3. Use the Scheduledtimertask class to configure the start time delay for each task, the interval between each boot, and of course, the most important thing is to run the object, Examples of the Methodinvokingtimertaskfactorybean class mentioned above are used here;
4. Finally, a Timerfactorybean class is defined and an instance of the Scheduledtimertask class is used as a task requiring scheduling;
Finally, write a test class to test the code and configuration above
Package Org.garbagecan.springstudy.schedule.timer;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {public
static void Main (string[] args) throws Exception {
new Classpathxmlapplicationcontext ( "/org/garbagecan/springstudy/schedule/timer/spring.xml");
}
Running the test class, you can see that two tasks will start, and use the same 10 seconds as the interval between each run.