在Java的Spring框架中配置Quartz的教程
Spring中配置Quartz的過程:
1.導(dǎo)入JAR包
quartz需要的JAR包,已經(jīng)包含在spring中,位置在spring解壓后目錄的 \lib\quartz 下的quartz-all-1.6.1.jar,
將其拷貝到工程 的 WEB-INF/lib 下就行了。
2.配置web.xml,讓spring啟動(dòng)時(shí)加載quartz的配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-*.xml</param-value> </context-param> <!-- spring監(jiān)聽 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
3.編寫quartz的配置文件: application-quartz.xml 其實(shí)這些配置完全可以寫在其他的spring配置文件里
<?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:aop="http://www.springframework.org/schema/aop" 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.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 任務(wù)實(shí)體 --> <bean id="testTaskBean" class="com.jp.task.TestTask" /> <!-- 定時(shí)更新政策任務(wù)方法 --> <bean id="testTaskTimerMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="testTaskBean" /> <!-- targetMethod 配置定時(shí)執(zhí)行的方法名 --> <property name="targetMethod" value="executeAction" /> <property name="concurrent" value="false" /> </bean> <!-- 定時(shí)更新政策觸發(fā)器 --> <bean id="testTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="syncPolicyTaskTimerMethod" /> <!-- 每個(gè)3分鐘的第0秒執(zhí)行 --> <property name="cronExpression" value="0 0/3 * * * ?" /> </bean> <!-- 訂制任務(wù)列表 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="testTaskTrigger" /> </list> </property> </bean> </beans>
4.編寫執(zhí)行任務(wù)的JAVA類 TestTask
package com.jp.task; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class TestTask extends QuartzJobBean{ Logger log = Logger.getLogger( PolicyServiceTest.class ); public void executeAction(){ log.info("Hello quartz"); } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { } }
5.所有工作準(zhǔn)備完畢,一運(yùn)行?;旧暇蛨?bào)錯(cuò)了。大約是NoSuchMethod這樣的錯(cuò)誤。
這是由于commons-collections.jar版本太低引起的。要commons-collections-3.2.jar以上。
這時(shí)你找工程中卻是有個(gè)commons-collections-3.2.jar。 怎么回事呢,再找會(huì)發(fā)現(xiàn)在hibernate的里有個(gè)commons-collections-2.1.1.jar。
將commons-collections-2.1.1.jar刪掉就行了。
PS:cronExpression——Cron表達(dá)式說明
- 秒
- 分
- 小時(shí)
- 月內(nèi)日期
- 月
- 周內(nèi)日期
- 年(可選字段)
- 特殊字符
Cron 觸發(fā)器利用一系列特殊字符,如下所示:
反斜線(/)字符表示增量值。例如,在秒字段中“5/15”代表從第 5 秒開始,每 15 秒一次。
問號(hào)(?)字符和字母 L 字符只有在月內(nèi)日期和周內(nèi)日期字段中可用。問號(hào)表示這個(gè)字段不包含具體值。所以,如果指定月內(nèi)日期,可以在周內(nèi)日期字段中插入“?”,表示周內(nèi)日期值無關(guān)緊要。字母 L 字符是 last 的縮寫。放在月內(nèi)日期字段中,表示安排在當(dāng)月最后一天執(zhí)行。在周內(nèi)日期字段中,如果“L”單獨(dú)存在,就等于“7”,否則代表當(dāng)月內(nèi)周內(nèi)日期的最后一個(gè)實(shí)例。所以“0L”表示安排在當(dāng)月的最后一個(gè)星期日?qǐng)?zhí)行。
在月內(nèi)日期字段中的字母(W)字符把執(zhí)行安排在最靠近指定值的工作日。把“1W”放在月內(nèi)日期字段中,表示把執(zhí)行安排在當(dāng)月的第一個(gè)工作日內(nèi)。
井號(hào)(#)字符為給定月份指定具體的工作日實(shí)例。把“MON#2”放在周內(nèi)日期字段中,表示把任務(wù)安排在當(dāng)月的第二個(gè)星期一。
星號(hào)(*)字符是通配字符,表示該字段可以接受任何可能的值。
字段 允許值 允許的特殊字符 :
秒 0-59 , - * /
分 0-59 , - * /
小時(shí) 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可選) 留空, 1970-2099 , - * /
表達(dá)式意義
"0 0 12 * * ?" 每天中午12點(diǎn)觸發(fā)
"0 15 10 ? * *" 每天上午10:15觸發(fā)
"0 15 10 * * ?" 每天上午10:15觸發(fā)
"0 15 10 * * ? *" 每天上午10:15觸發(fā)
"0 15 10 * * ? 2005" 2005年的每天上午10:15觸發(fā)
"0 * 14 * * ?" 在每天下午2點(diǎn)到下午2:59期間的每1分鐘觸發(fā)
"0 0/5 14 * * ?" 在每天下午2點(diǎn)到下午2:55期間的每5分鐘觸發(fā)
"0 0/5 14,18 * * ?" 在每天下午2點(diǎn)到2:55期間和下午6點(diǎn)到6:55期間的每5分鐘觸發(fā)
"0 0-5 14 * * ?" 在每天下午2點(diǎn)到下午2:05期間的每1分鐘觸發(fā)
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44觸發(fā)
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15觸發(fā)
"0 15 10 15 * ?" 每月15日上午10:15觸發(fā)
"0 15 10 L * ?" 每月最后一日的上午10:15觸發(fā)
"0 15 10 ? * 6L" 每月的最后一個(gè)星期五上午10:15觸發(fā)
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一個(gè)星期五上午10:15觸發(fā)
"0 15 10 ? * 6#3" 每月的第三個(gè)星期五上午10:15觸發(fā)
每天早上6點(diǎn)
0 6 * * *
每兩個(gè)小時(shí)
0 */2 * * *
晚上11點(diǎn)到早上8點(diǎn)之間每兩個(gè)小時(shí),早上八點(diǎn)
0 23-7/2,8 * * *
每個(gè)月的4號(hào)和每個(gè)禮拜的禮拜一到禮拜三的早上11點(diǎn)
0 11 4 * 1-3
1月1日早上4點(diǎn)
0 4 1 1 *
相關(guān)文章
SpringBoot使用ip2region獲取地理位置信息的方法
這篇文章主要介紹了SpringBoot使用ip2region獲取地理位置信息的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Eclipse中查看android工程代碼出現(xiàn)"android.jar has no source attachment
這篇文章主要介紹了Eclipse中查看android工程代碼出現(xiàn)"android.jar has no source attachment"的解決方案,需要的朋友可以參考下2016-05-05關(guān)于SpringBoot攔截器中Bean無法注入的問題
這兩天遇到SpringBoot攔截器中Bean無法注入問題。下面介紹關(guān)于SpringBoot攔截器中Bean無法注入的問題,感興趣的朋友一起看看吧2021-10-10Java NIO Path接口和Files類配合操作文件的實(shí)例
下面小編就為大家分享一篇Java NIO Path接口和Files類配合操作文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11spring boot補(bǔ)習(xí)系列之幾種scope詳解
這篇文章主要給大家介紹了關(guān)于spring boot補(bǔ)習(xí)系列之幾種scope的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07一步步教會(huì)你使用Java原生指令編譯并運(yùn)行一個(gè)程序
Java是一種廣泛使用的編程語言,具有跨平臺(tái)性和面向?qū)ο蟮奶匦?下面這篇文章主要給大家介紹了關(guān)于使用Java原生指令編譯并運(yùn)行一個(gè)程序的相關(guān)資料,需要的朋友可以參考下2024-07-07Java8使用lambda實(shí)現(xiàn)Java的尾遞歸
這篇文章主要介紹了Java8使用lambda實(shí)現(xiàn)Java的尾遞歸的相關(guān)資料,需要的朋友可以參考下2017-10-10