基於Spring Task的定時任務調度器實現


在很多時候,我們會需要執行一些定時任務 ,Spring團隊提供了Spring Task模塊對定時任務的調度提供了支持,基於注解式的任務使用也非常方便。

只要跟需要定時執行的方法加上類似 @Scheduled(cron = "0 1 * *  *  *") 的注解就可以實現方法的定時執行。

cron 是一種周期的表達式,六位從右至左分別對應的是年、月、日、時、分、秒,數字配合各種通配符可以表達種類豐富的定時執行周期。

/**
* Cron Example patterns:
* <li>"0 0 * * * *" = the top of every hour of every day.</li>
* <li>"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.</li>
* <li>"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.</li>
* <li>"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays</li>
* <li>"0 0 0 25 12 ?" = every Christmas Day at midnight</li>
*/

基於注解的使用案列:

import org.springframework.stereotype.Component; 
   
@Component(“task”) 
public class Task { 
    @Scheduled(cron = "0 1 * * * *")  // 每分鍾執行一次
    public void job1() { 
        System.out.println(“任務進行中。。。”); 
    } 
}

 

基於注解方式的定時任務,啟動會依賴於系統的啟動。如果需要通過代碼或前台操作觸發定時任務,就需要進行包裝了。

下面是一個可以直接提供業務代碼調用的定時任務調度器。調用 schedule(Runnable task, String cron) 傳入要執行的任務

task和定時周期cron就可以了。注:基於注解方式需要在注解掃描范圍內。

package com.louis.merak.schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
 
@Component
public class MerakTaskScheduler {
     
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
     
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
        return new ThreadPoolTaskScheduler();
    }
     
    /**
     * Cron Example patterns:
     * <li>"0 0 * * * *" = the top of every hour of every day.</li>
     * <li>"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.</li>
     * <li>"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.</li>
     * <li>"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays</li>
     * <li>"0 0 0 25 12 ?" = every Christmas Day at midnight</li>
     */
    public void schedule(Runnable task, String cron){
        if(cron == null || "".equals(cron)) {
            cron = "0 * * * * *";
        }
        threadPoolTaskScheduler.schedule(task, new CronTrigger(cron));
    }
     
    /**
     * shutdown and init
     * @param task
     * @param cron
     */
    public void reset(){
        threadPoolTaskScheduler.shutdown();
        threadPoolTaskScheduler.initialize();
    }
    
    /**
     * shutdown before a new schedule operation
     * @param task
     * @param cron
     */
    public void resetSchedule(Runnable task, String cron){
        shutdown();
        threadPoolTaskScheduler.initialize();
        schedule(task, cron);
    }
    
    /**
     * shutdown
     */
    public void shutdown(){
        threadPoolTaskScheduler.shutdown();
    }
}

 

如果是需要通過前台操作調用RESTful執行定時任務的調度,使用以下Controller即可。

package com.louis.merak.common.schedule;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MerakTaskSchedulerController {
    
    @Autowired
    MerakTaskScheduler taskScheduler;
    
    @RequestMapping("/schedule")
    public String schedule(@RequestParam String cron) {
     if(cron == null) {
       cron =
"0/5 * * * * *";
}
 Runnable runnable = new Runnable() { public void run() { String time = new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new Date()); System.out.println("Test GETaskScheduler Success at "  + time); } };
taskScheduler.schedule(runnable, cron
); return "Test TaskScheduler Interface."; } }

 


作者:朝雨憶輕塵
出處:https://www.cnblogs.com/xifengxiaoma/ 
版權所有,歡迎轉載,轉載請注明原文作者及出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM