@Scheduled注解的使用这里不详细说明,直接对8个参数进行讲解。

参数详解

  1. cron
    该参数接收一个cron表达式,cron表达式是一个字符串,字符串以5或6个空格隔开,分开共6或7个域,每一个域代表一个含义。

cron表达式语法

[秒] [分] [小时] [日] [月] [周] [年]

注:[年]不是必须的域,可以省略[年],则一共6个域

通配符说明:

  • 表示所有值。 例如:在分的字段上设置 *,表示每一分钟都会触发。
    ? 表示不指定值。使用的场景为不需要关心当前设置这个字段的值。例如:要在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为”?” 具体设置为 0 0 0 10 * ?
  • 表示区间。例如 在小时上设置 “10-12”,表示 10,11,12点都会触发。
    , 表示指定多个值,例如在周字段上设置 “MON,WED,FRI” 表示周一,周三和周五触发
    / 用于递增触发。如在秒上面设置”5/15” 表示从5秒开始,每增15秒触发(5,20,35,50)。 在月字段上设置’1/3’所示每月1号开始,每隔三天触发一次。
    L 表示最后的意思。在日字段设置上,表示当月的最后一天(依据当前月份,如果是二月还会依据是否是润年[leap]), 在周字段上表示星期六,相当于”7”或”SAT”。如果在”L”前加上数字,则表示该数据的最后一个。例如在周字段上设置”6L”这样的格式,则表示“本月最后一个星期五”
    W 表示离指定日期的最近那个工作日(周一至周五). 例如在日字段上置”15W”,表示离每月15号最近的那个工作日触发。如果15号正好是周六,则找最近的周五(14号)触发, 如果15号是周未,则找最近的下周一(16号)触发.如果15号正好在工作日(周一至周五),则就在该天触发。如果指定格式为 “1W”,它则表示每月1号往后最近的工作日触发。如果1号正是周六,则将在3号下周一触发。(注,”W”前只能设置具体的数字,不允许区间”-“)。
    #序号(表示每月的第几个周几),例如在周字段上设置”6#3”表示在每月的第三个周六.注意如果指定”#5”,正好第五周没有周六,则不会触发该配置(用在母亲节和父亲节再合适不过了) ;小提示:’L’和 ‘W’可以一组合使用。如果在日字段上设置”LW”,则表示在本月的最后一个工作日触发;周字段的设置,若使用英文字母是不区分大小写的,即MON与mon相同。
    示例
    每隔5秒执行一次:*/5 * * * * ?

每隔1分钟执行一次:0 */1 * * * ?

每天23点执行一次:0 0 23 * * ?

每天凌晨1点执行一次:0 0 1 * * ?

每月1号凌晨1点执行一次:0 0 1 1 * ?

每月最后一天23点执行一次:0 0 23 L * ?

每周星期天凌晨1点实行一次:0 0 1 ? * L

在26分、29分、33分执行一次:0 26,29,33 * * * ?

每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?

cron表达式使用占位符
另外,cron属性接收的cron表达式支持占位符。eg:

配置文件:

time:cron: */5 * * * * *interval: 5

每5秒执行一次:

    @Scheduled(cron="${time.cron}")void testPlaceholder1() {System.out.println("Execute at " + System.currentTimeMillis());}@Scheduled(cron="*/${time.interval} * * * * *")void testPlaceholder2() {System.out.println("Execute at " + System.currentTimeMillis());}
  1. zone
    时区,接收一个java.util.TimeZone#ID。cron表达式会基于该时区解析。默认是一个空字符串,即取服务器所在地的时区。比如我们一般使用的时区Asia/Shanghai。该字段我们一般留空。

  2. fixedDelay
    上一次执行完毕时间点之后多长时间再执行。如:

@Scheduled(fixedDelay = 5000) //上一次执行完毕时间点之后5秒再执行
  1. fixedDelayString
    与 3. fixedDelay 意思相同,只是使用字符串的形式。唯一不同的是支持占位符。如:
@Scheduled(fixedDelayString = "5000") //上一次执行完毕时间点之后5秒再执行

