SpringBoot2.0系列--06--定时任务Scheduled及具体例子
SpringBoot2.0系列–06–定时任务Scheduled及具体例子
文章目录
- SpringBoot2.0系列--06--定时任务Scheduled及具体例子
- 前言
- 介绍
- 总流程
- 时间循环参数
- fixedRate
- fixedDelay
- cron
- 文字解释
- 代码解释
- 示例代码
- 联系方式
前言
JDK出11了,SpringBoot出2.0了,还没有系统的学习过,刚好最近项目中有使用到,就把一些关键的东西列出来,避免忘记
SpringBoot2.0系列–00–目录
介绍
SpringBoot的定时任务就是Spring里面的,可以直接使用Scheduled注解,
在这篇里面讲过这个类似的
https://blog.csdn.net/cmqwan/article/details/81117639
下面直接看下怎么使用吧
总流程
- 只要是交由Spring管理的类,都可以在其中的方法上面加上Scheduled注解,然后设置循环时间
- 在SpringBoot的主入口(Application)中添加@EnableScheduling注解,开启定时任务功能
时间循环参数
- fixedRate
- fixedDelay
- cron
fixedRate
不管内部执行过程,到时间就执行一次,单位毫秒
fixedDelay
在上一个执行完毕之后,执行下一个任务,单位毫秒
cron
这个参数可以从秒到年进行设置,扩展性最好,但是也最复杂
cron是一个表达式 可以看下这篇https://www.cnblogs.com/ark-blog/p/9000079.html字段名 含义 允许的值 允许的特殊字符 seconds 秒 0-59 , - * / minutes 分 0-59 , - * / hours 小时 0-23 , - * / daysOfMonth 日 1-31 , - * ? / L W C months 月 1-12 or JAN-DEC , - * / daysOfWeek 周几 1-7 or SUN-SAT , - * ? / L C #
--------------------- 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 * 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 ? * 6#3" 每月的第三个星期五上午10:15触发
文字解释
(T是任务,W是间隔时间,假设是5秒的间隔)
fixedRate: 不管内部执行过程,到时间就执行一次T1.T1WWWT2.T2.T2WW.T3.T3.T3.T3.T3.T4.T4.T4.T4.T4.T4.T4T5T5WWWT6.T6........
fixedDelay: 在上一个执行完毕之后,执行下一个任务T1.T1.WWWWW.T2.T2.T2WWWWW.T3.T3.T3.T3.T3.WWWWW.T4.T4.T4.T4.T4.T4.T4.WWWWWT6.T6......
---------------------
代码解释
大家可以看下下面的代码,执行下面的代码,打印出来的log。
代码中都是每3秒执行一次,在方法中sleep 1秒,然后打印出当前时间,整理好的log以图片的形式发出来。
可以看到在87秒的时间内fixedRate执行了30次,cron也是30次,fixedDelay是20次
2018-10-12 19:29:14.464 fixedRate 1 time 1539343754464
2018-10-12 19:29:15.464 fixedDelay 1 time 1539343755464
2018-10-12 19:29:16.465 cron 1 time 1539343756465
2018-10-12 19:29:17.465 fixedRate 2 time 1539343757465
2018-10-12 19:29:19.002 cron 2 time 1539343759002
2018-10-12 19:29:20.003 fixedDelay 2 time 1539343760003
2018-10-12 19:29:21.003 fixedRate 3 time 1539343761003
2018-10-12 19:29:22.003 cron 3 time 1539343762003
2018-10-12 19:29:23.465 fixedRate 4 time 1539343763465
2018-10-12 19:29:24.465 fixedDelay 3 time 1539343764465
2018-10-12 19:29:25.465 cron 4 time 1539343765465
2018-10-12 19:29:26.466 fixedRate 5 time 1539343766466
2018-10-12 19:29:28.003 cron 5 time 1539343768003
2018-10-12 19:29:29.003 fixedDelay 4 time 1539343769003
2018-10-12 19:29:30.003 fixedRate 6 time 1539343770003
2018-10-12 19:29:31.004 cron 6 time 1539343771004
2018-10-12 19:29:32.466 fixedRate 7 time 1539343772466
2018-10-12 19:29:33.467 fixedDelay 5 time 1539343773467
2018-10-12 19:29:34.468 cron 7 time 1539343774468
2018-10-12 19:29:35.468 fixedRate 8 time 1539343775468
2018-10-12 19:29:37.002 cron 8 time 1539343777002
2018-10-12 19:29:38.003 fixedDelay 6 time 1539343778003
2018-10-12 19:29:39.003 fixedRate 9 time 1539343779003
2018-10-12 19:29:40.003 cron 9 time 1539343780003
2018-10-12 19:29:41.465 fixedRate 10 time 1539343781465
2018-10-12 19:29:42.466 fixedDelay 7 time 1539343782466
2018-10-12 19:29:43.466 cron 10 time 1539343783466
2018-10-12 19:29:44.467 fixedRate 11 time 1539343784467
2018-10-12 19:29:46.003 cron 11 time 1539343786003
2018-10-12 19:29:47.003 fixedDelay 8 time 1539343787003
2018-10-12 19:29:48.003 fixedRate 12 time 1539343788003
2018-10-12 19:29:49.004 cron 12 time 1539343789004
2018-10-12 19:29:50.466 fixedRate 13 time 1539343790466
2018-10-12 19:29:51.466 fixedDelay 9 time 1539343791466
2018-10-12 19:29:52.467 cron 13 time 1539343792467
2018-10-12 19:29:53.467 fixedRate 14 time 1539343793467
2018-10-12 19:29:55.002 cron 14 time 1539343795002
2018-10-12 19:29:56.002 fixedDelay 10 time 1539343796002
2018-10-12 19:29:57.003 fixedRate 15 time 1539343797003
2018-10-12 19:29:58.003 cron 15 time 1539343798003
2018-10-12 19:29:59.464 fixedRate 16 time 1539343799464
2018-10-12 19:30:00.465 fixedDelay 11 time 1539343800465
2018-10-12 19:30:01.465 cron 16 time 1539343801465
2018-10-12 19:30:02.466 fixedRate 17 time 1539343802466
2018-10-12 19:30:04.003 cron 17 time 1539343804003
2018-10-12 19:30:05.003 fixedDelay 12 time 1539343805003
2018-10-12 19:30:06.003 fixedRate 18 time 1539343806003
2018-10-12 19:30:07.004 cron 18 time 1539343807004
2018-10-12 19:30:08.465 fixedRate 19 time 1539343808465
2018-10-12 19:30:09.465 fixedDelay 13 time 1539343809465
2018-10-12 19:30:10.466 cron 19 time 1539343810466
2018-10-12 19:30:11.466 fixedRate 20 time 1539343811466
2018-10-12 19:30:13.001 cron 20 time 1539343813001
2018-10-12 19:30:14.001 fixedDelay 14 time 1539343814001
2018-10-12 19:30:15.002 fixedRate 21 time 1539343815002
2018-10-12 19:30:16.002 cron 21 time 1539343816002
2018-10-12 19:30:17.464 fixedRate 22 time 1539343817464
2018-10-12 19:30:18.465 fixedDelay 15 time 1539343818465
2018-10-12 19:30:19.465 cron 22 time 1539343819465
2018-10-12 19:30:20.465 fixedRate 23 time 1539343820465
2018-10-12 19:30:22.002 cron 23 time 1539343822002
2018-10-12 19:30:23.003 fixedDelay 16 time 1539343823003
2018-10-12 19:30:24.003 fixedRate 24 time 1539343824003
2018-10-12 19:30:25.004 cron 24 time 1539343825004
2018-10-12 19:30:26.465 fixedRate 25 time 1539343826465
2018-10-12 19:30:27.465 fixedDelay 17 time 1539343827465
2018-10-12 19:30:28.465 cron 25 time 1539343828465
2018-10-12 19:30:29.466 fixedRate 26 time 1539343829466
2018-10-12 19:30:31.003 cron 26 time 1539343831003
2018-10-12 19:30:32.003 fixedDelay 18 time 1539343832003
2018-10-12 19:30:33.004 fixedRate 27 time 1539343833004
2018-10-12 19:30:34.004 cron 27 time 1539343834004
2018-10-12 19:30:35.464 fixedRate 28 time 1539343835464
2018-10-12 19:30:36.465 fixedDelay 19 time 1539343836465
2018-10-12 19:30:37.465 cron 28 time 1539343837465
2018-10-12 19:30:38.465 fixedRate 29 time 1539343838465
2018-10-12 19:30:40.002 cron 29 time 1539343840002
2018-10-12 19:30:41.003 fixedDelay 20 time 1539343841003
2018-10-12 19:30:42.003 fixedRate 30 time 1539343842003
2018-10-12 19:30:43.004 cron 30 time 1539343843004
示例代码
- 只要是交由Spring管理的类,都可以在其中的方法上面加上Scheduled注解,然后设置循环时间
/** Copyright (C), 2015-2018* FileName: MyScheduler* Author: zhao* Date: 2018/10/12 19:07* Description: 测试用的定时任务* History:* <author> <time> <version> <desc>* 作者姓名 修改时间 版本号 描述*/
package com.lizhaoblog.pro006scheduled.scheduled;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;/*** 〈一句话功能简述〉<br>* 〈测试用的定时任务〉** @author zhao* @date 2018/10/12 19:07* @since 1.0.1*/
@Component
public class MyScheduler {private static final Logger logger = LoggerFactory.getLogger(MyScheduler.class);private int count = 0;private int count2 = 0;private int count3 = 0;@Scheduled(fixedRate = 3000)public void fixedRate() {try {Thread.sleep(1000);} catch (InterruptedException e) {logger.debug("fixedRate", e);}count++;logger.info("fixedRate " + count + " time " + System.currentTimeMillis());}@Scheduled(fixedDelay = 3000)public void fixedDelay() {try {Thread.sleep(1000);} catch (InterruptedException e) {logger.debug("fixedDelay", e);}count2++;logger.info("fixedDelay " + count2 + " time " + System.currentTimeMillis());}// 每隔3秒执行一次:*/3 * * * * ?@Scheduled(cron = "*/3 * * * * ?")public void cron() {try {Thread.sleep(1000);} catch (InterruptedException e) {logger.debug("cron", e);}count3++;logger.info("cron " + count3 + " time " + System.currentTimeMillis());}}
- 在SpringBoot的主入口(Application)中添加@EnableScheduling注解,开启定时任务功能
package com.lizhaoblog.pro006scheduled;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableScheduling
public class Pro006ScheduledApplication {public static void main(String[] args) {SpringApplication.run(Pro006ScheduledApplication.class, args);}
}
联系方式
项目代码路径码云:https://gitee.com/lizhaoandroid/Springboot-Learning-lz
联系方式:QQ3060507060
查看下一篇或者其他文章,可点击目录或者专栏查看
SpringBoot2.0系列--06--定时任务Scheduled及具体例子相关推荐
- SpringBoot2.0系列(1)----初识SpringBoot
[SpringBoot2.0系列01]初识SpringBoot 一.介绍 想必大家都一定用过spring框架,每次整合spring框架的时候总是会有无穷无尽的xml配置文件,第一次写配置文件的时候,大 ...
- SpringBoot2.0系列--02--Controller
SpringBoot2.0系列–02–Controller 文章目录 SpringBoot2.0系列--02--Controller 写在前面 示例 对于整个Controller类 Controlle ...
- SpringBoot2.0系列(4)---SpringBoot之使用JPA完成简单的rest api
一. 前言 在前面我们已经知道在springboot中如何使用freemark与thymeleaf之类的视图模板引擎去渲染我们的视图页面,但是没涉及跟数据库交互的东西,所以今天在这里我们将介绍了一下如 ...
- SpringBoot2.0系列(03)---SpringBoot之使用freemark视图模板
前言 freemarker介绍: FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户 ...
- SpringBoot2.0系列教程(十三)Springboot防止XSS攻击
Hello大家好,本章我们添加防止XSS攻击功能 .有问题可以加我VX:Mrchuchen. 一:什么是XSS XSS攻击全称跨站脚本攻击,是一种在web应用中的计算机安全漏洞,它允许恶意web用户将 ...
- springboot2.0系列(二):配置属性
为什么80%的码农都做不了架构师?>>> 前言 Spring Boot中核心思想:约定优于配置.那到底什么是约定优于配置? 约定优于配置(convention over con ...
- SpringBoot2.0系列(2)---SpringBoot之使用Thymeleaf视图模板
前言 Thymeleaf 是Java服务端的模板引擎,与传统的JSP不同,前者可以使用浏览器直接打开,因为可以忽略掉拓展属性,相当于打开原生页面,给前端人员也带来一定的便利.如果你已经厌倦了JSP+J ...
- SpringBoot2.0 基础案例(04):定时任务和异步任务的使用方式
一.定时任务 1.基本概念 按照指定时间执行的程序. 2.使用场景 数据分析 数据清理 系统服务监控 二.同步和异步 1.基本概念 同步调用 程序按照代码顺序依次执行,每一行程序都必须等待上一行程序执 ...
- spring-boot-2.0.3不一样系列之源码篇 - springboot源码一,绝对有值得你看的地方
前言 上篇:spring-boot-2.0.3不一样系列之shiro - 搭建篇,实现了spring-boot与shiro的整合,效果大家也看到了,工程确实集成了shiro的认证与授权功能.如果大家能 ...
最新文章
- Metasploit技巧命令支持tips
- 文巾解题 182. 查找重复的电子邮箱
- 管理费用负数报不了怎么办_我的心脏血管堵了很久,大夫说打不开了,我可怎么办?...
- boost::fusion::zip用法的测试程序
- 趣谈设计模式 | 外观模式(Facade):为子系统提供高粒度接口
- Spring学习笔记之二----基于XML的Spring AOP配置
- IE6layout元素自动包含浮动元素
- linux pci带宽,Linux查看PCIe版本及速率
- Windows 10 Enterprise LTSC 2019 (x64) 版本 (安装+激活+添加系统邮箱)
- 2020考研初期作息时间表
- Leetcode 739 每日温度
- Android 上 Https 双向通信— 深入理解KeyManager 和 TrustManagers
- 邓俊辉《数据结构》-向量学习笔记
- [SRv6]《SRv6网络编程》SRv6 OAM与随路网络测量(2/2:IFIT)
- Docker使用docker-compose配合Makefile部署Nginx 挂载文件目录 实现视频图片html等静态资源的代理
- 数据传输服务包年包月_包年包月转按月付费
- 线性表篇 什么是线性表
- Jupyter Notebook 修改默认打开的文件夹的位置 jupyter_notebook_config.py改不了默认配置文件更改
- Flutter百度地图定位插件与地理围栏插件冲突问题
- 【计算机网络期末复习】
热门文章
- 牛客编程巅峰赛S2第5场 - 钻石王者 C.Tree III
- Python获取全国所有的省、市、县、镇、村
- mysql sql语句 引号_关于sql:何时在MySQL中使用单引号,双引号和反引号
- 四种方法解决01背包问题
- 【大学物理·早期量子论和量子力学基础】康普顿效应
- 电容电感阻抗模型分析和电源解耦电容选取经验
- 如何让计算机和服务器时间同步,如何设置电脑及服务器时间与授时服务器时间同步(耿娟平供稿)...
- 如何设置windows定时执行python程序,定时运行代码
- C#界面里的winform AutoValidate和CausesValidation属性
- outlook POP3 IMAP设置