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

在不少時候,咱們會須要執行一些定時任務 ,Spring團隊提供了Spring Task模塊對定時任務的調度提供了支持,基於註解式的任務使用也很是方便。java

只要跟須要定時執行的方法加上相似 @Scheduled(cron = "0 1 * *  *  *") 的註解就能夠實現方法的定時執行。web

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

/**
* 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>
*/app

基於註解的使用案列:ui

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

 

基於註解方式的定時任務,啓動會依賴於系統的啓動。若是須要經過代碼或前臺操做觸發定時任務,就須要進行包裝了。spa

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

task和定時週期cron就能夠了。注:基於註解方式須要在註解掃描範圍內。orm

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便可。blog

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/ 
版權全部,歡迎轉載,轉載請註明原文做者及出處。get

相關文章
相關標籤/搜索