Spring Boot的定时任务
主要讨论几种在Spring Boot下常见的定时器使用。
1、基于注解(@Scheduled)
// An highlighted block
package com.example.dbandcache.scheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@EnableScheduling //1、开始定时任务
public class EasyScheduleTask {
//2、添加定时任务
@Scheduled(cron = "0/1 * * * * ?")
public void easyTask() {
System.out.println("任务开始时间:" + LocalDateTime.now().toLocalTime());
}
}
1.1 代码解析:
1. @Component
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。
2. @EnableScheduling
Spring使用@EnableScheduling开启任务调度功能。
3. @Scheduled(cron = "0/1 * * * * ?")
添加定时任务
4. cron表达式
Cron表达式是一个字符串,字符串以5或6个空格隔开,分成6或7个域,每一个域代表一个含义,比如:cron="0/1 * * * * ?"。其中各个域的定义如下:
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年份 1970-2099
其中它们的含义是:
1>. ,:表示列出枚举值值。例如:在分域使用5,20,则意味着在5和20分每分钟触发一次。
2>. -: 表示范围,在分域中使用5-10,表示5-10分钟每分钟触发一次。
3>. *: 表示匹配该域的任意值,假如在秒域中使用,表示每秒钟都会触发事件。
4>. /: 表示起始时间开始触发,然后每隔固定时间触发一次,例如在秒域使用0/10,则意味着10秒触发一次。
5>. ?:只能用在日期和星期两个域。它也匹配域的任意值,但实际不会。因为日期和星期会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。
6>. L:表示最后,只能出现在日期和星期域,如果在星期域使用5L,意味着在最后的一个星期四触发。
7>. W:表示有效工作日(周一到周五),只能出现在日期域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 日期使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份
8>. LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
9>. #:用于确定每个月第几个星期几,只能出现在日期域。例如在4#2,表示某月的第二个星期三。
举例:
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 * * ? 2019 2019 年的每天上午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分钟触发
2、 基于接口(SchedulingConfigurer)
由于基于注解(@Scheduled)方式,每次修改cron表达式时,都需要重启应用程序。因此提供了另一种基于接口(SchedulingConfigurer)方式的定时器任务。
2.1 建立数据表
用于存放cron表达式信息
2.2 编写接口查询Cron信息
package com.example.dbandcache.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface CronMapper {
@Select("select cron from cron limit 1")
public String getCron();
}
2.3 编写测试类DynamicScheduleTask实现SchedulingConfigurer接口
package com.example.dbandcache.scheduler;
import com.example.dbandcache.mapper.CronMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
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;
@Component
@Configuration
@EnableScheduling
public class DynamicScheduleTask implements SchedulingConfigurer {
@Autowired
private CronMapper cronMapper;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(
()-> System.out.println("执行动态定时任务:"+ LocalDateTime.now().toLocalTime())
,triggerContext -> {
String cron = cronMapper.getCron();
return new CronTrigger(cron).nextExecutionTime(triggerContext);
}
);
}
}
运行结果:
2.4 修改数据表cron表达式
运行结果:
注意: 如果在数据库修改时格式出现错误,则定时任务会停止,即使重新修改正确;此时只能重新启动项目才能恢复。