欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

spring 通過配置文件與quarz實現定時任務

系統 1951 0

最近在研究Spring中的定時任務功能,最好的辦法當然是使用Quartz來實現。對于一個新手來說,花了我不少時間,這里我寫個筆記,給大家參考。?
我使用的是Maven來管理項目,需要的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 > ??


特別注意一點,與Spring3.1以下版本整合必須使用Quartz1,最初我拿2.1.3的,怎么搞都報錯: ?
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?

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

在Spring中使用Quartz有兩種方式實現:第一種是任務類繼承QuartzJobBean,

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

? ?<!-- 表示任務完成之后是否依然保留到數據庫,默認false -->

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

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

?

</bean>

?

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

第一種方式的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. <!------------?配置調度程序quartz?,其中配置JobDetail有兩種方式--------------> ????
  2. ???? <!--方式一:使用JobDetailBean,任務類必須實現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. ???? <!--運行時請將方式一注釋掉!?--> ????
  13. ???? <!--?方式二:使用MethodInvokingJobDetailFactoryBean,任務類可以不實現Job接口,通過targetMethod指定調用方法--> ????
  14. ???? <!--?定義目標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" > ?? <!--?要執行的方法名稱?--> ??
  21. ???????? < value > execute </ value > ??
  22. ???? </ property > ??
  23. </ bean > ??
  24. ??
  25. <!--?========================?調度觸發器?========================?--> ??
  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. <!--?========================?調度工廠?========================?--> ??
  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 > ????


關于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點觸發?
"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點到下午2:59期間的每1分鐘觸發?
"0 0/5 14 * * ?" 在每天下午2點到下午2:55期間的每5分鐘觸發?
"0 0/5 14,18 * * ?" 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發?
"0 0-5 14 * * ?" 在每天下午2點到下午2:05期間的每1分鐘觸發?
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和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年至2005年的每月的最后一個星期五上午10:15觸發?
"0 15 10 ? * 6#3" 每月的第三個星期五上午10:15觸發?
每天早上6點?
0 6 * * *?
每兩個小時?
0 */2 * * *?
晚上11點到早上8點之間每兩個小時,早上八點?
0 23-7/2,8 * * *?
每個月的4號和每個禮拜的禮拜一到禮拜三的早上11點?
0 11 4 * 1-3?
1月1日早上4點?
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 > ??


運行結果:?
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實現定時任務


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久www免费人成精品 | 免费视频二区 | 欧美精品99毛片免费高清观看 | 天天天天天天操 | 亚洲天堂午夜 | 国产一区二区 | 伊人a.v在线 | 国产精品国产三级国产aⅴ中文 | 国产一级免费不卡 | 一级一片免费看 | 在线视频国产一区 | 欧美日韩视频在线 | 色偷偷亚洲男人 | 亚洲一区二区国产 | 涩涩小网站 | 欧美日韩欧美日韩 | 99激情视频| 成人毛片免费播放 | 中国一级黄色片 | 国产精品va在线观看无 | 毛片无码免费无码播放 | 5c5c5c精品视频在线观看 | 日韩操操操| 久久中文字幕一区 | 国产免费www| 羞羞的视频在线免费观看 | 免费成人直播 | 久久精品无码一区二区日韩av | 麻豆传媒视频入口 | 影音先锋中文字幕在线 | 亚洲激情一区 | 日韩精品免费 | 亚洲综合色视频在线观看 | 国产一级一级毛片 | 色婷婷综合久久久中文一区二区 | 欧美三级电影在线观看 | 亚洲第一在线 | 三级黄色毛片视频 | 亚洲精品久久久久久中文字幕小说 | 黄色综合 | 在线视频 中文字幕 |