SpringBoot中集成任务调度

SpringBoot中集成任务调度

1. 任务调度基本介绍

任务调度器就是按照规定的计划完成任务;
	比如windows,linux的自带的任务调度系统功能;
	平常开发中也就是按照规定的时间点轮询执行计划任务
	(比如每周三的凌晨进行数据备份),
	或者按时间隔触发一次任务调度(比如每3小时执行一次定时抓拍);

2. corn表达式介绍

在线Cron表达式生成器

2-1 corn的每一个位置功能介绍

在这里插入图片描述

2-2 占位符说明

在这里插入图片描述

2-3 常用cron举例

0 0 3 * * ?	每月每天凌晨3点触发
0 0 3 1 * ?	每月1日凌晨3点触发
0 0 3* WEN	星期三中午12点触发
0 0 3* MON-FRI	周一至周五凌晨3点触发
0 0/5 8 * * ?	每天7点至7:55分每隔5分钟触发一次
0 10,20 8 * * ?	每天的810分,820分触发
0 0 1-3 * * ?	每天的1点至三点每小时触发一次
0 0 8 L * ?	每月最后一天的8点触发
0 10 12* 6#3	每月的第三个星期五的12:10分触发
0 10 12* 6L 2022	表示2022年每月最后一个星期五10:22分触发

// 其他一些常用Cron表达式参考10/2 * * * * ?   表示每2秒 执行任务
  (10 0/2 * * * ?    表示每2分钟 执行任务
  (10 0 2 1 * ?   表示在每月的1日的凌晨2点调整任务
  (20 15 10 ? * MON-FRI   表示周一到周五每天上午10:15执行作业
  (30 15 10 ? 6L 2002-2006   表示2002-2006年的每个月的最后一个星期五上午10:15执行作
  (40 0 10,14,16 * * ?   每天上午10点,下午2点,4点 
  (50 0/30 9-17 * * ?   朝九晚五工作时间内每半小时 
  (60 0 12 ? * WED    表示每个星期三中午12点 
  (70 0 12 * * ?   每天中午12点触发 
  (80 15 10 ? * *    每天上午10:15触发 
  (90 15 10 * * ?     每天上午10:15触发 
  (100 15 10 * * ?    每天上午10:15触发 
  (110 15 10 * * ? 2005    2005年的每天上午10:15触发 
  (120 * 14 * * ?     在每天下午2点到下午2:59期间的每1分钟触发 
  (130 0/5 14 * * ?    在每天下午2点到下午2:55期间的每5分钟触发 
  (140 0/5 14,18 * * ?     在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
  (150 0-5 14 * * ?    在每天下午2点到下午2:05期间的每1分钟触发 
  (160 10,44 14 ? 3 WED    每年三月的星期三的下午2:102:44触发 
  (170 15 10 ? * MON-FRI    周一至周五的上午10:15触发 
  (180 15 10 15 * ?    每月15日上午10:15触发 
  (190 15 10 L * ?    每月最后一日的上午10:15触发 
  (200 15 10 ? * 6L    每月的最后一个星期五上午10:15触发 
  (210 15 10 ? * 6L 2002-2005   2002年至2005年的每月的最后一个星期五上午10:15触发 
  (220 15 10 ? * 6#3   每月的第三个星期五上午10:15触发

3. SpringBoot项目中,集成任务调度@Scheduled

3-1 添加SpringBoot启动依赖

<dependencies>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

3-2 具体corn任务调度计划

@Service
public class PlainService {
    
    
    @Scheduled(cron = "30 * * * * ?")
    public void cronScheduled(){
    
    
        System.out.println("测试任务调度内容打印。。。");
    }
}

3-3 SpringBoot启动类添加注解@EnableScheduling

@SpringBootApplication
// 开启任务调度
@EnableScheduling
public class ScheduledApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ScheduledApplication.class,args);
    }
}

4 @Scheduled 其它方式执行任务调度

4-1 fixedDelay 必须是上次调度成功

	// 每隔3000毫秒执行一次,必须是上次调度成功后3000毫秒;
   @Scheduled(fixedDelay = 3000)
    public void fixedDelayScheduled(){
    
    
        System.out.println("the day nice");
    }

4-2 fixedRate 无论上次是否会执行成功,下次都会执行

	// 每隔3000毫秒执行一次,无论上次是否会执行成功,下次都会执行;
    @Scheduled(initialDelay = 1000,fixedRate = 3000)
    public void initialDelayStringScheduled(){
    
    
        System.out.println("the night nice");
    }

4-3 initialDelay 表示初始化延迟 X 毫秒后,执行具体的任务调度

	// 表示初始化延迟1000毫秒后,执行具体的任务调度,之后按照fixedRate进行任务调度;
    @Scheduled(initialDelay = 1000,fixedRate = 3000)
    public void initialDelayStringScheduled(){
    
    
        System.out.println("the night nice");
    }

5 SpringBoot中@Scheduled读取配置文件获取cron值

5-1 说明

# 说明:
1、在类上加注解@Component,交给Spring管理
2、在@PropertySource指定配置文件名称,如下配置,
		指定放在src/main/resource目录下的application.properties文件
3、配置文件application.properties参考内容如下

5-2 Yml中配置cron表达式的值

如在application.yml中配置参数cronVal
#每秒钟执行一次, 这个cronVal可以随便自定义,保持代码中用到时一致即可
cronVal=0/1 * * * * *

5-3 Java代码中使用

import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component	// 交给Spring管理
@PropertySource("classpath:/application.yml")	// 指定配置文件名称
public class ScoreTask1 {
    
    
	
	@Scheduled(cron="${cronVal}")
	public void scoreTask(){
    
    
		System.out.println("从配置文件读取cron表达式定时器");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_17847881/article/details/130334737