1. 程式人生 > >Spring-Quertz配置每隔三個小時執行一次函式

Spring-Quertz配置每隔三個小時執行一次函式

  • applicationContext-quertz.xml的配置,開啟job任務註解
<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd "
>
<task:annotation-driven/> </beans>
  • 假設有一個類叫Timmer
@Component(Timmer.serialVersionUID + "")
public class Timmer implements Serializable{

    private static final long serialVersionUID = -2499873556819765772L;

    private static Log logger = LogFactory.getLog(Timmer.class);

    @Scheduled
(cron = "0 0 */3 * * ?") public void timer() throws Exception { try{ //處理當前一天的資料 }catch (Exception e) { logger.debug(e.getMessage()); } } }
  • 由於不止一個這樣的timmer所以,當第一個timmer到第二個timmer的時候會存在一定的誤差,所以就需要考慮到凌晨十二點不是整點觸發進入程式的,所以這個誤差的範圍需要考慮,又因為每天凌晨跑的應該是昨天一天的資料而且誤差範圍不能大於零點之後的三個小時因為凌晨三點執行的是當天的資料,所以說誤差範圍就是當前時間小於凌晨三點就跑昨天的資料。
我們用Calendar類進行處理,Date不適用於改變時間
首先獲取當前的時間
1.獲取Calendar的物件 Calendar c = Calendar.getInstance();
2.獲取當前時間的毫秒數用來比較當天凌晨三點的毫秒數,判斷時間範圍用的
long currentMills = c.getTimeInMillis(); 
3.//c.getTime();得到的是一個當前時間的Date物件
計算一天的資料為 當天的頭一天的23:59:59到當天的明天的00:00:00
4.獲取頭一天的235959秒,並格式化時間為“YYYY-MM-dd HH:mm:ss”
String sdTime = ReportUitls.format(TimeUtil.preDay(c.getTime()));
TimeUtil.preDay()的函式為:
public Date preDay(Date date){
    Calendar c1 = Calendar.getInstance();
    c1.setTime(date);
    c1.add(c1.DATE,-1); //c.DATE 指示一個月中的某天。DAY_OF_MONTH 也可以
    c1.set(c.get(c1.YEAR),c1.get(c1.MONTH),c1.get(c1.DAY_OF_MONTH),23,59,59);   
    //get(int field)  返回給定日曆欄位的值。
    return new Date(c.getTimeInMillis());
}   

5.獲取頭一天的000000秒,並格式化時間為“YYYY-MM-dd HH:mm:ss”
String edTime = ReportUitls.format(TimeUtil.backDay(c.getTime()));
TimeUtil.backDay()的函式為:
public Date backDay(Date date){
    Calendar c1 = Calendar.getInstance();
    c1.setTime(date);
    c1.add(c1.DATE,1); //c.DATE 指示一個月中的某天。DAY_OF_MONTH 也可以
    c1.set(c.get(c1.YEAR),c1.get(c1.MONTH),c1.get(c1.DAY_OF_MONTH),00,00,00);   
    //get(int field)  返回給定日曆欄位的值。
    return new Date(c.getTimeInMillis());
}
6.或指定的當天的凌晨三點的Calendar物件
    Calendar threeCal = Calendar.getInstance();
    threeCal.set(threeCal.get(threeCal .YEAR),threeCal .get(threeCal .MONTH),threeCal .get(threeCal.DAY_OF_MONTH),03,00,00);     
  • 最後為timmer收尾
    @Scheduled(cron = "0 0 */3 * * ?")
    public void timer() throws Exception {
          try{
            Calendar c = Calendar.getInstance(); 
            String sdTime = ReportUitls.format(TimeUtil.preDay(c.getTime()));
            String edTime = ReportUitls.format(TimeUtil.backDay(c.getTime()));

            Calendar threeCal = Calendar.getInstance();
            threeCal.set(threeCal.get(threeCal.YEAR),threeCal.get(threeCal.MONTH),threeCal.get(threeCal.DAY_OF_MONTH),03,00,00);   
            if(c.getTimeInMillis()<threeCal.getTimeInMillis()){
                c.add(Calendar.DATE, -1);
                sdTime = ReportUitls.format(TimeUtil.preDay(c.getTime()));
                edTime = ReportUitls.format(TimeUtil.backDay(c.getTime()));
            }             

          //在這處理service層的資料將sdTime和edTime傳入,處理當天一天的資料
          }catch (Exception e) {
            logger.debug(e.getMessage());
            System.out.println(e.getMessage());
        }   

    }