Spring的定时器功能

pengGeiii 2013-04-04

spring支持jdk内置的Timer类和Quartz Scheduler,两种Spring定时器的实现方式各有优点,可结合具体项目考虑是否采用。

1.Java Timer定时

首先继承java.util.TimerTask类实现run方法

import java.util.TimerTask;  
public class EmailReportTask extends TimerTask{  
    @Override  
    public void run() {  
        ...  
    }    
}  
在Spring定义

...

配置Spring定时器

<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">  
<property name="timerTask" ref="reportTimerTask" />  
<property name="period">  
<value>86400000value>  
property>  
bean>  
timerTask属性告诉ScheduledTimerTask运行哪个。86400000代表24个小时

启动Spring定时器

Spring的TimerFactoryBean负责启动定时任务

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">  
<property name="scheduledTimerTasks">  
   <list><ref bean="scheduleReportTask"/>list>  
property>  
bean>  
scheduledTimerTasks里显示一个需要启动的定时器任务的列表。  
可以通过设置delay属性延迟启动  
<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">  
<property name="timerTask" ref="reportTimerTask" />  
<property name="period">  
<value>86400000value>  
property>  
<property name="delay">  
<value>3600000value>  
property>  
bean>  
这个任务我们只能规定每隔24小时运行一次,无法精确到某时启动

2.Quartz定时器


记得需继承QuartzJobBean类实现executeInternal方法

spring的定时器功能,它不仅实现起来方便,功能强大,而且在web开发时正好配合spring框架使用。

   

介绍spring的定时器,当然要先介绍配置文件applicationContext.xml了。

<bean name="job" class="org.springframework.scheduling.quartz.JobDetailBean">

     <property name="jobClass">

         <value>jaoso.news.web.action.JobAction</value>

     </property>

     <property name="jobDataAsMap">

         <map>

             <entry key="timeout">

                 <value>10</value>

              </entry>

         </map>

     </property>

</bean>

说明:org.springframework.scheduling.quartz.JobDetailBean是spring对你的类进行调度的代理,在jobClass中要指定你的任务类(com.yangsq.web.action.JobAction),在jobDataAsMap中向你的任务类中注入一些信息,当然也可以reference一个,不要忘记在你的任务里加入这些属性及set方法。

timeout属性设定了当服务器启动后过10秒钟首次调用你的JobAction。

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

     <property name="jobDetail">

         <ref bean="job"/>

     </property>

     <property name="cronExpression">

         <value>0 0/2 * * * ?</value>

     </property>

</bean>

说明:org.springframework.scheduling.quartz.CronTriggerBean是spring提供的触发器,在这个触发器中设定了要触发的job(jobDetail属性设定了先前定义的bean),同时设定了触发时间(cronExpression)---每隔两分钟(0 0/2 * * * ?),这个的设定方式最后会说明。

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

     <property name="triggers">

         <list>

             <ref local="cronTrigger"/>

         </list>

     </property>

</bean>

说明:org.springframework.scheduling.quartz.SchedulerFactoryBean这是一个spring的工厂bean,在他的triggers属性列表中加入刚才定义的触发器,这里可以定义多个触发器(list嘛)。

好了,配置文件就介绍完了,该介绍com.yangsq.web.action.JobAction类了,

引入包:

Spring的定时器功能

说明:QuartzJobBean是spring自带的,把spring的jar包加入就行了,但是前两个包要去下了,呵呵,google吧。

Spring的定时器功能

Spring的定时器功能

当然要继承QuartzJobBean了,但是光extends不行,必须要重载他的executeInternal方法

Spring的定时器功能

好了,一个spring的时间调度完成了。

附:时间配置说明

sping定时器的时间配置十分强大,下面将介绍如何进行配置。

Spring的定时器功能

Spring的定时器功能

相关推荐

kyle00 / 0评论 2020-05-07