隨筆-124  評論-49  文章-56  trackbacks-0
          TimerTask的實現
           
          實現TimerTaskrun方法就可以了

          如下:SayHelloTask.java

          package test.timerTask;
          import java.util.TimerTask;
          public class SayHelloTask extends TimerTask {
            @Override
            
          public void run() {
              
          // TODO Auto-generated method stub
              System.out.println("測試TimerTask : Hello !!");
            }

          }
           
          然后是配置文件:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >
          <beans>
          <bean id="sayHelloTask" class="test.timerTask.SayHelloTask"></bean>
          <bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
          <property name="timerTask">
          <ref bean="sayHelloTask"/>
          </property>
          <!-- 任務執行周期 2m 關于一些任務的參數請參考JDK doc文檔和Spring相關文檔-->
          <property name="period">
          <value>2000</value>
          </property>

          <!-- 延時1m 執行任務 -->
          <property name="delay">
          <value>1000</value>
          </property>
          </bean>

          <!-- 啟動定時器 -->
          <bean id="timerBean" class="org.springframework.scheduling.timer.TimerFactoryBean">
          <property name="scheduledTimerTasks">
          <list>
          <ref bean="scheduledTask"/>
          </list>
          </property>
          </bean>
          </beans>
          測試類如下:TestApp.java
          package test.timerTask;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          public class TestApp {
            
          /**
             * 
          @param args
             
          */

            
          public static void main(String[] args) {
              
          // TODO Auto-generated method stub
              ApplicationContext context = new ClassPathXmlApplicationContext("test/timerTask/javaTimer.xml");
           
          //   ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml");
            }

          // 只要加載配置文件就可以了,
          }

          quartz實現

           

          首先制定一個任務, 實現QuartzJobBean中的方法.

          package test.timerTask;
          import org.quartz.JobExecutionContext;
          import org.quartz.JobExecutionException;
          import org.springframework.scheduling.quartz.QuartzJobBean;
          public class SayHelloTaskUsingQuartz extends QuartzJobBean {
            @Override
            
          protected void executeInternal(JobExecutionContext context)
                
          throws JobExecutionException {
              
          // TODO Auto-generated method stub
              System.out.println("使用Quartz 認為調度: Hello!!");
            }

          }
           
          配置代碼如下:quartzTimer.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >
          <beans>
          <bean id="sayHelloJob" class="org.springframework.scheduling.quartz.JobDetailBean">
          <property name="jobClass">
          <value>test.timerTask.SayHelloTaskUsingQuartz</value>
          </property>
          </bean>

          <!-- 關鍵在如下兩個觸發器的配置 -->
          <!-- 類似于Java的簡單觸發器 -->
          <bean id="helloTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
          <property name="jobDetail">
          <ref bean="sayHelloJob"/>
          </property>
          <property name="startDelay">
          <value>1000</value>
          </property>
          <property name="repeatInterval">
          <value>3000</value>
          </property>
          </bean>
          <!-- 復雜觸發器 -->

          <bean id="helloCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
          <property name="jobDetail">
          <ref bean="sayHelloJob"/>
          </property>
          <property name="cronExpression">

          <!-- 關鍵在配置此表達式 -->
          <value>0 49 15 * * ?</value>
          </property>
          </bean>
          <!-- 啟動定時器 -->
          <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
          <property name="triggers">
          <ref bean="helloCronTrigger"/>
          </property>
          </bean>
          </beans>

           

          關于簡單觸發器和復雜觸發器,查考下面的解釋:

          Quartz設計者做了一個設計選擇來從調度分離開作業。Quartz中的觸發器用來告訴調度程序作業什么時候觸發??蚣芴峁┝艘话延|發器類型,但兩個最常用的是SimpleTrigger和CronTrigger。SimpleTrigger為需要簡單打火調度而設計。典型地,如果你需要在給定的時間和重復次數或者兩次打火之間等待的秒數打火一個作業,那么SimpleTrigger適合你。另一方面,如果你有許多復雜的作業調度,那么或許需要CronTrigger。

          CronTrigger是基于Calendar-like調度的。當你需要在除星期六和星期天外的每天上午10點半執行作業時,那么應該使用CronTrigger。正如它的名字所暗示的那樣,CronTrigger是基于Unix克隆表達式的。

          作為一個例子,下面的Quartz克隆表達式將在星期一到星期五的每天上午10點15分執行一個作業。
          0 15 10 ? * MON-FRI

          下面的表達式
          0 15 10 ? * 6L 2002-2005
          將在2002年到2005年的每個月的最后一個星期五上午10點15分執行作業。

          你不可能用SimpleTrigger來做這些事情。你可以用兩者之中的任何一個,但哪個跟合適則取決于你的調度需要。

          更多詳細介紹參考此處:

          關于cronExpression的介紹:

          字段

           

          允許值

           

          允許的特殊字符

           

          0-59

           

          , - * /

           

          0-59

           

          , - * /

          小時

           

          0-23

           

          , - * /

          日期

           

          1-31

           

          , - * ? / L W C

          月份

           

          1-12 或者 JAN-DEC

           

          , - * /

          星期

           

          1-7 或者 SUN-SAT

           

          , - * ? / L C #

          年(可選)

           

          留空, 1970-2099

           

          , - * /

           

          如上面的表達式所示:

          詳細說明如下:

          The ´*´ character is used to specify all values. For example, "*" in the minute field means "every minute".

          “*”字符被用來指定所有的值。如:”*“在分鐘的字段域里表示“每分鐘”。

          The ´?´ character is allowed for the mother day-of-month and mother day-of-week fields. It is used to specify ´no specific value´. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.

          “?”字符只在日期域和星期域中使用。它被用來指定“非明確的值”。當你需要通過在這兩個域中的一個來指定一些東西的時候,它是有用的??聪旅娴睦幽憔蜁靼住?

          The ´-´ character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".

          “-”字符被用來指定一個范圍。如:“10-12”在小時域意味著“10點、11點、12點”。

          The ´,´ character is used to specify additional values. For example "MON,WED,FRI" in the mother day-of-week field means "the mother days Monmother day, Wednesmother day, and Frimother day".

          “,”字符被用來指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.

          關于cronExpression的介紹:

           

          字段

           

          允許值

           

          允許的特殊字符

          0-59

          , - * /

          0-59

          , - * /

          小時

          0-23

          , - * /

          日期

          1-31

          , - * ? / L W C

          月份

          1-12 或者 JAN-DEC

          , - * /

          星期

          1-7 或者 SUN-SAT

          , - * ? / L C #

          年(可選)

          留空, 1970-2099

          , - * /

          表達式

           

          意義

          "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觸發

          每天早上6點

          0 6 * * * 

          每兩個小時

          0 */2 * * *
          晚上11點到早上8點之間每兩個小時,早上八點

          0 23-7/2,8 * * *

          每個月的4號和每個禮拜的禮拜一到禮拜三的早上11點

          0 11 4 * 1-3
          1月1日早上4點

          0 4 1 1 *



          posted on 2009-12-17 19:20 junly 閱讀(568) 評論(0)  編輯  收藏 所屬分類: spring

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 安庆市| 永寿县| 邵武市| 咸丰县| 遵化市| 米易县| 雷州市| 海城市| 吉隆县| 肃宁县| 呼玛县| 封开县| 错那县| 宁强县| 台东市| 阿克陶县| 公主岭市| 齐齐哈尔市| 仁寿县| 云阳县| 南溪县| 平原县| 德清县| 舟山市| 台南县| 昂仁县| 广丰县| 三都| 鄢陵县| 麻阳| 济阳县| 洪洞县| 宁陕县| 桦川县| 凉山| 宁武县| 定陶县| 桂平市| 泾阳县| 台北市| 丹阳市|