定时任务
在线Cron表达式生成器: https://cron.qqe2.com/
一、SpringBoot自带的定时任务
解决:使用异步任务 + 定时任务来完成定时任务不阻塞的功能
-  定时任务 - 1、@EnableScheduling 开启定时任务 (可直接加在springboot启动类上)
- 2、@Scheduled开启一个定时任务 (可直接加在需要定时任务的方法上,这个方法必须是注入容器中)
 
-  异步任务 - 1、@EnableAsync:开启异步任务 (可直接加在springboot启动类上)
- 2、@Async:给希望异步执行的方法标注(可直接加在需要定时任务的方法上,这个方法必须是注入容器中)
 
-  cron表达式特点 - 1、在Spring中表达式是6位组成,不允许第七位的年份
- 2、在周几的的位置,1-7代表周一到周日
 
-  特殊字符(,-*/LW#) -  ,:枚举 (cron=“7,9,23 * * * * ?”):任意时刻的 7,9,23 秒启动这个任务; 
-  -:范围 (cron=“7-20 * * * * ?”):任意时刻的 7-20 秒之间,每秒启动一次 
-  *:任意 指定位置的任意时刻都可以 
-  /:步长 (cron=“7/5 * * * * ?”):第 7 秒启动,每 5 秒一次; (cron="*/5 * * * * ?"):任意秒启动,每 5 秒一次; 
-  ?:(出现在日和周几的位置):为了防止日和周冲突,在周和日上如果要写通配符使用? (cron="* * * 1 * ?"):每月的 1 号,启动这个任务; 
-  L:(出现在日和周的位置), last:最后一个 (cron="* * * ? * 3L"):每月的最后一个周二 
-  W:Work Day:工作日 (cron="* * * W * ?"):每个月的工作日触发 (cron="* * * LW * ?"):每个月的最后一个工作日触发 
-  #:第几个 (cron="* * * ? * 5#2"):每个月的第 2 个周 4 
 
-  
-  代码示例: -  定时任务配置类 import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @EnableAsync //开启异步任务 @EnableScheduling //开启定时任务 @Configuration public class ScheduledConfig { }
-  业务执行带代码 import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class SeckillScheduled { // @Scheduled(cron = "*/5 * * * * ? ") @Scheduled(cron = "0 0 1/1 * * ? ") public void uploadSeckillSkuLatest3Days() { //调用service层方法 定时执行业务代码 } }
 
-  










