Spring任务调度实战之Timer

本文地址:http://blog.csdn.net/kongxx/article/details/6751151

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

一个简单的Task类,没有实现任何接口,其中包含一个run方法用来运行这个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;
	}
}

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

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


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

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秒作为每次运行之间的间隔。







  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值