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());
        }   

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章