Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Spring 動態定時器:讓執行時間由程式決定

關於 Spring 的靜態定時器,可以參考這兩篇:
Spring3.0 定時任務(一)簡單示例
Spring 定時任務(二)多個任務與執行時間間隔配置

為什麼需要動態

過去一直用靜態定時器,非常簡單,只要兩件事:寫好執行任務的類別與方法,再寫好設定檔。任務間隔也寫死在設定檔裡。資料備份、資料同步這類固定節奏的工作,這樣就夠了;但遇到需要更靈活調整的場合便束手無策,於是有了動態定時器的概念。

動態定時器:執行任務的時間由程式自己控制,而不是由設定檔決定。

核心在 cronExpression

定時的關鍵是 cronExpression,也就是設定檔裡配置的這一段:

<property name="cronExpression">
       <value>0/10 * * * * ?</value>
</property>

只要能動態設定這個參數,就實現了動態定時器。靜態版寫在設定檔,動態版寫進程式——做法就是自訂一個類別繼承 CronTriggerBean,在構造方法中完成設定。

執行任務的類別

運行環境為 spring 2.5.6 + quartz 1.8(Spring 一般建議 3.0 以上,此專案沿用 2.5),相關 jar 與設定流程此處略過。

package test;
import java.text.SimpleDateFormat;
import java.util.Date;

//執行任務的類別
public class ScheduleInfoAction{
   //執行任務的方法
   public void reScheduleJob() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("執行操作" + dateFormat.format(new Date()));
  }
}

自訂觸發器

package test;

import java.io.Serializable;
import java.text.ParseException;
import org.springframework.scheduling.quartz.CronTriggerBean;

//繼承 CronTriggerBean,父類就是靜態定時器中用到的觸發器
public class InitCronTrigger extends CronTriggerBean implements Serializable {

   public InitCronTrigger() throws ParseException {
       setCronExpression(getCronExceptionDB()); //在構造方法中設置 cronExpression
   }
   private String getCronExceptionDB(){
       String sql = "select CRON from t_test_task_trigger where available = 1 and trigger_name = 'cronTrigger'";
       System.out.println("*****" + sql);
       return "* * * 11 12 ?"; //測試用直接回傳,實務可改為從資料庫讀取
  }
}

設定檔

<?xml version="1.0" encoding="UTF-8"?>
   <beans xmlns="https://www.springframework.org/schema/beans"
     xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="https://www.springframework.org/schema/aop"
     xmlns:tx="https://www.springframework.org/schema/tx"
     xsi:schemaLocation="https://www.springframework.org/schema/beans
     https://www.springframework.org/schema/beans/spring-beans-2.0.xsd
     https://www.springframework.org/schema/aop
     https://www.springframework.org/schema/aop/spring-aop-2.0.xsd
     https://www.springframework.org/schema/tx
     https://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
     default-autowire="no" default-lazy-init="false">

   <!-- 配置 spring 響應觸發器的入口 -->
   <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
       <property name="triggers">
          <list>
             <ref local="cronTrigger"/>
          </list>
       </property>
   </bean>
   <bean id="scheduleInfoAction" class="test.ScheduleInfoAction"/>

   <!-- 此處注意:靜態定時器的 class 為 org.springframework.scheduling.quartz.CronTriggerBean,
     我用了自訂的類,它繼承了靜態定時器的類,並把執行時間的設置放進構造方法,這是動態定時器的一種實現方式。 -->
  <bean id="cronTrigger" class="test.InitCronTrigger">
       <property name="jobDetail" ref="jobDetail"/>
       <!--
       <property name="cronExpression">
          <value>0/10 * * * * ?</value>
       </property>
       -->
  </bean>

   <!-- 配置執行任務的類和方法 -->
   <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
       <property name="targetObject" ref="scheduleInfoAction"/><!-- 執行任務的物件 -->
       <property name="targetMethod" value="reScheduleJob"/><!-- 執行任務的方法,此處不帶參數,即使實際方法帶參數,執行時也會被略掉 -->
       <!-- concurrent 設為 false,多個 job 不會並發運行 -->
       <property name="concurrent" value="false"/>
   </bean>
</beans>

關鍵對比只有一行:原本 class 寫的是 CronTriggerBean,現在換成自己繼承出來的 InitCronTrigger,並把 cronExpression 的設定搬進構造方法。若觸發時間來源是資料庫,只要讓 getCronExceptionDB() 真正去查詢即可。

Copyright © Linux教程網 All Rights Reserved