Spring-Task用法

Spring-Task

上節介紹了在spring 中使用Quartz,本文介紹Spring3.0以後自主開發的定時任務工具,spring task,可以將它比作一個輕量級的Quartz,而且使用起來很簡單,除spring相關的包外不需要額外的包,而且支持註解和配置文件兩種

形式,下面將分別介紹這兩種方式。


第一種:配置文件方式


第一步:編寫作業類

即普通的pojo,如下:

  1. import org.springframework.stereotype.Service;  
  2. @Service  
  3. public class TaskJob {  
  4.       
  5.     public void job1() {  
  6.         System.out.println(“任務進行中。。。”);  
  7.     }  
  8. }  

 第二步:在spring配置文件頭中添加命名空間及描述

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:task="http://www.springframework.org/schema/task"   
  3.     。。。。。。  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">  

 第三步:spring配置文件中設置具體的任務

  1.  <task:scheduled-tasks>   
  2.         <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   
  3. </task:scheduled-tasks>  
  4.   
  5. <context:component-scan base-package=" com.gy.mytask " />  

說明:ref參數指定的即任務類,method指定的即需要運行的方法,cron及cronExpression表達式,具體寫法這裏不介紹了,詳情見上篇文章附錄。

<context:component-scan base-package="com.gy.mytask" />這個配置不消多說了,spring掃描註解用的。

到這裏配置就完成了,是不是很簡單。


第二種:使用註解形式

也許我們不想每寫一個任務類還要在xml文件中配置下,我們可以使用註解@Scheduled,我們看看源文件中該註解的定義:

Java代碼  收藏代碼
  1. @Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. @Documented  
  4. public @interface Scheduled  
  5. {  
  6.   public abstract String cron();  
  7.   
  8.   public abstract long fixedDelay();  
  9.   
  10.   public abstract long fixedRate();  
  11. }  

 可以看出該註解有三個方法或者叫參數,分別表示的意思是:

cron:指定cron表達式

fixedDelay:官方文檔解釋:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示從上一個任務完成開始到下一個任務開始的間隔,單位是毫秒。

fixedRate:官方文檔解釋:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即從上一個任務開始到下一個任務開始的間隔,單位是毫秒。

 

下面我來配置一下。

第一步:編寫pojo

Java代碼  收藏代碼
  1. import org.springframework.scheduling.annotation.Scheduled;    
  2. import org.springframework.stereotype.Component;  
  3.   
  4. @Component(“taskJob”)  
  5. public class TaskJob {  
  6.     @Scheduled(cron = "0 0 3 * * ?")  
  7.     public void job1() {  
  8.         System.out.println(“任務進行中。。。”);  
  9.     }  
  10. }  

 第二步:添加task相關的配置:

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:task="http://www.springframework.org/schema/task"  
  7.     xsi:schemaLocation="  
  8.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  10.         http://www.springframework.org/schema/context   
  11. http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
  12.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  13.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"  
  14.     default-lazy-init="false">  
  15.   
  16.   
  17.     <context:annotation-config />  
  18.     <!—spring掃描註解的配置   -->  
  19.     <context:component-scan base-package="com.gy.mytask" />  
  20.       
  21. <!—開啓這個配置,spring才能識別@Scheduled註解   -->  
  22.     <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
  23.     <task:scheduler id="qbScheduler" pool-size="10"/>  

說明:理論上只需要加上<task:annotation-driven />這句配置就可以了,這些參數都不是必須的。

 

 Ok配置完畢,當然spring task還有很多參數,我就不一一解釋了,具體參考xsd文檔http://www.springframework.org/schema/task/spring-task-3.0.xsd。


附錄:

cronExpression的配置說明,具體使用以及參數請百度google

字段   允許值   允許的特殊字符

秒    0-59    , - * /

分    0-59    , - * /

小時    0-23    , - * /

日期    1-31    , - * ? / L W C

月份    1-12 或者 JAN-DEC    , - * /

星期    1-7 或者 SUN-SAT    , - * ? / L C #

年(可選)    留空, 1970-2099    , - * / 

- 區間  

* 通配符  

? 你不想設置那個字段


下面只例出幾個式子

 

CRON表達式    含義 

"* * * * * ?"  每秒執行一次

"0/3 * * * * ?" 每3秒執行一次

"0 0 12 * * ?"    每天中午十二點觸發 

"0 15 10 ? * *"    每天早上10:15觸發 

"0 15 10 * * ?"    每天早上10:15觸發 

"0 15 10 * * ? *"    每天早上10:15觸發 

"0 15 10 * * ? 2005"    2005年的每天早上10:15觸發 

"0 * 14 * * ?"    每天從下午2點開始到2點59分每分鐘一次觸發 

"0 0/5 14 * * ?"    每天從下午2點開始到2:55分結束每5分鐘一次觸發 

"0 0/5 14,18 * * ?"    每天的下午2點至2:55和6點至6點55分兩個時間段內每5分鐘一次觸發 

"0 0-5 14 * * ?"    每天14:00至14:05每分鐘一次觸發 

"0 10,44 14 ? 3 WED"    三月的每週三的14:10和14:44觸發 

"0 15 10 ? * MON-FRI"    每個週一、週二、週三、週四、週五的10:15觸發 

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