標籤:location config run system span 3.0 net import int
1. 定時任務實現方式
定時任務實現方式:
- Java內建的java.util.Timer類,這個類允許你調度一個java.util.TimerTask任務。使用這種方式可以讓你的程式按照某一個頻度執行,但不能在指定時間運行。一般用的較少,這篇文章將不做詳細介紹。
- 使用Quartz,這是一個功能比較強大的的調度器,可以讓你的程式在指定時間執行,也可以按照某一個頻度執行,配置起來稍顯複雜,有空介紹。
- SpringBoot內建的Scheduled,可以將它看成一個輕量級的Quartz,而且使用起來比Quartz簡單許多,本文主要介紹。
定時任務執行方式:
2. 建立定時任務
package com.autonavi.task.test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import com.autonavi.task.ScheduledTasks;@Componentpublic class ScheduledTest { private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class); @Scheduled(cron="0 0/2 8-20 * * ?") public void executeFileDownLoadTask() { // 間隔2分鐘,執行工單上傳任務 Thread current = Thread.currentThread(); System.out.println("定時任務1:"+current.getId()); logger.info("ScheduledTest.executeFileDownLoadTask 定時任務1:"+current.getId()+ ",name:"+current.getName()); } @Scheduled(cron="0 0/1 8-20 * * ?") public void executeUploadTask() { // 間隔1分鐘,執行工單上傳任務 Thread current = Thread.currentThread(); System.out.println("定時任務2:"+current.getId()); logger.info("ScheduledTest.executeUploadTask 定時任務2:"+current.getId() + ",name:"+current.getName()); } @Scheduled(cron="0 0/3 5-23 * * ?") public void executeUploadBackTask() { // 間隔3分鐘,執行工單上傳任務 Thread current = Thread.currentThread(); System.out.println("定時任務3:"+current.getId()); logger.info("ScheduledTest.executeUploadBackTask 定時任務3:"+current.getId()+ ",name:"+current.getName()); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
@Scheduled 註解用於標註這個方法是一個定時任務的方法,使用@Scheduled(cron=”…”) 運算式來設定定時任務。
// 每天早八點到晚八點,間隔2分鐘執行任務@Scheduled(cron="0 0/2 8-20 * * ?") // 每天早八點到晚八點,間隔3分鐘執行任務@Scheduled(cron="0 0/3 8-20 * * ?") // 每天早八點到晚八點,間隔1分鐘執行任務@Scheduled(cron="0 0/1 8-20 * * ?")
3. 啟動定時任務
@ComponentScan@EnableAutoConfiguration@EnableScheduling@Configurationpublic class App { private static final Logger logger = LoggerFactory.getLogger(App.class); public static void main(String[] args) { SpringApplication.run(App.class, args); logger.info("oops"); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
其中 @EnableScheduling 註解的作用是發現註解@Scheduled的任務並後台執行。
4. 執行結果
2016-02-14-14-51 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadBackTask 定時任務3:15,name:pool-2-thread-1 定時任務2:152016-02-14-14-51 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定時任務2:15,name:pool-2-thread-1 定時任務1:152016-02-14-14-52 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeFileDownLoadTask 定時任務1:15,name:pool-2-thread-1 定時任務2:152016-02-14-14-52 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定時任務2:15,name:pool-2-thread-1 定時任務2:152016-02-14-14-53 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定時任務2:15,name:pool-2-thread-1
5. 串列任務
上述方法可以實現定時任務,方式也比較簡單,不用配置什麼檔案啥的,但你會發現一個問題,就是不論定時任務被安排在多少個class類中,其依然是單線程執行定時任務(串列任務):
2016-02-14-15-05 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTasks.executeUploadTask 定時任務1:15,name:pool-2-thread-1 定時任務2:152016-02-14-15-06 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定時任務2:15,name:pool-2-thread-1
上述執行結果中ScheduledTest和ScheduledTasks是兩個獨立類,都有各自定時任務,但運行時起Thread Name都是一樣的pool-2-thread-1,因此每個定時任務若要新啟一個線程,需要自行編寫實現或者設定檔。
SpringBoot定時任務預設單線程,多線程需要自行實現或設定檔
6. 並行任務
有時候會碰到不同業務的定時任務,這時候利用並行任務處理要妥當,採用多線程任務。只需要配置SpringBoot的設定檔:applicationContext.xml,添加如下內容:
<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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- Enables the Spring Task @Scheduled programming model --> <task:executor id="executor" pool-size="5" /> <task:scheduler id="scheduler" pool-size="10" /> <task:annotation-driven executor="executor" scheduler="scheduler" /></beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
添加紅框中的內容
同時注意補充title中遺漏的網址。
效果如下,每個調度處理一個任務,每個調度也是一個子線程:
有關executor、scheduler參數的介紹見文中的34.5 The Task Namespace節。
7. 基於springboot的定時任務工程範例
demo工程
8. 動態定時任務說明
有時候需要實現動態定時任務,即工程啟動後,可以實現啟動和關閉任務,同時也可以設定定時計劃。這就需要利用到quartz,spring官方對於這個包下面各類的介紹,後續抽空配置下這類業務的實現:
http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/scheduling/quartz/package-summary.html。
quartz API:
http://www.quartz-scheduler.org/api/2.1.7/index.html
SpringBoot定時任務說明