`
Dxx23
  • 浏览: 140990 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring Quartz定时器详情

阅读更多
    在Spring中,使用JDK的Timer类库来做任务调度功能不是很方便,关键它不可以象cron服务那样可以指定具体年、月、日、时和分的时间。你只能将时间通过换算成微秒后传给它。如任务是每天执行一次,则需要在spring中如下配置:
­
<bean id="scheduledTask" class= "org.springframework.scheduling.timer.ScheduledTimerTask">
<!--程序启动后开始执行任务的延迟时间 -->
<property name="delay" value="0" />
<!--每隔一天【一天=24×60×60×1000微秒】执行一次-->
<property name="period" value="86400000" />
<!--业务统计报表bean -->
<property name="timerTask" ref="businessReport" />
</bean>

­
其中period就是一天的微秒数。如果每月1日运行一次,那就复杂了,不知如何配置。因为月份有大、小月之分,每月的微秒数都不一样。
­
而Quartz类库不但有着上述JDK的Timer类库类似的配置,更重要的,它还有着类似于unix的cron服务的配置。因此,在迁移中我们采用了Quartz类库的接口。
­
Quartz可以通过两种方式来调度程序:一是使用Spring提供的 MethodInvokingJobDetailFactoryBean 代理类,Quartz通过该代理类直接调度任务类的某个函数;二是任务类继承并实现Quartz接口,Quartz通过该接口进行调度。
­
如果采用第一种方式,即由Quartz直接调度任务类的某个接口,那么,业务类是不必进行任何修改的。我们的业务类大概如下
public class BusinessReport {
    public void perform(){ //执行报表统计入口函数
        //业务逻辑
    }
}

第一步,在Spring配置文件中增加本业务类
­
<bean id=" businessReport " class=" BusinessReport "/>

­
第二步,定义任务。在Spring配置文件中配置代理类MethodInvokingJobDetailFactoryBean,定义任务的详细信息。
­
<bean id=" reportTask " class= "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name=" targetObject " ref=" businessReport " />
<property name=" targetMethod " value=" perform " />
<property name=" concurrent " value=" false " />
</bean>

­
这个配置告诉Spring,我们的任务是执行id为businessReport的bean中的perform函数。其中参数concurrent告诉Spring,不要并发运行这个任务。
­
第三步,配置一个触发器。在Spring配置文件中配置触发器类CronTriggerBean 。
­
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref=" reportTask " />
    <property name="cronExpression" value="0 0 1 1 * ?" />
</bean>

­
触发器将告诉Quartz两件事:在何时触发任务、触发哪个任务。其中属性参数cronExpression为 调度时间,格式和unix上的crontab类似,具体见下表1。“0 0 1 1 * ?”表示每月1日凌晨1点运行。其中问号表示忽略该位置(星期)上的值。属性参数jobDetail指向具体的任务bean:reportTask 。如果你有多个任务,每个任务的触发时间都不一样,则你可以在此配置多个不同的触发器。
­
表1. cronExpression的时间格式
位置 - 含义­
1 - 秒(0–59)
­2 - 分(0–59)
3 - 时(0–23)
­4 - 日(1–31)
­5 - 月(1–12)
­6 - 星期(SUN–SAT or 1–7)
­7 - 年(可选, 1970–2099)
第四步,配置一个调度器。在Spring配置文件中配置调度器类SchedulerFactoryBean。
<bean class= "org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
      <list>
            <ref bean="cronTrigger" />
       </list>
    </property>
</bean>

­
该调度器用于管理触发器。只有在调度器中列表出现的触发器才被Quartz系统调度执行。至此,所有的配置已完成,任务已能正常跑了。
­
如果采用第二种方式,那业务类是要进行小小的修改。整个过程如下。
­
第一步,修改上述的业务类,修改如下:
public class BusinessReport implements org.quartz.Job { //继承quartz 的job接口
    //实现job接口的execute函数,在其中简单调用perform()函数就可以了。
    public void execute(org.quartz.JobExecutionContext context){ //执行报表统计入口函数
       perform()
    }
    //其它的保持不变。
    public void perform(){ //执行报表统计入口函数
        //业务逻辑
    }
}

修改过程比较简单,只增加了两三行代码。
­
第二步,定义任务。在Spring配置文件中配置如下任务的详细信息。
­
<bean name=" reportTask " class= "org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value=" BusinessReport " />
</bean>

­
此配置告诉Quartz,我们的任务类的名字为BusinessReport。在定时触发时,Quartz会利用该类名来创建任务的实例,并执行该实例的execute方法。
­
第三、第四步与第一种的调度方式相应的步骤一样。
­
按第二种方式,整个Spring的配置文件如下:
­
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
­
<!-- 我们的任务 -->
<bean name=" reportTask " class= "org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value=" BusinessReport " />
</bean>
­
<!-- 触发器 -->
<bean id="cronTrigger" class= "org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 指向我们的任务 -->
<property name="jobDetail" ref=" reportTask " />
<!-- 每月1日凌晨1点运行 -->
<property name="cronExpression" value="0 0 1 1 * ?" />
</bean>
­
<!-- 调度器 -->
<bean class= "org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list><!-- 触发器列表-->
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>

附:cronExpression表达式解释:

字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /


0 0 12 * * ?---------------在每天中午12:00触发
0 15 10 ? * *---------------每天上午10:15 触发
0 15 10 * * ?---------------每天上午10:15 触发
0 15 10 * * ? *---------------每天上午10:15 触发
0 15 10 * * ? 2005---------------在2005年中的每天上午10:15 触发
0 * 14 * * ?---------------每天在下午2:00至2:59之间每分钟触发一次
0 0/5 14 * * ?---------------每天在下午2:00至2:59之间每5分钟触发一次
0 0/5 14,18 * * ?---------------每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
0 0-5 14 * * ?---------------每天在下午2:00至2:05之间每分钟触发一次
0 10,44 14 ? 3 WED---------------每三月份的星期三在下午2:00和2:44时触发
0 15 10 ? * MON-FRI---------------从星期一至星期五的每天上午10:15触发
0 15 10 15 * ?---------------在每个月的每15天的上午10:15触发
0 15 10 L * ?---------------在每个月的最后一天的上午10:15触发
0 15 10 ? * 6L---------------在每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6L 2002-2005--------在2002, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6#3---------------在每个月的第三个星期五的上午10:15触发
0 0 12 1/5 * ?---------------从每月的第一天起每过5天的中午12:00时触发
0 11 11 11 11 ?---------------在每个11月11日的上午11:11时触发.­

程序逻辑:web.xml添加
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


applicationContext.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.0.xsd">

<bean id="remindQuartz" class="test.quartz.RemindQuartz"/>

<bean id="remindQuartzTask" class= "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="remindQuartz" />
<property name="targetMethod" value="perform" />
<property name="concurrent" value="false" />
</bean>
<!-- 调度器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="remindQuartzTask" />
<property name="cronExpression" value="0 0 1 1 * ?" />
</bean>

</beans>




附:实际代码*
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
">
<!-- quartz scheduler实例在spring中的初始化和自动运行 -->

<bean id="timerJob" class="com.eduwo.application.web.evaluationSheets.TimerJob">
<property name="evaluationSheetService">
<ref bean="evaluationSheetService"/>
</property> 
</bean>
<bean id="methodInvokingJobDetail" 
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"><ref bean="timerJob"/></property>
<property name="targetMethod"><value>doJob</value></property>
<property name="concurrent" value="false"/>
</bean>

<bean   id="simpleTrigger"   class="org.springframework.scheduling.quartz.CronTriggerBean">   
<property   name="jobDetail">   
<ref   bean="methodInvokingJobDetail"/>   
</property>    
<property name="cronExpression" value="0 0/60 * ? * * *"  />  
</bean>

<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<property name="configLocation" value="classpath:quartz.properties" />
<property name="triggers">
<list>
<ref local="simpleTrigger"/>
</list>
</property>
</bean>
</beans>

JAVA类:
package com.eduwo.application.web.evaluationSheets;

import java.util.Date;

import org.apache.log4j.Logger;

import com.eduwo.application.service.evaluationSheets.EvaluationSheetService;


public class TimerJob {
Logger logger=Logger.getLogger(TimerJob.class);

private EvaluationSheetService evaluationSheetService;
public EvaluationSheetService getEvaluationSheetService() {
return evaluationSheetService;
}
public void setEvaluationSheetService(
EvaluationSheetService evaluationSheetService) {
this.evaluationSheetService = evaluationSheetService;
}
public void doJob(){
Date d =new Date();
logger.info("Quartz begin ------------------------------"+d.toLocaleString()+"-----"+d.getHours());
//评估单过期处理
evaluationSheetService.updateExpiredEvaluationSheet();
logger.info("Quartz end ------------------------------");

} 
}

public void updateExpiredEvaluationSheet() {
// TODO Auto-generated method stub
Date date=new Date(System.currentTimeMillis()-2*24*60*60*1000);
SimpleDateFormat formattxt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timejob=formattxt.format(date);

String hql="select count(*) from ComTevaluationsheet sheet where sheet.updatedate is not null"
+" and sheet.expired='0' and sheet.status in ('1','2') and sheet.updatedate<=to_date('" + timejob + "'," + "'"
+ "yyyy-mm-dd hh24:mi:ss')";

List list=new ArrayList();
list=this.get(hql);
int count=0;
if(list!=null&&list.size()>0) count=(Integer)list.get(0);
if(count>0){
System.out.println("start---");
hql="select sheet from ComTevaluationsheet sheet where sheet.updatedate is not null"
+" and sheet.expired='0' and sheet.updatedate<=to_date('" + timejob + "'," + "'"
+ "yyyy-mm-dd hh24:mi:ss')";
list = this.get(hql);
for (int i = 0; i < list.size(); i++) {
ComTevaluationsheet comTevaluationsheet=(ComTevaluationsheet) list.get(i);
comTevaluationsheet.setExpired("1");
comTevaluationsheet.setStatus("3");
//this.updateComTevaluationsheet(comTevaluationsheet);
}
}
}

参考资料:
1.http://www.springframework.orgSpring的官方网站
2.http://www.opensymphony.com/quartz/Quartz的官方网站
摘自:
http://hi.baidu.com/z57354658/blog/item/c808523499a1e2bdd0a2d31f.html


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics