文章目录
1.主启动类(@EnableScheduling)
@SpringBootApplication
@EnableScheduling // 开启定时任务功能
@EnableAsync // 开启多线程定时任务
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2.定时任务实现方式
1.注解(@Scheduled)
package com.qin.test;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @desciption: 注解实现定时任务
* @author: wangzhibin
* @date: 2020/10/16 10:41
* @version: V1.0.0
* @className: ScheduledTest
*/
@Component
public class ScheduledTest1 {
// ${cron}: 从配置文件读取cron表达式
@Scheduled(cron = "${cron}")
public void test(){
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}
2.接口(实现SchedulingConfigurer)
package com.qin.test;
import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* @desciption: 接口实现定时任务
* @author: wangzhibin
* @date: 2020/10/16 10:41
* @version: V1.0.0
* @className: ScheduledTest
*/
@Component
public class ScheduledTest2 implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(
//1.添加任务内容(Runnable)
new Runnable() {
@Override
public void run() {
System.out.println("执行动态定时任务: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
},
//2.设置执行周期(Trigger)
new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
//2.1 从数据库动态获取cron表达式/或者根据业务需求从其它地方获取
String cron = "*/5 * * * * ?";
//2.2 校验cron表达式是否合法.
if (StringUtils.isEmpty(cron)) {
// Omitted Code ..
}
//2.3 返回执行周期(Date)
return new CronTrigger(cron).nextExecutionTime(triggerContext);
}
}
);
}
}
3.多线程(@Async:可以标注在类和方法上)
package com.qin.test;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @desciption: 多线程实现定时任务
* @author: wangzhibin
* @date: 2020/10/20 15:41
* @version: V1.0.0
* @className: ScheduledTest3
*/
@Component
@Async
public class ScheduledTest3 {
// ${cron}: 从配置文件读取cron表达式
@Scheduled(cron = "*/3 * * * * ?")
@Async
public void test(){
System.out.println("第一个任务:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
@Scheduled(cron = "*/1 * * * * ?")
public void test2(){
System.out.println("第二个任务:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}
3.cron表达式
在线工具:https://qqe2.com/cron/index
1.表格
位置 | 时间单位 | 值(数字或字母) | 值(特殊字符) |
---|---|---|---|
1 | 秒 | 0-59 | , - * / |
2 | 分钟 | 0-59 | , - * / |
3 | 小时 | 0-23 | , - * / |
4 | 日 | 1-31 | , - * / L W C |
5 | 月 | 1-12 | , - * / |
6 | 星期 | 1-7 (注意1是星期天)SUN,MON,TUE,WED,THU,FRI,SAT | , - * / ? L W C |
7 | 年(可选) | 1970-2099 | , - * / |
2.特殊字符说明
- 星号(*):可用在所有字段中,表示对应时间域的每一个时刻,例如,*在分钟字段时,表示“每分钟”;
- 问号(?):该字符只在日期和星期字段中使用,它通常指定为“无意义的值”,相当于占位符;
- 减号(-):表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12;
- 逗号(,):表达一个列表值,如在星期字段中使用“MON,WED,FRI”,则表示星期一,星期三和星期五;
- 斜杠(/):x/y表达一个等步长序列,x为起始值,y为增量步长值。如在秒数字段中使用0/15,则表示为0,15,30和45秒,而5/15在分钟字段中表示5,20,35,50,你也可以使用*/y,它等同于0/y;
- 井号(#):该字符只能在星期字段中使用,表示当月某个工作日。如6#3表示当月的第三个星期五(6表示星期五,#3表示当前的第三个),而4#5表示当月的第五个星期三,假设当月没有第五个星期三,忽略不触发;
- L:该字符只在日期和星期字段中使用,代表“Last”的意思,但它在两个字段中意思不同。L在日期字段中,表示这个月份的最后一天,如一月的31号,非闰年二月的28号;如果L用在星期中,则表示星期六,等同于7。但是,如果L出现在星期字段里,而且在前面有一个数值X,则表示“这个月的最后X天”,例如,6L表示该月的最后星期五;
- W:该字符只能出现在日期字段里,是对前导日期的修饰,表示离该日期最近的工作日。例如15W表示离该月15号最近的工作日,如果该月15号是星期六,则匹配14号星期五;如果15日是星期日,则匹配16号星期一;如果15号是星期二,那结果就是15号星期二。但必须注意关联的匹配日期不能够跨月,如你指定1W,如果1号是星期六,结果匹配的是3号星期一,而非上个月最后的那天。W字符串只能指定单一日期,而不能指定日期范围;
- LW组合:在日期字段可以组合使用LW,它的意思是当月的最后一个工作日;
- C:该字符只在日期和星期字段中使用,代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。例如5C在日期字段中就相当于日历5日以后的第一天。1C在星期字段中相当于星期日后的第一天。
3.示例
- “*/5 * * ?” 每隔5秒执行一次:
- ”0 */1 * ?“ 每隔1分钟执行一次:
- 0 0 10,14,16 ? 每天上午10点,下午2点,4点
- “0 0/30 9-17 ?” 朝九晚五工作时间内每半小时
- “0 0 12 ? * WED” 表示每个星期三中午12点
- “0 0 12 ?” 每天中午12点触发
- “0 15 10 ? “ 每天上午10:15触发
- “0 15 10 ?” 每天上午10:15触发
- “0 15 10 ? *” 每天上午10:15触发
- “0 15 10 ? 2005” 2005年的每天上午10:15触发
- “0 *14 ** ?” 在每天下午2点到下午2:59期间的每1分钟触发
- “0 0/5 14 ?” 在每天下午2点到下午2:55期间的每5分钟触发
- “0 0/5 14,18 ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
- “0 0-5 14 ?” 在每天下午2点到下午2:05期间的每1分钟触发
- “0 10,44 14 ? 3 WED” 每年三月的星期三的下午2:10和2:44触发
- “0 15 10 ? * MON-FRI” 周一至周五的上午10:15触发
- “0 15 10 15 * ?” 每月15日上午10:15触发
- “0 15 10 L * ?” 每月最后一日的上午10:15触发
- “0 15 10 ? * 6L” 每月的最后一个星期五上午10:15触发
- “0 15 10 ? * 6L 2002-2005” 2002年至2005年的每月的最后一个星期五上午10:15触发
- “0 15 10 ? * 6#3” 每月的第三个星期五上午10:15触发