当前位置:网站首页>@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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- PostgreSQL 基础--常用命令
- Calculate the angle of a line defined by two points
- Special characters & escapes in bat
- sys_kill system call
- [LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
- 6134. Find the closest node to the given two nodes - force double hundred code
- 6133. 分组的最大数量
- 颜色透明参数
- C语言——分支语句和循环语句
- Jmeter是什么
猜你喜欢
2022 6th Strong Net Cup Part WP
Access the selected node in the console
Classical Literature Reading--DLO
Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training
Work for 5 years, test case design is bad?To look at the big case design summary
技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
Quartus uses tcl files to quickly configure pins
The monthly salary of the test post is 5-9k, how to increase the salary to 25k?
Building a cloud-native DevOps environment
随机推荐
6133. 分组的最大数量
recursion: method calls itself
chrome复制一张图片的base64数据
Calculate the angle of a line defined by two points
一款简洁的文件传输工具
计算两点之间的距离
Chapter 11 Working with Dates and Times
2022 6th Strong Net Cup Part WP
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
Always use "noopener" or "noreferrer" for links that open in a new tab
Quartus 使用 tcl 文件快速配置管脚
Chapter 12 End-User Task As Shell Scripts
Classical Literature Reading--DLO
Get piggy homestay (short-term rental) data
几道关于golang并发的面试题
欧拉路径与欧拉回路
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights
避免使用 <b>、<i>、<s> 和 <u> 标签
PDF转Word有那么难吗?做一个文件转换器,都解决了