首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring任务调度步骤之JDK Timer

2012-08-27 
spring任务调度方法之JDK Timer一、基本概念任务(job):需要被调度以指定的时间间隔运行的任务单元。触发器(t

spring任务调度方法之JDK Timer

一、基本概念
任务(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 {@Overridepublic 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" /><bean id="timerTask" /></property></bean><bean id="timerFactory" /></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" /><bean id="job"/></property><property name="targetMethod"><value>someJob</value></property><property name="arguments"><value>Hello!</value></property></bean><bean id="timerTask" /></property></bean><bean id="timerFactory" /></list></property></bean></beans>



测试程序TimerMethodInvokerTest.java:

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();??????????}??}?

热点排行