占位符的使用(配置文件中有配置:time.fixedDelay=5000):

    @Scheduled(fixedDelayString = "${time.fixedDelay}")void testFixedDelayString() {System.out.println("Execute at " + System.currentTimeMillis());}

运行结果:

5. fixedRate
上一次开始执行时间点之后多长时间再执行。如:

@Scheduled(fixedRate = 5000) //上一次开始执行时间点之后5秒再执行
  1. fixedRateString
    与 5. fixedRate 意思相同,只是使用字符串的形式。唯一不同的是支持占位符。
  2. initialDelay
    第一次延迟多长时间后再执行。如:
@Scheduled(initialDelay=1000, fixedRate=5000) //第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次
  1. initialDelayString
    与 7. initialDelay 意思相同,只是使用字符串的形式。唯一不同的是支持占位符。

That’s all ! Thanks for reading.

附:
截至spring-context-4.3.14.RELEASE的源码

/*** An annotation that marks a method to be scheduled. Exactly one of* the {@link #cron()}, {@link #fixedDelay()}, or {@link #fixedRate()}* attributes must be specified.** <p>The annotated method must expect no arguments. It will typically have* a {@code void} return type; if not, the returned value will be ignored* when called through the scheduler.** <p>Processing of {@code @Scheduled} annotations is performed by* registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be* done manually or, more conveniently, through the {@code <task:annotation-driven/>}* element or @{@link EnableScheduling} annotation.** <p>This annotation may be used as a <em>meta-annotation</em> to create custom* <em>composed annotations</em> with attribute overrides.** @author Mark Fisher* @author Dave Syer* @author Chris Beams* @since 3.0* @see EnableScheduling* @see ScheduledAnnotationBeanPostProcessor* @see Schedules*/
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {/*** A cron-like expression, extending the usual UN*X definition to include* triggers on the second as well as minute, hour, day of month, month* and day of week.  e.g. {@code "0 * * * * MON-FRI"} means once per minute on* weekdays (at the top of the minute - the 0th second).* @return an expression that can be parsed to a cron schedule* @see org.springframework.scheduling.support.CronSequenceGenerator*/String cron() default "";/*** A time zone for which the cron expression will be resolved. By default, this* attribute is the empty String (i.e. the server's local time zone will be used).* @return a zone id accepted by {@link java.util.TimeZone#getTimeZone(String)},* or an empty String to indicate the server's default time zone* @since 4.0* @see org.springframework.scheduling.support.CronTrigger#CronTrigger(String, java.util.TimeZone)* @see java.util.TimeZone*/String zone() default "";/*** Execute the annotated method with a fixed period in milliseconds between the* end of the last invocation and the start of the next.* @return the delay in milliseconds*/long fixedDelay() default -1;/*** Execute the annotated method with a fixed period in milliseconds between the* end of the last invocation and the start of the next.* @return the delay in milliseconds as a String value, e.g. a placeholder* @since 3.2.2*/String fixedDelayString() default "";/*** Execute the annotated method with a fixed period in milliseconds between* invocations.* @return the period in milliseconds*/long fixedRate() default -1;/*** Execute the annotated method with a fixed period in milliseconds between* invocations.* @return the period in milliseconds as a String value, e.g. a placeholder* @since 3.2.2*/String fixedRateString() default "";/*** Number of milliseconds to delay before the first execution of a* {@link #fixedRate()} or {@link #fixedDelay()} task.* @return the initial delay in milliseconds* @since 3.2*/long initialDelay() default -1;String initialDelayString() default "";}

