Spring Task 定時任務
所謂定時任務。就是依據我們設定的時間定時運行任務,就像定時發郵件一樣,設定時間到了。郵件就會自己主動發送。
在Spring大行其道的今天,Spring也提供了其定時任務功能,Spring Task。同Spring的其它功能一樣,我們既能夠通過配置文件也能夠通過註解形式來實現。
一、通過配置文件
1、任務運行類
import org.springframework.stereotype.Service; @Service public class TaskTest{ public void Test(){ System.out.println(new Date() + "定時任務開始…"); } }
2、spring配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" <strong>xmlns:task="http://www.springframework.org/schema/task"</strong> xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd <strong>http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd</strong> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"> . . . <context:component-scan base-package=" com.hp.task" /> <task:scheduled-tasks> <task:scheduled ref="taskTest" method="test" cron="0/5 * * * * ?"/> </task:scheduled-tasks> </beans>
參數說明:ref參數是運行任務的類。method是類中須要運行的方法。cron運行表達式表示運行的時間及策略。
二、通過註解
1、任務運行類
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * 定時處理類 * * @author zjj * @date 2015-6-30 */ @Component public class TaskTest{ /** * 定時處理方法測試 * 每5秒一次 */ @Scheduled(cron = "0/5 * * * * ?") public void Test() { System.out.println(new Date() + "定時任務開始…"); } }
這裏須要兩個註解:
[email protected]:將該類完畢bean創建和自己主動依賴註入
[email protected]:將該方法定義為Spring定時調用的方法,當中cron指該方法運行的時間及策略
2、Spring配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"> <context:component-scan base-package=" com.hp.task" /> <!—開啟這個配置,[email protected] --> <task:scheduler id="scheduler" pool-size="10" /> <task:executor id="executor" pool-size="10" /> <task:annotation-driven scheduler="scheduler" executor="executor" /> </beans>
總結
兩種方式實現定時任務都是非常方便的,可是都存在同一個問題。定時策略在項目啟動後我們無法動態改動。要改動就須要重新啟動服務,如何做到定時策略動態設定也將是興許解決的問題。
Spring Task 定時任務