定时任务是后端开发中常见的需求,主要应用场景有按期数据报表、定时消息通知、异步的后台业务逻辑处理、日志分析处理、垃圾数据清理、定时更新缓存等等。java
Spring Boot 集成了一整套的定时任务工具,让咱们专一于完成逻辑,剩下的基础调度工做将自动完成。git
实现方式 | 描述 |
---|---|
java.util.Timer | Timer 提供了一个 java.util.TimerTask 任务支持任务调度。该方式只能按指定频率执行,不能在指定时间运行。因为功能过于单一,使用较少。 |
Quartz | Quartz 是一个功能比较强大的调度器,支持在指定时间运行,也能够按照指定频率执行。缺点是使用起来相对麻烦。 |
Spring 框架自带的 Schedule 模块 | 能够看作是轻量级的 Quartz |
@Component
@EnableScheduling
public class StaticScheduleJob {
/**
* 上次开始执行时间点后5秒再次执行
*/
@Scheduled(fixedRate = 5000)
public void job3() {
System.out.println("执行任务job3:"+DateUtil.formatDateTime(new Date()));
}
/**
* 上次执行完毕时间点后3秒再次执行
*/
@Scheduled(fixedDelay = 3000)
public void job2() {
System.out.println("执行任务job2:"+DateUtil.formatDateTime(new Date()));
}
/**
* 每隔10秒执行一次(按照 corn 表达式规则执行)
*/
@Scheduled(cron = "0/10 * * * * ?")
public void job1() {
System.out.println("执行任务job1:"+DateUtil.formatDateTime(new Date()));
}
}
复制代码
@EnableScheduling 注解启用定时调动功能github
@Scheduled 参数说明:spring
{秒} {分} {时} {日} {月} {周} {年(可选)}数据库
字段 | 取值范围 | 容许的特殊字符 |
---|---|---|
Seconds(秒) | 0 ~ 59 | , - * / |
Minutes(分) | 0 ~ 59 | , - * / |
Hours(时) | 0 ~ 23 | , - * / |
Day-of-Month(天) | 能够用数字 1 ~ 31 中的任意一个值,但要注意特殊月份 | , - * ? / L W C |
Month(月) | 能够用 0 ~ 11 或者字符串 “JAN、FEB、MAR、APR、MAY、JUN、JUL、AUG、SEP、OCT、NOV、DEC” 表示 | , - * / |
Day-of-Week(每周) | 能够用数字 1 ~ 7 表示(1=星期日)或者用字符串 “SUN、MON、TUE、WED、THU、FRI、SAT” 表示 | , - * ? / L C # |
Year(年) | 取值范围(1970-2099),容许为空值 | , - * / |
特殊字符 | 说明 |
---|---|
* | 表示能够匹配该域的全部值 |
? | 主要用于日和星期,能够匹配域的任意值,但实际不会。当2个子表达式其中之一被指定了值之后,为了不冲突,须要将另外一个子表达式的值设为? |
/ | 表示为每隔一段时间。如 0 0/10 * * * ? 其中的 0/10表示从0分钟开始,每隔10分钟执行一次 |
- | 表示范围。如 0 0-5 14 * * ? 表示在天天14:00到14:05期间每1分钟执行一次 |
, | 表示枚举多个值,这些值之间是"或"的关系。如 0 10,30 14 * 3 ? 表示每一个星期二14点10分或者14点30分执行一次 |
L | 表示每个月或者每周的最后一天。如 0 0 0 L * ? * 表示每个月的最后一天执行 |
W | 表示最近工做日。如 0 0 0 15W * ?* 表示每个月15号最近的那个工做日执行 |
# | 用来指定具体的周数,"#"前面表明星期,"#"后面表明本月的第几周。如"2#1"表示本月第二周的星期日 |
www.bejson.com/othertools/…json
@Configuration
public class CustomScheduleConfig implements SchedulingConfigurer {
@Autowired
private CronTriggerDao cronTriggerDao;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// 上次开始执行时间点后5秒再执行
taskRegistrar.addFixedRateTask(() -> System.out.println("CustomScheduleConfig执行任务job1=>"
+ DateUtil.formatDateTime(new Date()) + ",线程:" + Thread.currentThread().getName()), 5000);
// 上次执行完毕时间点后3秒再执行
taskRegistrar.addFixedDelayTask(() -> System.out.println("CustomScheduleConfig执行任务job2=>"
+ DateUtil.formatDateTime(new Date()) + ",线程:" + Thread.currentThread().getName()), 3000);
// 添加一个配合数据库动态执行的定时任务
TriggerTask triggerTask = new TriggerTask(
// 任务内容.拉姆达表达式
() -> {
// 1)添加任务 Runnable
System.out.println("CustomScheduleConfig执行动态任务job3=>" + DateUtil.formatDateTime(new Date()) + ",线程:"
+ Thread.currentThread().getName());
// 2)设置执行周期
}, triggerContext -> {
// 3)从数据库获取执行周期
String cron = cronTriggerDao.getCronTriggerById(1L);
if (cron != null) {
// 4)返回执行周期(Date)
return new CronTrigger(cron).nextExecutionTime(triggerContext);
} else {
return null;
}
});
taskRegistrar.addTriggerTask(triggerTask);
}
}
复制代码
这边为了测试简单初始化两行数据:后端
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_cron_trigger
-- ----------------------------
DROP TABLE IF EXISTS `t_cron_trigger`;
CREATE TABLE `t_cron_trigger` (
`id` int(8) NOT NULL AUTO_INCREMENT COMMENT '任务id',
`cron` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT 'cron表达式',
`create_time` datetime DEFAULT NULL COMMENT '建立时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`is_deleted` int(1) DEFAULT '0' COMMENT '做废状态 0-否 1-是',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ----------------------------
-- Records of t_cron_trigger
-- ----------------------------
BEGIN;
INSERT INTO `t_cron_trigger` VALUES (1, '0/10 * * * * ?', '2019-10-30 13:40:14', NULL, 0);
INSERT INTO `t_cron_trigger` VALUES (2, '0/7 * * * * ?', '2019-10-30 13:40:34', NULL, 0);
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
复制代码
CronTrigger 实体类缓存
public class CronTrigger implements Serializable{
/**
*
*/
private static final long serialVersionUID = 880141459783509786L;
private Long id;//任务id
private String cron;//cron表达式
private String createTime;//建立时间
private String updateTime;//更新时间
private boolean isDeleted=false;//删除状态
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCron() {
return cron;
}
public void setCron(String cron) {
this.cron = cron;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean isDeleted) {
this.isDeleted = isDeleted;
}
}
复制代码
CronTriggerDaospringboot
public interface CronTriggerDao {
/**
* 根据id获取cron表达式
* @param id
* @return
*/
String getCronTriggerById(Long id);
}
复制代码
CronTriggerMapper.xmlbash
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.zwqh.springboot.dao.CronTriggerDao">
<resultMap type="cn.zwqh.springboot.model.CronTrigger" id="cronTrigger">
<id property="id" column="id"/>
<result property="cron" column="cron"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
<result property="isDeleted" column="is_deleted"/>
</resultMap>
<!-- 根据id获取cron表达式 -->
<select id="getCronTriggerById" resultType="String">
select cron from t_cron_trigger where is_deleted=0 and id=#{id}
</select>
</mapper>
复制代码
启动前记得在启动类上加上 @EnableScheduling
打印日志以下:
经过上面的日志咱们能够看到任务执行都是单线程的。若是要实现多线程执行任务,咱们能够经过在 SchedulingConfigurer 接口的 configureTasks方法中添加线程池便可。
@Configuration
public class CustomScheduleConfig implements SchedulingConfigurer {
@Autowired
private CronTriggerDao cronTriggerDao;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//设定一个长度为10的定时任务线程池
taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
// 上次开始执行时间点后5秒再执行
taskRegistrar.addFixedRateTask(() -> System.out.println("CustomScheduleConfig执行任务job1=>"
+ DateUtil.formatDateTime(new Date()) + ",线程:" + Thread.currentThread().getName()), 5000);
// 上次执行完毕时间点后3秒再执行
taskRegistrar.addFixedDelayTask(() -> System.out.println("CustomScheduleConfig执行任务job2=>"
+ DateUtil.formatDateTime(new Date()) + ",线程:" + Thread.currentThread().getName()), 3000);
// 添加第一个配合数据库动态执行的定时任务
TriggerTask triggerTask = new TriggerTask(
// 任务内容.拉姆达表达式
() -> {
// 1)添加任务 Runnable
System.out.println("CustomScheduleConfig执行动态任务job3=>" + DateUtil.formatDateTime(new Date()) + ",线程:"
+ Thread.currentThread().getName());
// 2)设置执行周期
}, triggerContext -> {
// 3)从数据库获取执行周期
String cron = cronTriggerDao.getCronTriggerById(1L);
if (cron != null) {
// 4)返回执行周期(Date)
return new CronTrigger(cron).nextExecutionTime(triggerContext);
} else {
return null;
}
});
taskRegistrar.addTriggerTask(triggerTask);
// 添加第二个配合数据库动态执行的定时任务
TriggerTask triggerTask2 = new TriggerTask(
// 任务内容.拉姆达表达式
() -> {
// 1)添加任务 Runnable
System.out.println("CustomScheduleConfig执行动态任务job4=>" + DateUtil.formatDateTime(new Date()) + ",线程:"
+ Thread.currentThread().getName());
// 2)设置执行周期
}, triggerContext -> {
// 3)从数据库获取执行周期
String cron = cronTriggerDao.getCronTriggerById(2L);
if (cron != null) {
// 4)返回执行周期(Date)
return new CronTrigger(cron).nextExecutionTime(triggerContext);
} else {
return null;
}
});
taskRegistrar.addTriggerTask(triggerTask2);
}
}
复制代码
打印日志以下:
非特殊说明,本文版权归 朝雾轻寒 全部,转载请注明出处.
原文标题:Spring Boot 2.X(十二):定时任务
原文地址: https://www.zwqh.top/article/info/21
若是文章对您有帮助,请扫码关注下个人公众号,文章持续更新中...