(六) Redis 實現Schedule 定時任務

Schedule(定時任務)

⼀張圖來說明(任務執⾏⻓度超過週期的情況):

在這裏插入圖片描述

​ 雖然定時任務可以嵌⼊到web應⽤程序和WAR⽂件中,但下⾯演⽰⼀種更簡單的⽅法創建了⼀個獨⽴的應⽤程序。您將所有的內容打包在⼀個單⼀的、可執⾏的JAR⽂件中,⽤⼀個傳統Java main()⽅法驅動。這也就是springboot的啓動類。

一. 入門案例

1.1 主程序

package com.qianfeng.test.task;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Application {
	public static void main(String[] args) throws Exception {
		SpringApplication.run(Application.class);
	}
}

1.2 任務類代碼

@Component
public class MyScheduled {
    
	/**
     *  fixedRate: 固定時間去執行。
     *  fixedDelay: 固定的延遲。
     *
     *  cron有六個站位符號: 第一個表示秒,第二個是分,第三個小時,第四是日,
     *   第五個是月份,第六個是星期
     *   0/2 * * * * *  每兩秒執行一次
     *   0 25 11 * * * 每天11:25執行
     *   0 0/1 11 * * *  每天的11點,每隔5分鐘執行一次
     *   0 0 20 * * *  每天晚上8點鐘執行
     *   0 0 8,20 * * * 每天早晚8點執行一次
     *   0 0 8-20 * * * 每天早上8點到晚8點,每個小時執行一次
     *   0 0 12 L * * * 每個月的最後一天12鍾執行。
     */
	@Scheduled(fixedRate = 5000)
	public void test1() {
		System.out.println("定時任務觸發了");
	}
}

二. 參數介紹

fixedRate

​ fixedRate表⽰從調⽤開始每次延遲多少毫秒繼續調⽤ ⽤法@Scheduled(fixedRate=5000),5000的單位是毫秒,也就是間隔時間是5秒。

fixedDelay

​ fixedDelay表⽰從調⽤開始延時多少毫秒繼續調⽤下⼀個週期 ⽤法@Scheduled(fixedDelay=5000),5000的單位是毫秒,也就是間隔時間是5秒。

initialDelay
​ fixedDelay表⽰在第⼀次執⾏fixedRate()或fixedDelay()任務之前延遲的毫秒數。 ⽤@Scheduled(fixedDelay=5000, initialDelay=10000),單位是毫秒,表⽰第⼀次執⾏fixedDelay()任務之前先延遲
10秒。

@Scheduled(cron=”0 0 * * * *”)

​ cron表達式相⽐於前⼏個是⽐較複雜的。 該模式是6個單獨的空間分隔字段的列表:表示秒、分鐘、
小時、日、月、星期。

"0 0 * * * *" = 每天每時整點
"*/10 * * * * *" = 每⼗秒(10:20:00, 10:20:10, 10:20:20 ...)觸發
"0 0 8-10 * * *" = 每天早上8:00、9:00 和 10:00 觸發
"0 0 6,19 * * *" = 每天6:00 和 19:00 觸發
"0 0/30 8-10 * * *" = 每天8:00, 8:30, 9:00, 9:30, 10:00 和 10:30 觸發
"0 0 9-17 * * MON-FRI" = 朝九晚五(周⼀⾄週五9:00-17:00的整點)觸發
"0 0 0 25 12 ?" = 聖誕節(每年的12⽉25⽇00:00)觸發
"0 15 10 L * ?" = 每⽉最後⼀⽇的上午10:15觸發
"0 15 10 ? * 6L" = 每⽉的最後⼀個星期五上午10:15觸發
"0 15 10 ? * 6#3" = 每⽉的第三個星期五上午10:15觸發 1234567891011

三. 並⾏執⾏任務

​ 上⾯的方式中,如果我們開啓多個定時任務,他們會使⽤同⼀個線程開啓任務,可能會因爲某個任務阻塞而導致
執⾏失敗,所以我們可以使⽤多線程並⾏執⾏任務,只需要添加⼀個線程池即可

3.1 並行類

@Component
public class MySheduleConfig implements SchedulingConfigurer {


    public Executor getExecutor() {
        return Executors.newScheduledThreadPool(3); //開啓特定的任務線程,開啓3個
    }

    //
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(getExecutor());
    }
}

3.2 任務類

@Component
public class MySchedule {

    @Scheduled(cron = "0/5 * * * * *")
    public void doSomething() {
        System.out.println("每五秒執行的任務線程名:" + Thread.currentThread().getName());
        System.out.println("定時任務開始................");
    }

    @Scheduled(cron = "0/3 * * * * *")
    public void doSomething1() {
    	System.out.println("每3秒執行的任務線程名:" + Thread.currentThread().getName());
        System.out.println("定時任務每3秒執行一次次,任務開始執行...");
    }

    @Scheduled(cron = "0/10 * * * * *")
    public void doSomething2() {
        System.out.println("每10秒執行的任務線程名:" + Thread.currentThread().getName());
        System.out.println("定時任務每天早8點到晚8點,每20分鐘執行一次,開始");
    }

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