@Scheduled相关推荐

  1. scheduled sampling_seq2seq

    来源:NIPS 2015 本文介绍了decode时采样的一种新方法,称为"curriculum learning"(课程学习),对应的采样方法叫做"scheduled s ...

  2. Java Spring @Scheduled 定时任务crontab表达式设置

    Java Spring @Scheduled 定时任务crontab表达式设置 1. Cron详解 2. 例子 参考 1. Cron详解 Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或 ...

  3. SpringBoot源码分析之@Scheduled

    Springboot写上注解@Scheduled就可以实现定时任务, 这里对其源码做一点分析 @Service public class MyScheduled {@Scheduled(cron=&q ...

  4. scheduled 一秒钟执行一次_spring boot的Scheduled帮你实现定时任务,spring boot实践(11)...

    01 spring boot读取配置信息 02 多环境配置 03 处理全局异常 04 spring boot admin 05 spring mvc + mybatis 06 spring boot ...

  5. @scheduled cron动态修改_spring boot实现动态增删启停定时任务

    作者:jessehua 来源:https://www.jianshu.com/p/0f68936393fd 在spring boot项目中,可以通过@EnableScheduling注解和@Sched ...

  6. springBoot @Scheduled多任务同时开始执行

    这篇文章主要介绍了springBoot @Scheduled实现多个任务同时开始执行,具有很好的参考价值,希望对大家有所帮助.如有错误或未考虑完全的地方,望不吝赐教 @Scheduled多个任务同时开 ...

  7. Spring 的@Scheduled注解实现定时任务运行和调度

    Spring 的@Scheduled注解实现定时任务运行和调度 首先要配置我们的spring.xml   ---  即spring的主配置文件(有的项目中叫做applicationContext.xm ...

  8. @scheduled cron启动后和每小时执行_小耶哥: 一个Redis分布式锁又要和小鑫同学扯半个小时!...

    1 Redis分布式锁 |1-1 定时任务重复执行-问题引入 最近小耶哥在做一个功能, 什么功能呢? 就是超时未支付的订单我们要定时关闭, 释放库存, 并且短信通知用户该订单因超时被取消了.由于小耶哥 ...

  9. SpringBoot 实战定时任务 Scheduled

    序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...

  10. scheduled线程池ScheduledExecutorService只执行一次_有个定时任务突然不执行了

    scheduled线程池ScheduledExecutorService只执行一次_有个定时任务突然不执行了 原因 If any execution of the task encounters an ...

最新文章

  1. 依图颜水成:AI芯片设计要深度融合算法,才能实现极致性能
  2. jQuery针对多个元素的相同点击事件
  3. JSONEasy的用法(JSONDateHandler)
  4. 使用qtcreator中的git_Git中submodule的使用
  5. 《Android游戏编程入门经典》——1.7节小结
  6. 线程同步,线程不同步_同步多线程集成测试
  7. 北大教授李忠:谁说学数学只是为了升学?数学可以让你受益终生!
  8. rsync同步工具学习笔记
  9. 【Flink】Flink 写入 Clickhouse 大对象直接进入老年代 导致OOM
  10. 深入分析之Cluster层
  11. python爬虫外贸客户_python实战成功爬取海外批发商价格信息并写入记事本
  12. Electron技术架构
  13. 广州“开四停四”交通限行,技术上是如何实现的?
  14. LINUX 下查看设备状态的常用命令
  15. 量子计算机能做到0延迟吗,延迟选择量子擦除实验
  16. Android桌面插件宽度,android 屏幕适配插件
  17. C语言:鸡兔同笼(随机输入头数和脚数)
  18. i.MX6ULL系统移植 | 移植NXP官方linux4.1.15内核
  19. Linux系统制作启动U盘并安装centos 7.6
  20. OSI(网络)参考模型

热门文章

  1. 28个不得不看的经典编程算法!!
  2. python矩阵施密特标准型_矩阵与数值计算(3)——Schur标准型和Jordan分解
  3. python全文检索引擎_Python中使用haystack实现django全文检索搜索引擎功能
  4. 自然水体辐射特性与数值模拟 pdf_OpenGMS系列讲座(十三)汪亚平教授:南黄海水动力过程和辐射沙脊群演化...
  5. 学编导还是学计算机,高二学编导烧钱吗
  6. 对flex深入研究一点
  7. 论文《learning to link with wikipedia》
  8. pycharm安装lxml
  9. 如何使用 tf object detection
  10. 4.navicat11激活教程,亲测可用哦!