日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

spring 通過配置文件與quarz實(shí)現(xiàn)定時(shí)任務(wù)

系統(tǒng) 2105 0

最近在研究Spring中的定時(shí)任務(wù)功能,最好的辦法當(dāng)然是使用Quartz來實(shí)現(xiàn)。對于一個(gè)新手來說,花了我不少時(shí)間,這里我寫個(gè)筆記,給大家參考。?
我使用的是Maven來管理項(xiàng)目,需要的Jar包我給大家貼出來。
?
quartz-1.8.5.jar?
commons-logging.jar?
spring-core-3.0.5.RELEASE.jar?
spring-beans-3.0.5.RELEASE.jar?
spring-context-3.0.5.RELEASE.jar?
spring-context-support-3.0.5.RELEASE.jar?
spring-asm-3.0.5.RELEASE.jar?
spring-expression-3.0.5.RELEASE.jar?
spring.transaction-3.0.5.RELEASE.jar?
spring-web-3.0.5.RELEASE.jar
?
Maven的pom.xml的配置:?

Xml代碼?? 收藏代碼
  1. <? xml ? version = "1.0" ? encoding = "UTF-8" ?> ??
  2. < project ? xmlns = "http://maven.apache.org/POM/4.0.0" ??
  3. ????????? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ??
  4. ????????? xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd" > ??
  5. ???? < modelVersion > 4.0.0 </ modelVersion > ??
  6. ??
  7. ???? < groupId > QtzTest </ groupId > ??
  8. ???? < artifactId > QtzTest </ artifactId > ??
  9. ???? < version > 1.0 </ version > ??
  10. ??
  11. ???? < properties > ??
  12. ???????? < springframework.version > 3.0.5.RELEASE </ springframework.version > ??
  13. ???? </ properties > ??
  14. ??
  15. ???? < dependencies > ??
  16. ???????? < dependency > ??
  17. ???????????? < groupId > org.springframework </ groupId > ??
  18. ???????????? < artifactId > spring-context </ artifactId > ??
  19. ???????????? < version > ${springframework.version} </ version > ??
  20. ???????? </ dependency > ??
  21. ??
  22. ???????? < dependency > ??
  23. ???????????? < groupId > org.springframework </ groupId > ??
  24. ???????????? < artifactId > spring-context-support </ artifactId > ??
  25. ???????????? < version > ${springframework.version} </ version > ??
  26. ???????? </ dependency > ??
  27. ??
  28. ???????? < dependency > ??
  29. ???????????? < groupId > org.springframework </ groupId > ??
  30. ???????????? < artifactId > spring-tx </ artifactId > ??
  31. ???????????? < version > ${springframework.version} </ version > ??
  32. ???????? </ dependency > ??
  33. ??
  34. ???????? < dependency > ??
  35. ???????????? < groupId > org.springframework </ groupId > ??
  36. ???????????? < artifactId > spring-web </ artifactId > ??
  37. ???????????? < version > ${springframework.version} </ version > ??
  38. ???????? </ dependency > ??
  39. ??
  40. ???????? < dependency > ??
  41. ???????????? < groupId > org.quartz-scheduler </ groupId > ??
  42. ???????????? < artifactId > quartz </ artifactId > ??
  43. ???????????? < version > 1.8.5 </ version > ??
  44. ???????? </ dependency > ??
  45. ???? </ dependencies > ??
  46. ??
  47. ???? < build > ??
  48. ???????? < finalName > ${project.artifactId} </ finalName > ??
  49. ???????? < plugins > ??
  50. ???????????? < plugin > ??
  51. ???????????????? < groupId > org.mortbay.jetty </ groupId > ??
  52. ???????????????? < artifactId > jetty-maven-plugin </ artifactId > ??
  53. ???????????????? < version > 7.5.4.v20111024 </ version > ??
  54. ???????????????? < configuration > ??
  55. ???????????????????? < scanIntervalSeconds > 10 </ scanIntervalSeconds > ??
  56. ???????????????????? < webApp > ??
  57. ???????????????????????? < contextPath > /${project.artifactId} </ contextPath > ??
  58. ???????????????????? </ webApp > ??
  59. ???????????????? </ configuration > ??
  60. ???????????? </ plugin > ??
  61. ???????? </ plugins > ??
  62. ???? </ build > ??
  63. </ project > ??


