当前位置:网站首页>@Scheduled注解详解
@Scheduled注解详解
2022-08-01 23:41:00 【drhrht】
文章目录
1.注解源码
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
String cron() default "";
String zone() default "";
long fixedDelay() default -1;
String fixedDelayString() default "";
long fixedRate() default -1;
String fixedRateString() default "";
long initialDelay() default -1;
String initialDelayString() default "";
}
2.注解参数
参数
说明
示例
cron
任务执行的cron表达式
0/2 * * * *
zone
cron表达时解析使用的时区,默认为服务器的本地时区。
使用java.util.TimeZone#getTimeZone(String)方法解析
GMT-8:00
fixedRate
固定速率上一次任务执行开始到下一次执行开始的间隔时间固定
,单位为ms。若在调度任务执行时,上一次任务还未执行完毕,会加入worker队列,等待上一次执行完成后,马上执行下一次任务
1000
fixedRateString
与fixedRate一致,只是间隔时间使用java.time.Duration#parse解析
1000或PT1S
fixedDelay
固定延迟上一次任务执行结束到下一次执行开始的间隔时间固定
,单位为ms。
1000
fixedDelayString
与fixedDelay一致,只是间隔时间使用java.time.Duration#parse解析
1000或PT1S
initialDelay
首次延迟多长时间后执行,单位ms。
之后按照fixedRate、fixedRateString、fixedDelay、fixedDelayString指定的规则执行,需要指定其中一个规则。
注意:不能和cron一起使用
1000
initialDelayString
与initialDelay 一致,只是间隔时间使用java.time.Duration#parse解析
1000或PT1S
3.示例
cron
@Scheduled(cron = "0/2 * * * * ?")
从0秒开始,每隔两秒执行一次。
zone
@Scheduled(cron = "0/2 * * * * ?", zone = "GMT-8:00")
从0秒开始,每隔两秒执行一次。
指定时间使用的时区为东八区。
fixedRate
@Scheduled(fixedRate = 1000)
1秒执行一次,上次执行开始后过1秒执行下一次。若到了1秒后但上次执行还未完成,会加入worker队列,等待上一次执行完成后,马上执行下一次。
fixedRateString
@Scheduled(fixedDelayString = "1000")
@Scheduled(fixedDelayString = "PT1S")
都表示1秒执行一次,上次执行开始后过1秒执行下一次。若到了1秒后但上次执行还未完成,会加入worker队列,等待上一次执行完成后,马上执行下一次。
fixedDelay
@Scheduled(fixedDelay = 1000)
1秒执行一次,上次执行完成后过1秒继续执行下一次。
fixedDelayString
@Scheduled(fixedDelayString = "1000")
@Scheduled(fixedDelayString = "PT1S")
都表示1秒执行一次,上次执行完成后过1秒继续执行下一次。
initialDelay
@Scheduled(initialDelay = 5000, fixedRate = 2000)
首次5秒后执行,后续每隔2秒执行一次(遵循fixedRate 规则)
@Scheduled(initialDelay = 5000, fixedDelay = 2000)
首次5秒后执行,后续每隔2秒执行一次(遵循fixedDelay 规则)
注意:initialDelay、initialDelayString都不能和cron一起使用
initialDelayString
@Scheduled(initialDelayString = "5000", fixedRate = 2000)
首次5秒后执行,后续每隔2秒执行一次(遵循fixedRate 规则)
@Scheduled(initialDelayString = "5000", fixedDelay = 2000)
首次5秒后执行,后续每隔2秒执行一次(遵循fixedDelay 规则)
注意:initialDelay、initialDelayString都不能和cron一起使用
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- The monthly salary of the test post is 5-9k, how to increase the salary to 25k?
- 测试岗月薪5-9k,如何实现涨薪到25k?
- 计算由两点定义的线的角度
- Flink学习第三天——一文带你了解什么是Flink流?
- Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
- 软技能之UML图
- 6133. 分组的最大数量
- 添加大量元素时使用 DocumentFragments
- With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
- cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
猜你喜欢
With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
【参营经历贴】2022网安夏令营
cmd command
TCP 可靠吗?为什么?
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights
Flink学习第三天——一文带你了解什么是Flink流?
Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历
cmd指令
Access the selected node in the console
Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
随机推荐
Work for 5 years, test case design is bad?To look at the big case design summary
Additional Features for Scripting
Chapter 12 End-User Task As Shell Scripts
数据机构---第五章树与二叉树---二叉树的概念---应用题
Additional Features for Scripting
The Spark of Sql join on the and and where
研发团队数字化转型实践
【参营经历贴】2022网安夏令营
使用Jenkins做持续集成,这个知识点必须要掌握
Nacos配置中心之加载配置
UI自动化测试框架搭建-标记性能较差用例
Programmer is still short of objects? A new one is enough
Chapter 11 Working with Dates and Times
6132. All the elements in the array is equal to zero - quick sort method
npm npm
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
Quartus 使用 tcl 文件快速配置管脚
IDEA入门看这一篇就够了
DRF generating serialization class code
Jmeter是什么