定时任务 @Scheduled
Application
加注解 @EnableScheduling -------可以支持定时任务
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling//可以支持定时任务
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(App.class, args);
}
}
TimerDemo
使用@Scheduled注解
- spring在初始化bean后,通过“postProcessAfterInitialization”拦截到所有的用到“@Scheduled”注解的方法,并解析相应的的注解参数,放入“定时任务列表”等待后续处理;之后再“定时任务列表”中统一执行相应的定时任务(任务为顺序执行,先执行cron,之后再执行fixedRate)
- @scheduled注解用来配置到方法上来完成对应的定时任务的配置,如执行时间,间隔时间,延迟时间等等
参考链接:@Scheduled执行原理解析 https://www.jianshu.com/p/92dfc36c2160
编写定时任务类和方法
- 定时任务类通过 Spring IOC 加载,使用 @Component 注解(当然也可以使用 @Controller 和 @Service 等其他与 @Component 作用相同的注解),定时方法使用 @Scheduled 注解。
参考链接:Spring Boot 定时任务 – @Scheduled https://www.jianshu.com/p/da710593e9be
package com.base.util;
import java.time.LocalDateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component //这个类会被springboot管理 万能
public class TimerDemo1 {
//在 Seconds 域使用 10,15,25 表示在第 10 秒、15 秒和 25 秒触发一次
@Scheduled(cron = "10,15,25 * * * * ?")
public void scheduledTask() {
System.out.println("Task executed at " + LocalDateTime.now());
}
//每个月的30号凌晨执行
@Scheduled(cron = "0 0 0 30 * ?")
public void scheduledTask2() {
System.out.println("Task executed at " + LocalDateTime.now());
}
}
cron属性
- Cron(计划任务)表达式广泛应用于各种定时解决方案
参考链接:Cron 表达式 https://www.jianshu.com/p/73784dff0b0e
参考链接 :spring-boot之@Scheduled https://blog.csdn.net/baidu_28610773/article/details/88384234
cron表达式各占位符解释: https://cloud.tencent.com/developer/article/1326452
语法格式:Cron 字符串包含 6 或 7 个域,域之间使用空格分隔
- Seconds Minutes Hours DayofMonth Month DayofWeek
- Seconds Minutes Hours DayofMonth Month DayofWeek Year
- {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}
类型 | 有效范围(数字形式) | 字符形式 |
---|---|---|
Seconds | 0~59 的整数 | , - * / 四个字符 |
Minutes | 0~59 的整数 | , - * / 四个字符 |
Hours | 0~23 的整数 | , - * / 四个字符 |
DayofMonth | 0~31 的整数 | , - * / ? L W C 八个字符 |
Month | 1~12 的整数 或 JAN ~ DEC | , - * / 四个字符 |
DayofWeek | 1~7的整数 (1表示星期天)或 SUN ~ SAT | , - * / ? L C # 八个字符 |
Year | 1970-2099年 | , - * / 四个字符 |
特殊字符说明
字符 | 说明 | 举例 |
---|---|---|
, | 用于分隔枚举值 | @Scheduled(cron = “10,15,25 * * * * ?”) -------表示在第 10 秒、15 秒和 25 秒触发一次 |
- | 用于表示范围 | @Scheduled(cron = “30-45 * * * * ?”)----表示在第 30 秒至 45 秒范围内,每秒触发一次 |
* | 匹配所在域有效范围内的任意值 | @Scheduled(cron = “0 * * * * ?”) ---------------表示在 Minutes 域使用则每分钟触发一次 |
/ | 表示从起始时间开始,每隔固定时间触发一次 | @Scheduled(cron = “10/15 * * * * ?”) 表示每分钟内第 10 秒触发以后,后续每隔 15 秒触发一次 注意:这种间隔循环只在每分钟内执行,即第 55 秒时中断每 15 秒的间隔循环,等到下一分钟的第 10 秒再次执行 |
? | 表示匹配任意值 只能用于 DayofMonth 和 DayofWeek 这两个域 | @Scheduled(cron = “* * * 10 * ?”) -----------DayofMonth 域使用固定值 10(表示每月第 10 天触发),则 DayofWeek 使用 ? |
fixedRate属性
该属性的含义是上一个调用开始后再次调用的延时(不用等待上一次调用完成),这样就会存在重复执行的问题,所以不是建议使用,但数据量如果不大时在配置的间隔时间内可以执行完也是可以使用的。
@Scheduled(fixedRate = 3000)
public void scheduledTask() {
System.out.println("Task executed at " + LocalDateTime.now());
}
- fixedRate 是 long 类型,表示任务执行的间隔毫秒数,以上代码中的定时任务每 3 秒执行一次。
其他属性参考
参考链接 :spring-boot之@Scheduled https://blog.csdn.net/baidu_28610773/article/details/88384234
参考链接:Spring Boot 定时任务 – @Scheduled https://www.jianshu.com/p/da710593e9be