Spring的定時任務(任務調度)

原文:https://www.cnblogs.com/luchangyou/p/6856725.htmlhtml

Spring內部有一個task是Spring自帶的一個設定時間自動任務調度,提供了兩種方式進行配置,一種是註解的方式,而另一種就是XML配置方式了。註解方式比較簡潔,XML配置方式相對而言有些繁瑣,可是應用場景的不一樣,二者又各有優勢,因此具體使用仍是根據需求來劃分。由於任務調度這樣的需求,一般改動都是比較多的,若是用註解的方式改動就變得麻煩了,必須去從新編譯。因此更多的時候我選擇用XML配置的方式。java

下面就介紹一下兩種方式的配置:spring

第一種:XML配置方式測試

第一步:編寫做業類spa

即普通的pojo,以下:code

複製代碼
import org.springframework.stereotype.Service;  
@Service  
public class TaskJob {  
      
    public void job1() {  
        System.out.println(「任務進行中。。。」);  
    }  
} 
複製代碼

第二步:添加spring-task配置文件,相關頭信息以下:xml

複製代碼
<?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:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    <task:scheduled-tasks>

        <task:scheduled ref="taskJob" method="job1" cron="0 0 5 * * ?"/> </task:scheduled-tasks> 

</beans>
複製代碼

說明:ref參數指定任務類,method指定須要運行的方法,cron及cronExpression表達式見文章結尾。htm

 

第三步:在spring配置文件中引入spring-task配置文件blog

。。。。。
<!-- 引入Spring的任務配置文件。 -->
    <import resource="xxx.xml" />
。。。。。

這樣配置就完成了,能夠進行測試驗證了。文檔

 

第二種:使用註解形式

首先看下源文件中@Scheduled註解的定義

複製代碼
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface Scheduled  
{  
  public abstract String cron();  
  
  public abstract long fixedDelay();  
  
  public abstract long fixedRate();  
} 
複製代碼

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

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

複製代碼
import org.springframework.scheduling.annotation.Scheduled;    
import org.springframework.stereotype.Component;  
  
@Component(「taskJob」)  
public class TaskJob {  
    @Scheduled(cron = "0 0 3 * * ?")  
    public void job1() {  
        System.out.println(「任務進行中。。。」);  
    }  
} 
複製代碼

第二步:在spring配置文件開啓task:

<task:annotation-driven/>

這樣 就實現了註解方式的配置。

 

cron及cronExpression表達式:

 

可視化設置:http://cron.qqe2.com/

 

複製代碼
cronExpression的配置說明
字段   容許值   容許的特殊字符
秒    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 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"    每一個周1、周2、周3、周4、週五的10:15觸發 
複製代碼
相關文章
相關標籤/搜索