Spring Boot 菜鸟教程 13 注解定时任务
来源:互联网 发布:沪宁杭 知乎 编辑:程序博客网 时间:2024/06/05 12:02
GitHub
注解 @Scheduled(cron = “0/5 * * * * ?”)
相当于原来的xml版本的如下配置
<task:scheduled ref="scheduledTask" method="getTask1" cron="0/5 * * * * ?" />
定时任务类ScheduledTask
package com.jege.spring.boot.task;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;/** * @author JE哥 * @email 1272434821@qq.com * @description:从配置文件加载任务信息 */@Componentpublic class ScheduledTask { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedDelayString = "${jobs.fixedDelay}") public void getTask1() { System.out.println("任务1,从配置文件加载任务信息,当前时间:" + dateFormat.format(new Date())); } @Scheduled(cron = "${jobs.cron}") public void getTask2() { System.out.println("任务2,从配置文件加载任务信息,当前时间:" + dateFormat.format(new Date())); }}
application.properties
jobs.fixedDelay=5000jobs.cron=0/5 * * * * ?
@Scheduled中的参数说明
@Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行;@Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行;@Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;@Scheduled(cron="* * * * * ?"):按cron规则执行。
常用Cron表达式
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 0 0 12 ? * WED 表示每个星期三中午12点 "0 0 12 * * ?" 每天中午12点触发 "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触发 "0 15 10 ? * *" 每天上午10:15触发 "0 15 10 * * ?" 每天上午10:15触发 "0 15 10 * * ? *" 每天上午10:15触发 "0 15 10 * * ? 2005" 2005年的每天上午10:15触发
在线Cron表达式生成器
http://cron.qqe2.com/
源码地址
https://github.com/je-ge/spring-boot
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!
0 0
- Spring Boot 菜鸟教程 13 注解定时任务
- Spring Boot 菜鸟教程 14 动态修改定时任务cron参数
- Spring Boot (教程十三:定时任务)
- spring boot 定时任务
- spring boot 定时任务
- spring boot定时任务
- Spring boot 定时任务
- Spring Boot 定时任务
- spring 注解定时任务
- Spring Boot参考教程(十)定时任务
- Spring Boot 实现定时任务
- spring boot创建定时任务
- Spring Boot定时任务实例
- 【Spring Boot】定时任务实现
- Spring Boot 实现定时任务
- spring boot实现定时任务
- Spring Boot : 定时任务(十)
- Spring Boot入门教程-定时任务
- java项目中程序加载配置文件的几种写法
- Linux的用户和用户组管理
- HTML基本结构
- volatile关键字解析
- JSONObject与JSONArray的使用
- Spring Boot 菜鸟教程 13 注解定时任务
- 正则表达式30分钟入门教程
- win32 wmi编程获取系统信息
- Android进阶:ListView性能优化异步加载图片 使滑动效果流畅
- web前端之dojo实际应用及开发一:开始dojo(附有源码)
- Web前端开发必备--浏览器知识科普
- 配置好的popupwindow 拿来就用
- Java编程中常见的异常
- BitmapFactory.decodeFile能否对一张来自HTTP的图片进行解码呢