1. 程式人生 > >quartz定時任務實現

quartz定時任務實現

適用於spring框架

1.在web.xml檔案中引入spring-quartz.xml檔案。

<!-- 讀取spring配置檔案 -->  <context-param>   <param-name>contextConfigLocation</param-name>   <param-value>classpath:config/spring.xml;             classpath:config/spring-mybatis.xml;              classpath:config/spring-quartz.xml;         </param-value>  </context-param>

2.spring-quartz.xml檔案如下

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:aop="

http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"     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         http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd         http://www.springframework.org/schema/cache         http://www.springframework.org/schema/cache/spring-cache.xsd">     <!-- 定時器工作排程的bean -->      <bean id="trainPassengerTraffic" class="springmvc.wx.controller.train.trainPassengerTraffic" />     <bean id="trainCode" class="springmvc.wx.controller.train.trainCode" />     <bean id="trainIntoTimeShare" class="springmvc.wx.controller.train.trainIntoTimeShare" />     <bean id="trainLateInfo" class="springmvc.wx.controller.train.trainLateInfo" />     <bean id="trainleaveTimeShare" class="springmvc.wx.controller.train.trainleaveTimeShare" />     <bean id="trainTwoInto" class="springmvc.wx.controller.train.trainTwoInto" />     <bean id="trainTwoLeave" class="springmvc.wx.controller.train.trainTwoLeave" />     <!-- job的配置開始 -->      <bean id="job1"         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <property name="targetObject" ref="trainPassengerTraffic"/>         <property name="targetMethod" value="getValue"/>         <property name="concurrent" value="true"/>      </bean>      <bean id="job2"         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <property name="targetObject" ref="trainCode"/>         <property name="targetMethod" value="getValue"/>         <property name="concurrent" value="true"/>      </bean>      <bean id="job3"         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <property name="targetObject" ref="trainIntoTimeShare"/>         <property name="targetMethod" value="getValue"/>         <property name="concurrent" value="true"/>      </bean>      <bean id="job4"         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <property name="targetObject" ref="trainLateInfo"/>         <property name="targetMethod" value="getValue"/>         <property name="concurrent" value="true"/>      </bean>      <bean id="job5"         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <property name="targetObject" ref="trainleaveTimeShare"/>         <property name="targetMethod" value="getValue"/>         <property name="concurrent" value="true"/>      </bean>      <bean id="job6"         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <property name="targetObject" ref="trainTwoInto"/>         <property name="targetMethod" value="getValue"/>         <property name="concurrent" value="true"/>      </bean>      <bean id="job7"         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <property name="targetObject" ref="trainTwoLeave"/>         <property name="targetMethod" value="getValue"/>         <property name="concurrent" value="true"/>      </bean>     <!-- 排程的配置開始 -->     <!-- 客運量統計資料:資料更新頻率:每天更新一次,大約每天早上9點更新完成。 -->     <bean id="crontestJobTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">         <property name="jobDetail">             <ref bean="job1" />         </property>         <property name="cronExpression">              <value>0 15 9 * * ? </value>          </property>     </bean>     <!-- 車次統計資料:資料更新頻率:每天更新一次,大約每天早上6點更新完成。 -->      <bean id="crontestJobTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">         <property name="jobDetail">             <ref bean="job2" />         </property>         <property name="cronExpression">              <value>0 15 6 * * ? </value>          </property>     </bean>       <!-- 到站列車分時統計資料:  資料更新頻率:2小時更新一次,1、3、5…23點更新 -->      <bean id="crontestJobTrigger3" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">         <property name="jobDetail">             <ref bean="job3" />         </property>         <property name="cronExpression">              <value>0 0 1,3,5,7,9,11,13,15,17,19,21,23 * * ? </value>          </property>     </bean>      <!-- 列車晚點統計資料:資料更新頻率:每小時更新一次,每小時的15分左右更新資料。-->      <bean id="crontestJobTrigger4" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">         <property name="jobDetail">             <ref bean="job4" />         </property>         <property name="cronExpression">              <value>0 20 0/1 * * ? </value>          </property>     </bean>      <!-- 離站列車分時統計資料:  資料更新頻率:2小時更新一次,1、3、5…23點更新 -->      <bean id="crontestJobTrigger5" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">         <property name="jobDetail">             <ref bean="job5" />         </property>         <property name="cronExpression">              <value>0 0 1,3,5,7,9,11,13,15,17,19,21,23 * * ?</value>          </property>     </bean>     <!-- 2小時進站統計資料:  資料更新頻率:2小時更新一次,1、3、5…23點更新 -->      <bean id="crontestJobTrigger6" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">         <property name="jobDetail">             <ref bean="job6" />         </property>         <property name="cronExpression">              <value>0 0 1,3,5,7,9,11,13,15,17,19,21,23 * * ?</value>          </property>     </bean>     <!-- 2小時離站統計資料:  資料更新頻率:2小時更新一次,1、3、5…23點更新 -->      <bean id="crontestJobTrigger7" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">         <property name="jobDetail">             <ref bean="job7" />         </property>         <property name="cronExpression">              <value>0 0 1,3,5,7,9,11,13,15,17,19,21,23 * * ?</value>          </property>     </bean>     <!-- 啟動觸發器的配置開始 -->     <bean name="startQuertz1" lazy-init="false" autowire="no"         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">         <property name="triggers">             <list>                 <ref bean="crontestJobTrigger1" />             </list>         </property>     </bean>      <bean name="startQuertz2" lazy-init="false" autowire="no"         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">         <property name="triggers">             <list>                 <ref bean="crontestJobTrigger2" />             </list>         </property>     </bean>      <bean name="startQuertz3" lazy-init="false" autowire="no"         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">         <property name="triggers">             <list>                 <ref bean="crontestJobTrigger3" />             </list>         </property>     </bean>      <bean name="startQuertz4" lazy-init="false" autowire="no"         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">         <property name="triggers">             <list>                 <ref bean="crontestJobTrigger4" />             </list>         </property>     </bean>      <bean name="startQuertz5" lazy-init="false" autowire="no"         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">         <property name="triggers">             <list>                 <ref bean="crontestJobTrigger5" />             </list>         </property>     </bean>      <bean name="startQuertz6" lazy-init="false" autowire="no"         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">         <property name="triggers">             <list>                 <ref bean="crontestJobTrigger6" />             </list>         </property>     </bean>      <bean name="startQuertz7" lazy-init="false" autowire="no"         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">         <property name="triggers">             <list>                 <ref bean="crontestJobTrigger7" />             </list>         </property>     </bean>         </beans>