您的位置:首页 > 编程语言 > Java开发

Spring任务调度实战之Timer

2015-08-27 13:54 513 查看
本文地址:/article/1416074.html

在spring中提供了一些关于任务调度的集成功能,最简单的就是利用JDK自带的Timer和TimerTask类来实现简单任务调度。看下面的小例子:

一个简单的Task类,没有实现任何接口,其中包含一个run方法用来运行这个task

[java] view
plaincopyprint?

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;

}

}

创建一个spring的配置文件,比如spring.xml,内容如下

[html] view
plaincopyprint?

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="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="timerFactory" 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.timer.ScheduledTimerTask">

<property name="delay" value="0" />

<property name="period" value="10000" />

<property 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" class="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>



<bean id="myTask2" class="org.garbagecan.springstudy.schedule.timer.MyTask">

<property name="name" value="task2"/>

</bean>

</beans>

1. 定义了两个task,task1和task2;

2. 利用spring提供的MethodInvokingTimerTaskFactoryBean类来实现来实现对对task类和方法的声明,声明目标对象和方法,从而使spring知道要运行那个类的那个方法;

3. 利用ScheduledTimerTask类来配置每个task的启动时间延时,每次启动之间的间隔,当然还有最重要的是需要运行那个对象,这里使用的上面提到的MethodInvokingTimerTaskFactoryBean类的实例;

4. 最后定义了一个TimerFactoryBean类,并且把ScheduledTimerTask类的实例作为需要调度的task;

最后,写一个测试类来测试上面的代码和配置

[java] view
plaincopyprint?

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");

}

}

运行Test类,可以看到两个task都会启动,并且使用同样的10秒作为每次运行之间的间隔。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: