Spring集成Quartz定時器實現定時作業任務
來自: http://blog.csdn.net//chenleixing/article/details/43319193
這篇文章有點久遠了,記得是第一次出來實習時,大三暑假自己找了一家軟件公司實習了很長時間,當時學到很多實踐性的東西,這個Quartz就是其中一個,還記得是做OA,一些消息需要定時提醒定時刪除,項目框架里也沒有,當時經驗不足也沒接觸過這樣類似的東西,所以用了整一下午的時間算是基本搞定了吧,今天很有興致,找出這篇大學時寫過的幾篇文章分享出來,內容如下:
1、 創建一個定時任務并繼承類QuartzJobBean并實現其方法executeInternal(JobExecutionContext context)需要定時的任務嵌入其中:
package com.test;
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; import com.test.StarShopManager;
public class TestJob extends QuartzJobBean { //引用的service(可以多個) private StarShopManager starShopManager; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { //添加定時任務程序 starShopManager.update(starshopdo); } public void setStarShopManager(StarShopManager starShopManager) { this.starShopManager = starShopManager; } } |
<!—配置定時任務--> <bean id="testJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>com.test.TestJob</value> </property> <property name="jobDataAsMap"> <map> <!—不是spring 管理的service需要放到這里--> <entry key="timeout"> <value>5</value> </entry> </map> </property> </bean>
<!—調度定時任務,這里使用了CronTirgger觸發器--> <bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="testJob" /> </property> <property name="cronExpression"> <value>0 0 * * * ?</value> </property> </bean> <bean id="test" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="schedulerContextAsMap"> <map> <!-- spring 管理的service需要放到這里,才能夠注入成功 --> <description>schedulerContextAsMap</description> <entry key="starShopManager" value-ref="StarShopManager" /> </map> </property> <property name="triggers"> <list> <ref bean="testTrigger" /> </list> </property> </bean> |