论坛首页 Java企业应用论坛

spring任务调度方法之JDK Timer

浏览 6643 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-10-26   最后修改:2010-10-26
一、基本概念
任务(job):需要被调度以指定的时间间隔运行的任务单元。
触发器(trigger):引发任务运行的条件。
调度计划(schedule):一个任务和用来管理这个任务的一组触发器的集合。
通常一个任务在他的调度计划中只有一个触发器,所以触发器和调度计划这两个概念通常可以互换。

二、spring的两种任务调度方法
1. JDK Timer JVM1.3版本后提供的任务调度能力,不需要依赖spring以外的产品。
优点:被包含在java中,不需要外部依赖库,当受限于程序大小或公司政策时可以使用。
不足:在定义一个调度计划时只能够提供有限的灵活性。
2. Quartz 提供很大的灵活性,允许定义类似于真实世界中的触发器。

三、JDK Timer
1)Timer触发器类型
一次性触发器(One-off)
重复固定延迟触发器(Fixed-Delay)
重复定时触发器(Fixed-Rated)
说明:
一次性触发器只能执行一次,执行完成后,不能被再度重新使用;
下面举例说明固定延迟触发器和定时触发器区别:
假如17:00开始任务执行,任务执行时间为30分钟,每小时执行一次,第一次运行将于17:30结束。如果采用固定延迟触发器,第二次运行将在18:30开始,计算方法为前一次结束时间加上间隔时间;如果采用定时触发器,第二次运行将在18:00开始,计算方法为前一次开始时间加上间隔时间。

2)JDK Timer的使用范例

package com.learnworld.timer;

import java.util.TimerTask;

public class HelloTask extends TimerTask {

	@Override
	public void run() {
		System.out.println("Hello!");
	}

}
package com.learnworld.timer;

import java.util.Calendar;
import java.util.Timer;

public class SchedulingTest {
	public static void main(String args[]){
	Timer t = new Timer();
	
	//一次性触发器,1000毫秒后开始运行
	t.schedule(new HelloTask(), 1000);
	
	//固定时间调用
	Calendar cal = Calendar.getInstance();
	cal.set(2010,Calendar.OCTOBER, 27);
	t.schedule(new HelloTask(), cal.getTime());

	//调用重复任务,固定延迟触发器
	t.schedule(new HelloTask(),1000,3000);
	
	//定时触发器
	t.scheduleAtFixedRate(new HelloTask(), 1000, 1000);

	}
}

3) Spring对JDK Timer调度的支持
目的:许多任务需要配置数据,采用spring管理,使得程序易于配置。
触发器信息是硬编码到程序里的,意味着对任务触发时间的修改都要修改程序并重新编译。
调度新任务或移除任务都需要修改程序代码,这可以通过spring来配置。

核心类:
ScheduleTimerTask: 扮演TimerTask的包装器实现,允许你为这个任务定义触发器信息。
TimerFactoryBean:让spring使用配置创建触发器,为一组指定的scheduledTimerTask bean自动创建Timer实例。

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="job" class="com.learnworld.timer.HelloTask" />
	<bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
		<property name="delay">
			<value>1000</value>
		</property>
		<property name="period">
			<value>3000</value>
		</property>
		<property name="timerTask">
			<ref local="job" />
		</property>
	</bean>

	<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
		<property name="scheduledTimerTasks">
			<list>
				<ref local="timerTask" />
			</list>
		</property>
	</bean>
</beans>

package com.learnworld.timer;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TimerFactoryBeanTest {
	public static void main(String[] args) throws IOException{
		ApplicationContext ac = new FileSystemXmlApplicationContext("src/conf/timer.xml");
		System.in.read();
	}
}


4)当进行任务调度时,通常需要执行一段现有逻辑,我们不需要创建一个包装你逻辑的TimerTask类,使用MethodInvokingTimerTaskFactoryBean,可以调度bean里任意方法或者指定类的静态方法。

配置文件methodInvoker.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="target" class="com.learnworld.timer.FooBean" />
	<bean id="job"
		class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
		<property name="targetObject">
			<ref local="target" />
		</property>
		<property name="targetMethod">
			<value>someJob</value>
		</property>
		<property name="arguments">
			<value>Hello!</value>
		</property>
	</bean>
	<bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
		<property name="delay">
			<value>1000</value>
		</property>
		<property name="period">
			<value>3000</value>
		</property>
		<property name="timerTask">
			<ref local="job" />
		</property>
	</bean>

	<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
		<property name="scheduledTimerTasks">
			<list>
				<ref local="timerTask" />
			</list>
		</property>
	</bean>
</beans>


测试程序TimerMethodInvokerTest.java:

package com.learnworld.timer;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TimerMethodInvokerTest {
		public static void main(String[] args) throws IOException{
			ApplicationContext ac = new FileSystemXmlApplicationContext("src/conf/methodInvoker.xml");
			System.in.read();
		}
}
   发表时间:2010-10-28  
很基础的东西,不过还是顶了...
0 请登录后投票
   发表时间:2010-10-28  
基础的我也不会哦,没做过任务调度方面的东西。了解多了总没坏处。
0 请登录后投票
   发表时间:2010-10-28  
用ScheduledExecutorService,Timer已经没有使用价值了。
0 请登录后投票
   发表时间:2010-10-28  
whitesock 写道
用ScheduledExecutorService,Timer已经没有使用价值了。

spring3.0中开始使用ScheduledExecutorService,前面的接口已经被淘汰了,不过新手上路,先从基础东西开始。
0 请登录后投票
   发表时间:2010-10-29  
3.0已经大大简化了Schedule,xml已经不需要了,只需要 @Scheduled就可以实现基本操作了
0 请登录后投票
   发表时间:2010-10-29   最后修改:2010-11-04
定时框架用什么倒无所谓,重要的在于对密集任务的调度和监控
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics