SpringBoot定時任務

轉載請註明來源:http://blog.csdn.net/loongshawn/article/details/50663393

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;

@Component
public 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());
    }    

}

@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
@Configuration
public 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");                        
    }   
}

其中 @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:15
2016-02-14-14-51 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定時任務2:15,name:pool-2-thread-1
     定時任務1:15
2016-02-14-14-52 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeFileDownLoadTask 定時任務1:15,name:pool-2-thread-1
     定時任務2:15
2016-02-14-14-52 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定時任務2:15,name:pool-2-thread-1
     定時任務2:15
2016-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:15
2016-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>

添加紅框中的內容 


同時注意補充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
--------------------- 

原文:https://blog.csdn.net/loongshawn/article/details/50663393 
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

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