天天看點

SpringTask 基于注解方式的實作定時器

  • SpringTask是一個內建在Spring架構中的輕量級的定時器功能。支援注解和配置檔案兩種實作方式。本文介紹使用注解方式實作定時器。
  • SpringTask預設是單線程執行的,無論有多少方法都要排隊執行。 多任務并行執行需要設定’pool-size’參數。
  • 用到的jar包:

    -

    SpringTask 基于注解方式的實作定時器
    配置檔案: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任務完成。