SpringBoot:@Schedule定时任务
一,@Schedule
SpringBoot内置了Sping Schedule定时框架,通过注解驱动方式添加所注解方法到定时任务,根据配置定时信息定时执行
二,定时任务实现
1,开启定时任务
package com.gupao.springboot;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableAsync
// 开启定时任务
@EnableScheduling
@MapperScan(basePackages = {"com.gupao.springboot.*.mapper"})
public class GupaoSpringbootApplication {public static void main(String[] args) {SpringApplication.run(GupaoSpringbootApplication.class, args);}}
2,定时任务,方法上有注释
package com.gupao.springboot.schedules;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import java.util.Date;/*** 定时任务演示* @author pj_zhang* @create 2018-12-26 21:19**/
@Component
public class ScheduleTestTask {// 如果方法执行时间超过定时器时间, 方法执行完成后直接执行任务@Scheduled(fixedRate = 3000)public void test_1() {System.out.println("fixedRate : " + new Date());try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}}// 方法执行完成后, 停留间隔时间, 再次执行该方法// 不因为方法执行时间长度影响定时器@Scheduled(fixedDelay = 3000)public void test_2() {System.out.println("fixedDelag : " + new Date());try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}}// 固定间隔时间执行, 方法执行完成后, 按照间隔时间点再次执行该方法// 比如方法执行5s, 定时间隔为3s, 则中间有一次执行不上, 从第6s开始下一次执行@Scheduled(cron = "0/3 * * * * *")public void test_3() {System.out.println("corn : " + new Date());try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}}}
3,测试
* 可以看到三个线程按照线程争抢方式依次执行,与三个定时器并行不相符,原因何在?
* 进入Spring Schedule源码org.springframework.scheduling.config.ScheduledTaskRegistrar#scheduleTasks
protected void scheduleTasks() {if(this.taskScheduler == null) {// 从这一行可以看出, 定时调度任务初始化时候初始化了一个单线程的线程池// 所以在定时任务调度时候, 如果定时任务过多, 就会存在线程争抢// 而且每一次也只会有一个定时任务运行this.localExecutor = Executors.newSingleThreadScheduledExecutor();this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);}}
三,定时任务线程池配置
1,可以通过配置信息更改定时调度任务线程池数量
package com.gupao.springboot.config;import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;import java.util.concurrent.Executors;/*** 更改定时器线程池初始化数量配置信息* @author pj_zhang* @create 2018-12-26 21:27**/
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {@Overridepublic void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {// 修改初始化线程数为100scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(100));}
}
* 执行结果,根据时间点可以看到,各个定时任务并行执行,不存在线程争抢
四,corn表达式
1,先放个大招,在线corn表达式生成器
* http://cron.qqe2.com/
2,表达式基本公式
* * * * * * * 分别对应,七个*分别对应单位为:秒,分,时,日,月,星期,年
3,字段含义
字段 | 取值范围 | 特殊字符 |
---|---|---|
秒 | 0~59的整数 | ,- * / |
分 | 0~59的整数 | ,- * / |
时 | 0~23的整数 | ,- * / |
日 | 1~31的整数 | ,- * ? / L W |
月 | 1~12的整数 | ,- * / |
星期 | 1~7的整数或者SUN~SAT(1=SUN) | ,- * ? / L # |
年 | 1970~2099 | ,- * / |
* "," :表示列出枚举值,例如分钟使用5,20,则表示5和20分钟各执行一次
* "-" :表示范围,例如分钟使用5-20,表示5-20分钟每分钟触发一次
* "*" :表示匹配该域任意值,例如分钟使用*,表示每分钟都会执行一次
* "/" :表示起始时间开始触发,以后每隔多长时间触发一次,例如秒使用0/3,表示从0开始触发,后每三分钟触发一次
* "?":只能在日和星期使用,表示匹配任意值,但实际不会;因为日和星期可能会存在冲突,如果想表示每月20号0点执行,则需要写为 0 0 0 20 * ?,星期位必须写为?,虽然概念上*也表示通配
* "L" :表示最后,只出现在日和星期;例如在星期的5L,表示最后一个星期五触发
* "W" :表示有效工作日(周一-周五),只出现在日,如果指定的当天在某月刚好为周末,则就近取周五或周一执行
* "LW" :连用表示每个月最后一个星期五,只在日使用
* "#" :用于确定第几个星期的星期几,只在星期使用;例如2#3,表示在每月的第三个星期一
4,常用表达式实例
-- 0/3 * * * * ? :表示每三秒钟执行一次
-- 0 0 2 1 * ? :表示每月1号凌晨两点执行任务
-- 0 15 10 ? * MON-FRI :表示周一到周五每天早上10:15执行
-- 0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
SpringBoot:@Schedule定时任务相关推荐
- SpringBoot+Schedule 定时任务的配置开关
很实用,建议收藏 @Scheduled(cron = "0/4 * * * * ?") springboot 定时任务注解 用起来简直不要太shuang, 我们有时在部署测试环境时 ...
- springboot之定时任务
定时线程 说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务. 回顾一下定时线程池. public static ScheduledExecutorService newScheduledT ...
- linux定时任务重复率,基于SpringBoot实现定时任务的设置(常用:定时清理数据库)...
1.构建SpringBoot工程项目 1)创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务. @SpringBootApplication @Ena ...
- SpringBoot | :定时任务的使用
前言 上一章我们简单的讲解了关于异步请求相关知识点.这一章节,我们来讲讲开发过程也是经常会碰见的定时任务.比如每天定时清理无效数据.定时发送短信.定时发送邮件.支付系统中的定时对账等等,往往都会定义一 ...
- Spring整合Schedule定时任务详解
Spring整合Schedule定时任务详解 Spring 定时任务官方网站 一.概述 用Spring,就是为了简单. 但是我还是要总结下java定时任务实现的几种方式. 1.TimerTask,等于 ...
- SpringBoot@Schedule入门基础
SpringBoot@Schedule入门基础 前言 Schedule是一个任务调度器,SpringBoot中可定时触发执行定时任务. 一.基本概念 在SpringBoot中,使用 @Schedule ...
- SpringBoot Schedule的三种使用方式
SpringBoot Schedule的三种使用方式 静态schedule 结果图 动态schedule schedule代码 结果图 mapper代码 application.yml文件配置 pom ...
- SpringBoot 实战定时任务 Scheduled
序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...
- springboot配置定时任务及常用的cron表达式
springboot引入定时任务 springboot引入定时任务主要需要以下几步: 1.引入相关的依赖 2.配置程序开启定时任务 3. 编写定时任务 引入相关的依赖 只用引一个基础的web的依赖就可 ...
- SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊)
SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 目录 SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 1.概述 2.最佳实践 2.1创建项目引入依赖(mail) ...
最新文章
- Java 异常处理的 9 个最佳实践
- ps -aux|grep 详细信息
- 多级队列调度算法可视化界面_冷月手撕408之操作系统(8)-处理机调度
- 医学图像处理期末复习(四)
- C语言实现寻找极值点,九之再续:教你一步一步用c语言实现sift算法、上
- html 树形图可拖拽,HTML5拖拽API实现vue树形拖拽组件
- Flutter 列表踩坑2
- matlab视频帧间差分,matlab中视频帧间差分
- Django中的ORM进阶操作
- oracle10g最快安装教程,史上最详细Oracle 10g安装教程
- NXP JN5169 USB Dongle 原理图
- 制作win7 u盘启动盘
- 阅读学术论文的心得体会
- Qt示例程序打开失败,出现一个感叹号图标
- hbase安装启动成功,但是执行命令报错
- 深入编程之QQ盗号核心代码
- 一个架构师的价值在于,他不仅能看到系统的美,而且能够在建造系统的时候能够把这些美创造出来...
- SAP请求本地导出导入
- 从Oracle迁移到PostgreSQL的十大理由
- kali更新系统软件官方源连接不上报错