(六) 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分钟执行一次,开始");
    }

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