特別注意一點(diǎn),與Spring3.1以下版本整合必須使用Quartz1,最初我拿2.1.3的,怎么搞都報(bào)錯(cuò): ?
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.scheduling.quartz.CronTriggerBean] for bean with name 'mytrigger' defined in class path resource [applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class?

查看發(fā)現(xiàn)spring3.0.5中org.springframework.scheduling.quartz.CronTriggerBean繼承了org.quartz.CronTrigger(public class CronTriggerBeanextends CronTrigger),而在quartz2.1.3中org.quartz.CronTrigger是個(gè)接口(publicabstract interface CronTrigger extends Trigger),而在quartz1.8.5及1.8.4中org.quartz.CronTrigger是個(gè)類(publicclass CronTrigger extends Trigger),從而造成無法在applicationContext中配置觸發(fā)器。這是spring3.1以下版本和quartz2版本不兼容的一個(gè)bug。(感謝tiren的回復(fù),spring3.1以及以后版本支持quartz2) ?

在Spring中使用Quartz有兩種方式實(shí)現(xiàn):第一種是任務(wù)類繼承QuartzJobBean,

<bean name=" SpringQtzJob " class="org.springframework.scheduling.quartz.JobDetailFactoryBean">

? ?<!-- 表示任務(wù)完成之后是否依然保留到數(shù)據(jù)庫,默認(rèn)false -->

? ?<property name="durability" value="true" />

? ?<property name="jobClass" value=" com.ncs.hj.SpringQtz " />

?

</bean>

?

第二種則是在配置文件里定義任務(wù)類和要執(zhí)行的方法,類和方法仍然是普通類,方法必須無參數(shù)和返回值。很顯然,第二種方式遠(yuǎn)比第一種方式來的靈活。 ?

第一種方式的JAVA代碼:?

Java代碼?? 收藏代碼
  1. package ?com.ncs.hj;??
  2. ??
  3. import ?org.quartz.JobExecutionContext;??
  4. import ?org.quartz.JobExecutionException;??
  5. import ?org.springframework.scheduling.quartz.QuartzJobBean;??
  6. ??
  7. public ? class ?SpringQtz? extends ?QuartzJobBean{??
  8. ???? private ? static ? int ?counter?=? 0 ;??
  9. ???? protected ? void ?executeInternal(JobExecutionContext?context)? throws ?JobExecutionException?{??
  10. ????????System.out.println();??
  11. ???????? long ?ms?=?System.currentTimeMillis();??
  12. ????????System.out.println( "\t\t" ?+? new ?Date(ms));??
  13. ????????System.out.println(ms);??
  14. ????????System.out.println( "(" ?+?counter++?+? ")" );??
  15. ????????String?s?=?(String)?context.getMergedJobDataMap().get( "service" );??
  16. ????????System.out.println(s);??
  17. ????????System.out.println();??
  18. ????}??
  19. }??


第二種方式的JAVA代碼:?

Java代碼?? 收藏代碼
  1. package ?com.ncs.hj;??
  2. ??
  3. import ?org.quartz.JobExecutionContext;??
  4. import ?org.quartz.JobExecutionException;??
  5. import ?org.springframework.scheduling.quartz.QuartzJobBean;??
  6. ??
  7. import ?java.util.Date;??
  8. ??
  9. public ? class ?SpringQtz?{??
  10. ???? private ? static ? int ?counter?=? 0 ;??
  11. ???? protected ? void ?execute()??{??
  12. ???????? long ?ms?=?System.currentTimeMillis();??
  13. ????????System.out.println( "\t\t" ?+? new ?Date(ms));??
  14. ????????System.out.println( "(" ?+?counter++?+? ")" );??
  15. ????}??
  16. }??


Spring的配置文件:?

Xml代碼?? 收藏代碼
  1. <!------------?配置調(diào)度程序quartz?,其中配置JobDetail有兩種方式--------------> ????
  2. ???? <!--方式一:使用JobDetailBean,任務(wù)類必須實(shí)現(xiàn)Job接口?--> ?????
  3. ???? < bean ? id = "myjob" ? class = "org.springframework.scheduling.quartz.JobDetailBean" > ????
  4. ????? < property ? name = "name" ? value = "exampleJob" > </ property > ????
  5. ????? < property ? name = "jobClass" ? value = "com.ncs.hj.SpringQtz" > </ property > ???
  6. ????? < property ? name = "jobDataAsMap" > ??
  7. < map > ??
  8. ???? < entry ? key = "service" > < value > simple?is?the?beat </ value > </ entry > ??
  9. </ map > ??
  10. ? ?</property>
  11. ???? </ bean > ???
  12. ???? <!--運(yùn)行時(shí)請將方式一注釋掉!?--> ????
  13. ???? <!--?方式二:使用MethodInvokingJobDetailFactoryBean,任務(wù)類可以不實(shí)現(xiàn)Job接口,通過targetMethod指定調(diào)用方法--> ????
  14. ???? <!--?定義目標(biāo)bean和bean中的方法?--> ??
  15. ???? < bean ? id = "SpringQtzJob" ? class = "com.ncs.hj.SpringQtz" /> ??
  16. ???? < bean ? id = "SpringQtzJobMethod" ? class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > ??
  17. ???? < property ? name = "targetObject" > ??
  18. ???????? < ref ? bean = "SpringQtzJob" /> ??
  19. ???? </ property > ??
  20. ???? < property ? name = "targetMethod" > ?? <!--?要執(zhí)行的方法名稱?--> ??
  21. ???????? < value > execute </ value > ??
  22. ???? </ property > ??
  23. </ bean > ??
  24. ??
  25. <!--?========================?調(diào)度觸發(fā)器?========================?--> ??
  26. < bean ? id = "CronTriggerBean" ? class = "org.springframework.scheduling.quartz.CronTriggerBean" > ??
  27. ???? < property ? name = "jobDetail" ? ref = "SpringQtzJobMethod" > </ property > ??
  28. ???? < property ? name = "cronExpression" ? value = "0/5?*?*?*?*??" > </ property > ??
  29. </ bean > ??
  30. ??
  31. <!--?========================?調(diào)度工廠?========================?--> ??
  32. < bean ? id = "SpringJobSchedulerFactoryBean" ? class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" > ??
  33. ???? < property ? name = "triggers" > ??
  34. ???????? < list > ??
  35. ???????????? < ref ? bean = "CronTriggerBean" /> ??
  36. ???????? </ list > ??
  37. ???? </ property > ??
  38. </ bean > ????


關(guān)于cronExpression表達(dá)式,這里講解一下:?
字段 允許值 允許的特殊字符?
秒 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 *
?
最后別忘了在web.xml里面配置Spring:?

Xml代碼?? 收藏代碼
  1. <? xml ? version = "1.0" ? encoding = "UTF-8" ?> ??
  2. < web-app ? xmlns = "http://java.sun.com/xml/ns/javaee" ??
  3. ????????? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ??
  4. ????????? xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee??
  5. ??????????http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"??
  6. ????????? version = "2.5" > ??
  7. ???? < welcome-file-list > ??
  8. ???????? < welcome-file > index.html </ welcome-file > ??
  9. ???? </ welcome-file-list > ??
  10. ??
  11. ???? < context-param > ??
  12. ???????? < param-name > contextConfigLocation </ param-name > ??
  13. ???????? < param-value > /WEB-INF/spring-config.xml </ param-value > ??
  14. ???? </ context-param > ??
  15. ??
  16. ???? < listener > ??
  17. ???????? < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > ??
  18. ???? </ listener > ??
  19. </ web-app > ??


運(yùn)行結(jié)果:?
Wed Feb 08 13:58:30 CST 2012?
(0)?
Wed Feb 08 13:58:35 CST 2012?
(1)?
Wed Feb 08 13:58:40 CST 2012?
(2)?
Wed Feb 08 13:58:45 CST 2012?
(3)?
Wed Feb 08 13:58:50 CST 2012?
(4)?
Wed Feb 08 13:58:55 CST 2012?
(5)?
Wed Feb 08 13:59:00 CST 2012?
(6)?

spring 通過配置文件與quarz實(shí)現(xiàn)定時(shí)任務(wù)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 仁化县| 成武县| 九龙城区| 化隆| 涿州市| 平武县| 喜德县| 邯郸市| 吴忠市| 宜昌市| 交城县| 个旧市| 浮梁县| 潮州市| 海晏县| 和林格尔县| 惠水县| 高碑店市| 华容县| 班戈县| 双柏县| 贺兰县| 伊金霍洛旗| 丹阳市| 东明县| 新泰市| 铜鼓县| 分宜县| 来安县| 郎溪县| 东台市| 邯郸市| 嵩明县| 茶陵县| 安丘市| 洛川县| 沂水县| 承德市| 凯里市| 辽宁省| 日土县|