读书人

spring中施用的两种任务计划

发布时间: 2012-08-11 20:50:31 作者: rapoo

spring中使用的两种任务计划
spring中使用的两种任务计划
[1]
spring中使用的两种任务计划,分别是:
ScheduledTimerTask: 只能指定任务与任务之间的周期,而无法指定某个特定的时间内执行某项任务.
Quartz: 推荐使用Quartz来解决这样的问题。[2]
ScheduledTimerTask的用法:

分为继承与不继承java.util.TimerTask.
其过程如下:
1.写任务类(timerTask);
2.(非继承用timerTaskBean);
3.设置任务的schedule(scheduledTimerTask)
4.放置到任务工厂中(timerFactoryBean)(1)
继承TimerTask用法:

写任务类:
package com.laoer.bbscs.service.task;
import java.util.Date;
import java.util.TimerTask;
public class JasonTimerTask extends TimerTask{
public void run()
{
System.out.println("JasonTimerTask1 start.the now time is:"+new Date());
}
}设置任务的schedule且放入任务工厂:
<!--定义定时任务类-->
<bean id="jasonTimerTask" /><bean id="indexScheduledTimerTask" >
<!--这里定义每20秒钟程序执行一次-->
<property name="period">
<value>20000</value>
</property>
<!--这里定义程序启动5秒钟后开始执行-->
<property name="delay">
<value>5000</value>
</property>
<!--这里定义定时任务的对象的位置-->
<property name="timerTask">
<ref local="jasonTimerTask"/>
</property>
</bean><bean id="timerFactory" >
<property name="scheduledTimerTasks">
<list>
<ref local="indexScheduledTimerTask"/>
<ref local="jasonHelloScheduledTimerTask"/>
</list>
</property>
</bean>(2)
不继承TimerTask用法:

1.
写任务类:
package com.laoer.bbscs.service.task;
import java.util.Date;
public class JasonHelloTimerTask {
public void execute()
{
System.out.println("JasonTimerTask2 start. Welcome to here !");
}
}
不同的地方:
不用继承TimerTask,且方法可以自定义,不须用run();2.
设置timerTaskBean:
<bean id="timerTaskBean" >
<property name="scheduledTimerTasks">
<list>
<ref local="indexScheduledTimerTask"/>
<ref local="jasonHelloScheduledTimerTask"/>
</list>
</property>
</bean>[3]
Quartz的用法:

Spring 则对Quartz 进行了封装,在使用上更加方便。
也分为继承与不继承QuartzJobBean.
其过程如下:
1.写任务类(jobDetailBean);
2.(非继承用MethodInvokingJobDetailFactoryBean);
3.设置任务的schedule(cronTriggerBean)
4.放置到任务工厂中(schedulerFactoryBean)"cronExpression" 属性的分析:
指定的格式是至少六个时间元素,最多七个时间元素,例如上面的指定是每天的19 时要执行Job 一次,"cronExpression" 属性指定的格式如下:u 秒(0-59)u 分(0-59)u 小时(0-23)u 每月第几天(1-31)u 月(1-12 或JAN-DEC)u 每星期第几天(1-7 或SUN-SAT)u 年(1970-2099)其中“每月第几天”与“每星期第几天”是互斥的,两个只能设定一个,不设定的以“?”符号编写,如果有好几个时间点,可以使用“,”符号,例如:“0 0 10,12,14 * * ? ”表示每天的10 时、12 时、14 时要执行Job;对于连续的时间可以使用 -符号,例如“0 0 10,12,14 1-15 * ? ”表示每月的1 到15 日每10 时、12 时、14 时要执行Job,时间格式中的年指定可有可无,例如:“0 0 10,12,14 ? * MON 2006 ”表示2006 年每星期一的10 时、12 时、14 时要执行Job。(1)
继承Quartz:

任务类:
package com.laoer.bbscs.service.scheduling;
public class JasonGetPersonJob extends QuartzJobBean{
private PersonService personService;
public PersonService getPersonService() {
return personService;
}
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public void executeInternal(JobExecutionContext arg0)
{
PersonInfo personInfo=this.getPersonService().queryPersonInfoByName("jason");
System.out.println("jason 定时执行的job.获取personInfo表中jason用户的密码是: "+personInfo.getPassword());
}
}2.
定义任务类:
<bean id="jasonGetPersonJobDetailBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTriggerBean"/>
<ref bean="jasonGetPersonCronTriggerBean"/>
</list>
</property>
</bean>[4]
以上所有方法的部分输出如下:

JasonTimerTask1 start.the now time is:Thu Jun 04 15:01:18 CST 2009
JasonTimerTask2 start. Welcome to here !
jason 定时执行的job. 每分钟的第30秒执行一次. Test......... !
jason 定时执行的job.获取personInfo表中jason用户的密码是: 1234
JasonTimerTask2 start. Welcome to here !
JasonTimerTask1 start.the now time is:Thu Jun 04 15:01:38 CST 2009
JasonTimerTask2 start. Welcome to here !
JasonTimerTask2 start. Welcome to here !
JasonTimerTask1 start.the now time is:Thu Jun 04 15:01:58 CST 2009
JasonTimerTask2 start. Welcome to here !
JasonTimerTask2 start. Welcome to here !
JasonTimerTask1 start.the now time is:Thu Jun 04 15:02:18 CST 2009[5]使用总结:
(1)
ScheduledTimerTask与Quartz的创建过程是相同的.
1.创建任务类(分继承与不继承两种方式)
2.创建时间表类
3.放入任务工厂
(2)
ScheduledTimerTask与Quartz何时使用继承与不继承:
一般情况下,若一开始创建定时功能时,可以自由选择两种方式,都可;
若对原系统的方法添加定时功能,这时,用非继承的方法比较方便(只须指定系统类与其定时执行的方法).
(3)
如何整合ScheduledTimerTask与Quartz到系统中(即让系统现有功能添加定时执行功能):
1.整合ScheduledTimerTask与Quartz到系统中去,是采用非继承的方式.
2.只须指定要执行的系统类与其定时执行的方法即可.
具体请看: "ActiveMQ+JMS笔记"中的一实例.

读书人网 >开源软件

热点推荐