---
title: "Spring中时间任务调度Quarta  "
author: "liuye"
date: "2008-07-21T17:43:03Z"
tags: []
slug: "spring-quarta"
draft: false
lastmod: "2008-07-21T17:43:03Z"
id: 217180
url: "https://liuye.iteye.com/blog/217180"
---

今天五四青年节，闲着无聊，分享一下Spring中的Quartz时间任务调度机制。 ![](http://www.javae3.com//images/smilies/97ada74b88049a6d50a6ed40898a03d7.gif)  
当然使用Quartz的前提是在Spring框架中的。  
第一，向工程中添加quartz-all-1.6.0.jar，在spring-framework-2.\*中的lib\\quarta\\目录下就有，可能版本有点差异，但并影响Quartz的使用  
第二，新建你的 EmailReportJob.java 文件，代码如下：  
package com.inqgen.hxy.common;  
import org.quartz.JobExecutionContext;  
import org.quartz.JobExecutionException;  
import org.springframework.scheduling.quartz.QuartzJobBean;  
public class EmailReportJob extends QuartzJobBean {

private static int i=1;

@Override  
protected void executeInternal(JobExecutionContext context)  
throws JobExecutionException {  
System.out.println(new java.util.Date());  
System.out.println((i++)+imageDatabase);  
}  
private String imageDatabase;

public void setImageDatabase(String imageDatabase){  
this.imageDatabase = imageDatabase;  
}  
}  
这个类EmailReportJob需得继承org.springframework.scheduling.quartz.QuartzJobBean，并实现其executeInternal方法，若你有Servic层的话，也可以将String imageDatabase换成你所需要的Service，其注入将会在applicationContext-config.xml中配置～  
第三，配置applicationContext-config.xml  
1.自己所写的EmailReportJob.java类，是注入到org.springframework.scheduling.quartz.JobDetailBean类中的jobClass属性中，map的配置方式及上步骤中所提到的(若有Servic层的话)Service层的注入方式，  
但写法可能就为&lt;entry key="imageDatabase" ref="Servic" /&gt;而这个Servic必须要受Spring容器管理；若有多个Map的话，要在com.inqgen.hxy.common.EmailReportJob中写入其的setter方法  
&lt;bean id="reportJob" class="org.springframework.scheduling.quartz.JobDetailBean"&gt;  
&lt;property name="jobClass" value="com.inqgen.hxy.common.EmailReportJob" /&gt;  
&lt;property name="jobDataAsMap"&gt;  
&lt;map&gt;  
&lt;entry key="imageDatabase" value="imageDatabase" /&gt;  
&lt;/map&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
所配置的JobDetailBean，本人管它叫【工作任务】。  
2.配置TriggerBean在这里我们叫他【时间触发器】  
&lt;bean id="simpleReportTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"&gt;  
&lt;property name="jobDetail"&gt;  
&lt;ref bean="reportJob" /&gt;  
&lt;/property&gt;  
&lt;property name="startDelay"&gt;  
&lt;value&gt;1000&lt;/value&gt;  
&lt;/property&gt;  
&lt;property name="repeatInterval"&gt;  
&lt;value&gt;2000&lt;/value&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
startDelay是触发前等待的时间(为毫秒ms),repeatInterval是触发的间隔时间jobDetail，是将我们配置的【工作任务】添加到【时间触发器】中，上述的写法的意思是，每隔2秒触发一次,再延缓1秒才执行  
SimpleTriggerBean是一种简单的间隔触发执行代码的一个时间触发器，也可以写成CronTriggerBean，如下  
－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－  
&lt;bean id="checkImagesTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;  
&lt;property name="jobDetail" ref="reportJob" /&gt;  
&lt;property name="cronExpression" value="0/5 * * * * ?" /&gt;  
&lt;/bean&gt;  
jobDetail的配置和上面的一样，cronExpression就有所不同了，上述的写法的意思是，每隔5秒触发一次  
3.配置【调度工厂】  
&lt;bean id="scheduler"  
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;  
&lt;property name="triggers"&gt;  
&lt;list&gt;  
&lt;ref local="simpleReportTrigger" /&gt;  
&lt;ref local="checkImagesTrigger" /&gt;  
&lt;/list&gt;  
&lt;/property&gt;  
&lt;property name="quartzProperties"&gt;  
&lt;props&gt;  
&lt;prop key="org.quartz.threadPool.threadCount"&gt;5&lt;/prop&gt;  
&lt;/props&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
把2步骤中所配置的【时间触发器】注入到triggers中；  
quartzProperties是启动的线程数。  
OK!This end！～  
大功告成！  
－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－  
下面是介绍cronExpression的一些含义～  
Expression  
Meaning  
"0 0 12 * * ?"  
Fire at 12pm (noon) every day  
"0 15 10 ? * \*"  
Fire at 10:15am every day  
"0 15 10 * * ?"  
Fire at 10:15am every day  
"0 15 10 * * ? \*"  
Fire at 10:15am every day  
"0 15 10 * * ? 2005"  
Fire at 10:15am every day during the year 2005  
"0 * 14 * * ?"  
Fire every minute starting at 2pm and ending at 2:59pm, every day  
"0 0/5 14 * * ?"  
Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day  
"0 0/5 14,18 * * ?"  
Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day  
"0 0-5 14 * * ?"  
Fire every minute starting at 2pm and ending at 2:05pm, every day  
"0 10,44 14 ? 3 WED"  
Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.  
"0 15 10 ? * MON-FRI"  
Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday  
"0 15 10 15 * ?"  
Fire at 10:15am on the 15th day of every month  
"0 15 10 L * ?"  
Fire at 10:15am on the last day of every month  
"0 15 10 ? * 6L"  
Fire at 10:15am on the last Friday of every month  
"0 15 10 ? * 6L"  
Fire at 10:15am on the last Friday of every month  
"0 15 10 ? * 6L 2002-2005"  
Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005  
"0 15 10 ? * 6#3"  
Fire at 10:15am on the third Friday of every month