http://blog.csdn.net/arui_email/article/details/9767447
- 1、applicationContext-job.xml 配置文件如下:
- <?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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="receiveWSDate"
- class="com.XXXX.agent.webservices.client.WebServiceClient">
- </bean>
- <bean id="receiveWSDateJob"
- class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject" ref="receiveWSDate"></property>
- <property name="targetMethod" value="getAgentInfo"></property>
- <property name="concurrent" value="false"></property>
- </bean>
- <!-- 調用觸發器trigger 每週一至週五 凌晨02點執行一次同步 -->
- <bean id="receiveWSDateTrigger"
- class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail" ref="receiveWSDateJob"></property>
- <property name="cronExpression">
- <value>0 00 02 ? * MON-FRI</value>
- </property>
- </bean>
- <!-- 調度工廠 -->
- <bean id="xxxx_jobScheduler"
- class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="receiveWSDateTrigger" />
- </list>
- </property>
- <!-- property name="startupDelay" value="5"></property-->
- </bean>
- <!-- ********************chenxl,任務管理分配,開始******************************************* -->
- <bean id="taskWSHost"
- class="com.XXXX.agent.webservices.host.TaskWSHost">
- </bean>
- <bean id="taskWSHostJob"
- class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject" ref="taskWSHost"></property>
- <property name="targetMethod" value="getTaskInfo"></property>
- <property name="concurrent" value="false"></property>
- </bean>
- <!-- 調用觸發器trigger 每週一至週五 早上9點到下午6點每五分鐘執行一次 -->
- <bean id="taskWSHostTrigger"
- class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail" ref="taskWSHostJob"></property>
- <property name="cronExpression">
- <value>0 0/5 09-18 ? * MON-FRI</value>
- </property>
- </bean>
- <!-- 調度工廠 -->
- <bean id="xxxx_jobScheduler2"
- class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="taskWSHostTrigger" />
- </list>
- </property>
- <!-- property name="startupDelay" value="5"></property-->
- </bean>
- <!-- ********************chenxl,任務管理分配,結束******************************************* -->
- </beans>
- 2、webservice服務端TaskWSHost.java
- package com.XXX.agent.webservices.host;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.XXX.agent.service.IAutoOutVisitService;
- /**
- *
- * 此類描述的是: 任務管理的webservice服務端
- * @author: <a target="_blank" href="mailto:[email protected]">[email protected]</a>
- * @version: 2013-7-29 上午11:15:39
- */
- public class TaskWSHost {
- private static ApplicationContext appcontext;
- protected final Log logger = LogFactory.getLog(this.getClass().getName());
- public static ApplicationContext initBeanFactory() {
- if(null == appcontext) {
- appcontext = new ClassPathXmlApplicationContext("classpath:applicationContext-resource.xml");
- }
- return appcontext;
- }
- /**
- *
- * 此方法描述的是: 自動外乎回訪,把數據給青牛
- * @author: <a target="_blank" href="mailto:[email protected]">[email protected]</a>
- * @version: 2013-7-31 下午04:09:29
- * @param name
- * @return String
- */
- public String getTaskInfo(){
- appcontext = initBeanFactory(); //獲取getBean對象
- IAutoOutVisitService iAutoOutVisitService = (IAutoOutVisitService) appcontext.getBean("mgrAutoOutVisitService");
- String str = iAutoOutVisitService.getTaskInfoStr();//把相關數據推送給青牛
- System.out.println("===================str:"+str);
- return str;
- }
- /**
- *
- * 此方法描述的是: 獲取從青牛返回的數據
- * @author: <a target="_blank" href="mailto:[email protected]">[email protected]</a>
- * @version: 2013-7-30 下午03:01:08
- * @param mobile :電話號碼
- * @param userName :姓名(負責人)
- * @param windowName :窗口名稱
- * @param orderTel :下單電話
- * @param selectTel :查詢電話
- * @param complainTel :投訴電話
- * @param obStatus :外撥電話狀態
- * @param resultFlag :確認結果
- * @param startDate :外撥電話時間
- * @param endDate :外撥結束時間
- * @return String
- */
- public String getReturnData(String mobile, String userName, String windowName, String orderTel, String selectTel,
- String complainTel, String obStatus, String resultFlag, String startDate, String endDate){
- appcontext = initBeanFactory(); //獲取getBean對象
- IAutoOutVisitService iAutoOutVisitService = (IAutoOutVisitService) appcontext.getBean("stasAutoOutVisitService");
- String str = iAutoOutVisitService.getReturnInfo(mobile, userName, windowName, orderTel, selectTel,
- complainTel, obStatus, resultFlag, startDate, endDate);
- return str;
- }
- }
- 下面是在網上搜索的一些信息,方便大家查看就正過來了:
- <pre class="html" name="code">關於簡單觸發器和複雜觸發器,查考下面的解釋:
- Quartz設計者做了一個設計選擇來從調度分離開作業。Quartz中的觸發器用來告訴調度程序作業什麼時候觸發。框架提供了一把觸發器類型,但兩個最常用的是</pre><pre class="html" name="code">SimpleTrigger和CronTrigger。</pre><pre class="html" name="code">SimpleTrigger爲需要簡單打火調度而設計。典型地,如果你需要在給定的時間和重複次數或者兩次打火之間等待的秒數打火一個作業,那麼SimpleTrigger適合你。</pre><pre class="html" name="code">另一方面,如果你有許多複雜的作業調度,那麼或許需要CronTrigger。
- CronTrigger是基於Calendar-like調度的。當你需要在除星期六和星期天外的每天上午10點半執行作業時,那麼應該使用CronTrigger。正如它的名字所暗示的那樣,</pre><pre class="html" name="code">CronTrigger是基於Unix克隆表達式的。
- 作爲一個例子,下面的Quartz克隆表達式將在星期一到星期五的每天上午10點15分執行一個作業。
- 0 15 10 * MON-FRI
- 下面的表達式
- 0 15 10 * 6L 2002-2005
- 將在2002年到2005年的每個月的最後一個星期五上午10點15分執行作業。
- 你不可能用SimpleTrigger來做這些事情。你可以用兩者之中的任何一個,但哪個跟合適則取決於你的調度需要。
- 更多詳細介紹參考此處:
- 關於cronExpression的介紹:
- </pre><pre class="html" name="code">字段 允許值 允許的特殊字符
- 秒 0-59, - * /
- 分 0-59, - * /
- 小時 0-23, - * /
- 日期 1-31, - * / L W C
- 月份 1-12 或者 JAN-DEC, - * /
- 星期 1-7 或者 SUN-SAT, - * / L C #
- 年(可選)留空, 1970-2099, - * /
- 如上面的表達式所示:
- “*”字符被用來指定所有的值。如:”*“在分鐘的字段域裏表示“每分鐘”。
- “-”字符被用來指定一個範圍。如:“10-12”在小時域意味着“10點、11點、12點”。
- </pre><pre class="html" name="code">
- “,”字符被用來指定另外的值。如:“MON,WED,FRI”在星期域裏表示”星期一、星期三、星期五”.
- “?”字符只在日期域和星期域中使用。它被用來指定“非明確的值”。當你需要通過在這兩個域中的一個來指定一些東西的時候,它是有用的。看下面的例子你就會明白。
- “L”字符指定在月或者星期中的某天(最後一天)。即“Last ”的縮寫。但是在星期和月中“L”表示不同的意思,如:在月子段中“L”指月份的最後一天-1月31日,2月28日</pre><pre class="html" name="code">,如果在星期字段中則簡單的表示爲“7”或者“SAT”。如果在星期字段中在某個value值得後面,則表示“某月的最後一個星期value”,如“6L”表示某月的最後一個星期五。
- “W”字符只能用在月份字段中,該字段指定了離指定日期最近的那個星期日。
- “#”字符只能用在星期字段,該字段指定了第幾個星期value在某月中
- 表達式 意義
- "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觸發
- </pre><pre class="html" name="code"> </pre><pre class="html" name="code">每天早上6點 0 6 * * *
- 每兩個小時 0 */2 * * *</pre><pre class="html" name="code"> </pre><pre class="html" name="code">
- 晚上11點到早上8點之間每兩個小時,早上八點 0 23-7/2,8 * * *
- 每個月的4號和每個禮拜的禮拜一到禮拜三的早上11點 0 11 4 * 1-3
- </pre><pre class="html" name="code"> </pre><pre class="html" name="code">1月1日早上4點 0 4 1 1 *
- 定時批處理作業是J2EE企業應用裏很重要的一環,用來在晚間進行財務掛賬,數據轉存,新聞聯播等等操作。
- 而在Spring裏,已經很好的集成了Quartz,簡單到像配cron一樣,在xml文件裏面配一下時間就可以自動執行,不需要寫一行代碼。Spring對Quartz大刀闊斧的簡化堪稱範例,Quartz項目組也許可以學習一下。
- <bean id="methodInvokingJobDetail"
- class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject"><ref bean="financeDAO"/></property>
- <property name="targetMethod"><value>confirmOrder</value></property>
- </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
- <ref bean="methodInvokingJobDetail"/>
- </property>
- <property name="cronExpression">
- <value>0 0 6,12,20 * * ?</value>
- </property>
- </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list><ref local="cronTrigger"/></list>
- </property>
- </bean>
- 上面這段配置文件規定了在早上6點和晚上8點執行financeDAO對象的confirmOrder()方法.
- </pre><br>
- <pre></pre>
- <p> </p>