參考spring 12quartz任務(wù)調(diào)度_第1頁
參考spring 12quartz任務(wù)調(diào)度_第2頁
參考spring 12quartz任務(wù)調(diào)度_第3頁
參考spring 12quartz任務(wù)調(diào)度_第4頁
參考spring 12quartz任務(wù)調(diào)度_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Quartz有效管理批處理程序中各種批處理任務(wù),通常會(huì)選擇合適的任務(wù)調(diào)度程序(Job(比如:Quartz),調(diào)度程序產(chǎn)品(BMCCONTROL)JDK1.3引入的Timer也可以實(shí)現(xiàn)簡(jiǎn)單的任務(wù)調(diào)度。因?yàn)槭窃趙ebwebSpring監(jiān)聽器啟動(dòng)Spring容器。publicclasspublicclass{publicvoid 2Quartz有效管理批處理程序中各種批處理任務(wù),通常會(huì)選擇合適的任務(wù)調(diào)度程序(Job(比如:Quartz),調(diào)度程序產(chǎn)品(BMCCONTROL)JDK1.3引入的Timer也可以實(shí)現(xiàn)簡(jiǎn)單的任務(wù)調(diào)度。這里以在webJDKTimer和使用因?yàn)槭窃趙ebwebSpring監(jiān)聽器啟動(dòng)Spring容器。publicclassHelloService{publicvoidsayHello(){publicclassHelloService{publicvoidsayHello(){}}publicpublicclassHelloTaskextends{privateHelloServicepublicvoidsetHelloService(HelloService{this.helloService=publicvoidrun(){run()方法定義了當(dāng)任務(wù)運(yùn)行時(shí)該做什么。在上面的代碼中,它調(diào)用helloService的sayHello()方法來打印”你好,世界”。helloService是通過依賴注入方式提供給HelloTask的。SpringScheduledTimerTask 3使用JavaTimerpublicclassHelloTaskextendsTimerTask{privatepublicclassHelloTaskextendsTimerTask{privateHelloServicepublicvoidsetHelloService(HelloServicehelloService){this.helloService=helloService;}publicvoidrun(){}}run()方法定義了當(dāng)任務(wù)運(yùn)行時(shí)該做什么。在上面的代碼中,它調(diào)用helloService的sayHello()方法來打印”你好,世界”。helloService是通過依賴注入方式提供給HelloTask的。SpringScheduledTimerTask<bean<beanid="helloTask"<!--任務(wù)調(diào)度器period:執(zhí)行任務(wù)的頻率,每隔5<beanid="scheduledTimeTask"方式在Spring配置文件中聲明它<bean<property<refbean="scheduledTimeTask" 4<!--任務(wù)--><bean<!--任務(wù)--><beanid="helloTask"<propertyname="helloService"ref="helloService"<!--任務(wù)調(diào)度器period:執(zhí)行任務(wù)的頻率,每隔5秒執(zhí)行任務(wù)--><beanid="scheduledTimeTask"<propertyname="timerTask"ref="helloTask"<!--執(zhí)行的頻率--><propertyname="period"<propertyname="delay"<bean<bean<property<refbean="scheduledTimeTask"JavaTimerJavaTimerQuartzQuartz調(diào)度器為調(diào)度工作提供了更豐富的支持。和Java定時(shí)器一樣,可以使用Quartz一個(gè)工作在某個(gè)特定的時(shí)間或日期執(zhí)行。下面使用Quartz調(diào)度器實(shí)現(xiàn)上面的調(diào)度功能需要從Spring的QuartzJobBean中派生子類。為此我們需要在項(xiàng)目中加入SpringQuartz包.Commons-collections.jarSpringlib/jakarta-commonsQuartz-all-1.61-RC1.jar在Spring發(fā)行包/lib/quartz目錄中publicpublicclassHelloJobextends{privateHelloServicepublicvoidsetHelloService(HelloService{this.helloService=publicvoidexecuteInternal(JobExecutionContextarg0)throwsJobExecutionException{ 5JavaTimerQuartzQuartz調(diào)度器為調(diào)度工作提供了更豐富的支持。和Java定時(shí)器一樣,可以使用QuartzQuartzJavaTimer一個(gè)工作在某個(gè)特定的時(shí)間或日期執(zhí)行。下面使用Quartz調(diào)度器實(shí)現(xiàn)上面的調(diào)度功能QuartzQuartz需要從Spring的QuartzJobBean中派生子類。為此我們需要在項(xiàng)目中加入SpringQuartz包.Spring發(fā)布包中拷貝以下jarCommons-collections.jar在Springlib/jakarta-commons目錄中Spring-context-support.jarSpringdist/modulespublicclassHelloJobpublicclassHelloJobextendsQuartzJobBean{privateHelloServicehelloService;publicvoidsetHelloService(HelloServicehelloService){this.helloService=helloService;}publicvoidexecuteInternal(JobExecutionContextarg0)throwsJobExecutionException{}}<beanid="helloJob"<property在這里并沒有直接聲明一個(gè)HelloJobBean,而是聲明了一個(gè)JobDetailBean。這是使用Quartz時(shí)的一個(gè)特點(diǎn)。它要求通過jobClass屬性來設(shè)置一個(gè)Job對(duì)象。QuartzJobDetailHelloJobhelloService間接設(shè)置的。JobDetailBean的jobDataAsMap屬性接受一個(gè)java.util.Map,其中包含了需要設(shè)置給jobClass的各種屬性。在這里,這個(gè)map包含了一個(gè)指向helloServiceBean的引用,它的鍵值為helloService。當(dāng)JobDetailBean實(shí)例化時(shí),它會(huì)將helloServiceBean注入到HelloJob的helloService屬性中?,F(xiàn)在工作已經(jīng)被定義好了,接下來你需要調(diào)度這個(gè)工作。Quartz的org.quartz.TriggerQuartzSpring兩個(gè)觸發(fā)器,SimpleTriggerBeanCronTriggerBean。我們使用CronTriggerBean<propertyname="cronExpression"value="006**?" 6<beanid="helloJob"<propertyname="jobClass"value="com.wq.job.HelloJob"<property<entrykey="helloService"value-ref="helloService"在這里并沒有直接聲明一個(gè)HelloJobBean,而是聲明了一個(gè)JobDetailBean。這是使用Quartz時(shí)的一個(gè)特點(diǎn)。它要求通過jobClass屬性來設(shè)置一個(gè)Job對(duì)象。QuartzJobDetailHelloJobhelloService間接設(shè)置的。JobDetailBean的jobDataAsMap屬性接受一個(gè)java.util.Map,其中包含了需要設(shè)置給jobClass的各種屬性。在這里,這個(gè)map包含了一個(gè)指向helloServiceBean的引用,它的鍵值為helloService。當(dāng)JobDetailBean實(shí)例化時(shí),它會(huì)將helloServiceBean注入到HelloJob的helloService屬性中?,F(xiàn)在工作已經(jīng)被定義好了,接下來你需要調(diào)度這個(gè)工作。Quartz的org.quartz.TriggerQuartzSpring兩個(gè)觸發(fā)器,SimpleTriggerBeanCronTriggerBean。我們使用CronTriggerBean<beanid="cronHelloTrigger"<propertyname="jobDetail"ref="helloJob"<propertyname="cronExpression"value="006**?"一個(gè)cron表達(dá)式有至少6個(gè)(也可能是7個(gè))由空格分隔的時(shí)間元素。從左至右, 達(dá) 達(dá) 0010,14,16**00,15,30,45*1-10*300011?008-18?*MON-<bean<property屬性triggers接受一組觸發(fā)器。由于目前只有一個(gè)觸發(fā)器,因此只需簡(jiǎn)單地裝配一個(gè)包含cronReportTriggerBean的一個(gè)引用的列表即可。 7一個(gè)cron6(7個(gè))由空格分隔的時(shí)間元素。從左至右,月份(1–12或星期中的日期(1–7或 達(dá) 0010,14,16**00,15,30,45*1-10*1015300011?20121130008-18?*MON-<bean<property<refbean="cronHelloTrigger"屬性triggers接受一組觸發(fā)器。由于目前只有一個(gè)觸發(fā)器,因此只需簡(jiǎn)單地裝配一個(gè)包含cronReportTriggerBean的一個(gè)引用的列表即可。HelloJob承了QuartzJobBean類,并且需要實(shí)現(xiàn)executeInternal方法.但是在executeInternal方法中我們只是簡(jiǎn)單的調(diào)用了helloService,能不能不用編寫HelloJob,就可以調(diào)用helloService方法?Spring提供了和MethodInvokingJobDetailFactoryBeanJavaQuartz調(diào)度器對(duì)方法調(diào)用進(jìn)行調(diào)度 8HelloJob承了QuartzJobBeanexecuteInternal方法.但是在executeInternal方法中我們只是簡(jiǎn)單的調(diào)用了helloService,能不能不用編寫HelloJob,就可以調(diào)用helloService方法?Spring提供了和MethodInvokingJobDetailFactoryBean,可以分別使用JavaHelloJob<beanid="helloJob"<property<beanid="cronHelloTrigger"<propertyname="cronExpression"value="1****?"<bean<property 9<!--工作--><!--工作--><beanid="helloJob"<propertyname="jobClass"value="com.wq.job.HelloJob"<property<entrykey="helloService"value-ref="helloService"<!--啟動(dòng)工作--><bean<property<refbean="cronHelloTrigger"<!--調(diào)度工作--><beanid="cronHelloTrigger"<propertyname="jobDetail"ref="helloJob"<propertyname="cronExpression"value="1****?"<beanid="helloJob"<beanid="cronHelloTrigger"<propertyname="cronExpression"value="006**<bean<property<ref 10<!--工作--><!

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論