Spring進階-怎樣整合定時調度Quartz,springquartz

來源:互聯網
上載者:User

Spring進階-怎樣整合定時調度Quartz,springquartz

  在一些項目裡面比如進銷存系統,對一些到期圖片的定時清理或者庫存不足發出預警提示,就需要用到定時調度技術。

  每當經過一段時間,程式會自動執行,就是定時調度。如果要使用定時調度,則必須保證程式始終運行才行哦,也就是說定時調度是在程式之外又啟動了一個新的線程。那麼怎樣才能實現定時調度呢?

  方案一:可以使用JDK內建的java.util.Timer對象。可以建立定製的timer或者調用某些方法的timer。封裝timer的工作由Spring TimerFactoryBean完成,不過這種方法不能準確設定一個起始時間。

 

  方案二:使用OpenSymphony Quartz架構。Quartz是OpenSymphony開源組織在Job scheduling領域又一個開源項目,網站地址:http://www.quartz-scheduler.org;它可以與J2EE與J2SE應用程式相結合也可以單獨使用。Quartz可以用來建立簡單或為運行好幾萬個Jobs這樣複雜的程式。(Struts2裡面的WebWork就是來自OpenSymphony。)

 

  Quartz使用Trigger, Job以及JobDetail等對象來進行各種類型的任務調度。為了讓基於Spring的應用程式方便使用,Spring提供了一些類來簡化quartz的用法。

 

開發步驟:  一、加入spring和quartz相關jar檔案

 

  二、Spring設定檔

  1.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"

  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 總管理類 如果將lazy-init='false'那麼容器啟動就會執行發送器 -->

<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="triggers">

<list>

<ref bean="doTime" />

</list>

</property>

</bean>

<!-- 定義觸發時間 -->

<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail">

<ref bean="jobtask" />

</property>

<property name="cronExpression">

<!-- cron運算式:在每天早上8點到晚上8點期間每1分鐘觸發一次 -->

<!-- <value>0 0/1 8-20 * * ?</value> -->

<!-- cron運算式:每5分鐘觸發一次 -->

<value>0 0/5 * * * ?</value>

</property>

</bean>

<!-- 定義調用對象和調用對象的方法 -->

<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<!-- 調用的類 -->

<property name="targetObject" ref="quartzJob" />

<!-- 調用類中的方法名稱 -->

<property name="targetMethod">

<value>work</value>

</property>

</bean>

<!-- 要調用的工作類 -->

<bean id="quartzJob" class="cd.itsource.pss.service.impl.QuartzJobServiceImpl">

<property name="baseDao" ref="baseDao" />

</bean>

 

</beans>

 

  2.Spring設定檔二:加入spring命名空間,簡化配置,強焊啊。原理就是在上面設定檔一的基礎上spring再次做了一次封裝,從而簡化配置(具體可以看spring-task.xsd檔案)。

<?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:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

 

<!-- cron運算式:在每天早上8點到晚上8點期間每1分鐘觸發一次 -->

<!--value>0 0/1 8-20 * * ?</value -->

<!-- cron運算式:每1分鐘觸發一次 -->

<!-- <value>0 0/1 * * * ?</value> -->

<!-- cron運算式:每天AM3點鐘觸發一次 -->

<!-- <value>0 0 3 * * ?</value> -->

 

<task:scheduled-tasks>

<!-- 執行quartzJob裡面的work方法,執行頻率是cron運算式 -->

<task:scheduled ref="quartzJob" method="work" cron="0 0/1 * * * ?" />

</task:scheduled-tasks>

 

<!-- 要調用的工作類 -->

<bean id="quartzJob" class="cn.itsource.pss.service.impl.QuartzJobServiceImpl">

<property name="baseDao" ref="baseDao" />

</bean>

 

</beans>

 

三、Junit

系統時間跑到整分的時候上面service的work方法自動被執行,間隔1分鐘,再次被執行,後面迴圈執行。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.