SpringTask 基於註解方式的實現

  • SpringTask是一個集成在Spring框架中的輕量級的定時器功能。支持註解和配置文件兩種實現方式。本文介紹使用註解方式實現定時器。
  • SpringTask默認是單線程執行的,無論有多少方法都要排隊執行。 多任務並行執行需要設置’pool-size’參數。
  • 用到的jar包:
    -這裏寫圖片描述
    配置文件:spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
            xmlns:mvc="http://www.springframework.org/schema/mvc" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:p="http://www.springframework.org/schema/p"
            xmlns:aop="http://www.springframework.org/schema/aop" 
            xmlns:task="http://www.springframework.org/schema/task"  
            xmlns:context="http://www.springframework.org/schema/context" 
            xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <context:component-scan base-package="com.project.controller" /> 
    <task:annotation-driven/>
    <task:scheduler id="taskScheduler" pool-size="100" />
</beans>
  • 配置中component-scan是默認掃描的文件路徑,根據項目的實際情況修改base-package內容
  • java實現:
package com.project.controller.task;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class testTimeTask {
    @Scheduled(cron = "0 0/2 * * * ?")
    public void test1() throws ParseException {
        System.out.println("阻塞線程觸發");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date = sdf.parse("2018-02-26 12-58-00");
        long longDate = date.getTime();
        do{
        }while(longDate-System.currentTimeMillis()>0);
    }
    @Scheduled(cron = "0 0/1 * * * ?")
    public void test2() {
        Date dt = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒");  
        System.out.println("TEST2定時執行 :"+sdf.format(dt));
    }
    @Scheduled(cron = "0 0/1 * * * ?")
    public void test3() {
        Date dt = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒");  
        System.out.println("TEST3定時執行 :"+sdf.format(dt));
    }
}
  • @Scheduled 註解是定時器註解,cron 中的數據是定時器執行時間表達式,可參考quartz 表達式。
  • 輸出:
  • TEST2定時執行 :2018年02月26日 11時31分00秒
    TEST3定時執行 :2018年02月26日 11時31分00秒
    TEST3定時執行 :2018年02月26日 11時32分00秒
    TEST2定時執行 :2018年02月26日 11時32分00秒
    阻塞線程觸發

  • 曾經在對SpringTask實際運用時遇到了定時器阻塞的問題,究其原因是沒配置“pool-size”這個參數,這就導致假如一個項目有A、B、C三個定時任務,三個任務是順序執行的,A在執行時B、C需要等待,直到A任務完成。

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