Spring中@Scheduled注解的参数详解

 更新时间:2023年11月10日 09:05:51   作者:日G一卒  
这篇文章主要介绍了Spring中@Scheduled注解的参数详解,@Scheduled注解的使用这里不详细说明,@Scheduled注解有几个参数需要说明一下,直接对8个参数进行讲解,需要的朋友可以参考下

@Scheduled注解的参数

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

cron

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

cron表达式语法

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

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

在这里插入图片描述

通配符说明:

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

zone

时区,接收一个java.util.TimeZone#ID。cron表达式会基于该时区解析。

默认是一个空字符串,即取服务器所在地的时区。

比如我们一般使用的时区Asia/Shanghai。该字段我们一般留空。

fixedDelay

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

@Scheduled(fixedDelay = 5000) //上一次执行完毕时间点之后5秒再执行

fixedDelayString

与  fixedDelay 意思相同,只是使用字符串的形式。

唯一不同的是支持占位符。如:

@Scheduled(fixedDelayString = "5000") //上一次执行完毕时间点之后5秒再执行

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

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

运行结果:

在这里插入图片描述

fixedRate

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

@Scheduled(fixedRate = 5000) //上一次开始执行时间点之后5秒再执行

fixedRateString

与fixedRate 意思相同,只是使用字符串的形式。

唯一不同的是支持占位符。

initialDelay

第一次延迟多长时间后再执行。如:

@Scheduled(initialDelay=1000, fixedRate=5000) //第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次

initialDelayString

与  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 "";
}

到此这篇关于Spring中@Scheduled注解的参数详解的文章就介绍到这了,更多相关@Scheduled注解的参数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java 随机生成6位短信验证码实例代码

    java 随机生成6位短信验证码实例代码

    这篇文章主要介绍了java 随机生成6位短信验证码的实例代码,文中给大家扩展介绍了java随机生成四位数字验证码的方法,需要的朋友可以参考下
    2019-12-12
  • java实现字符串四则运算公式解析工具类的方法

    java实现字符串四则运算公式解析工具类的方法

    今天小编就为大家分享一篇java实现字符串四则运算公式解析工具类的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • SpringAOP四种通知类型+环绕通知说明

    SpringAOP四种通知类型+环绕通知说明

    这篇文章主要介绍了SpringAOP四种通知类型+环绕通知说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Java创建Web项目以及没有web.xml的解决方法

    Java创建Web项目以及没有web.xml的解决方法

    本文详细介绍了如何在Eclipse中创建Web项目并自动生成web.xml,提供了直接生成、新建项目时勾选、手动创建Web.xml三种方法,重点在于web.xml的作用和常见问题解决方案,
    2026-01-01
  • springboot 如何解决cross跨域请求的问题

    springboot 如何解决cross跨域请求的问题

    这篇文章主要介绍了springboot 如何解决cross跨域请求的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • 详解Spring 参数验证@Validated和@Valid的区别

    详解Spring 参数验证@Validated和@Valid的区别

    这篇文章主要介绍了详解参数验证 @Validated 和 @Valid 的区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • Java后端实现生成验证码图片的示例代码

    Java后端实现生成验证码图片的示例代码

    验证码是一种用于验证用户身份或确保用户操作安全的技术手段,通常以图形、声音或文字的形式出现,本文主要介绍了如何通过java实现生成验证码图片,需要的可以参考下
    2023-12-12
  • 谈谈Java中try-catch-finally中的return语句

    谈谈Java中try-catch-finally中的return语句

    我们知道return语句用在某一个方法中,一是用于返回函数的执行结果,二是用于返回值为void类型的函数中,仅仅是一个return语句(return ;),此时用于结束方法的执行,也即此return后的语句将不会被执行,当然,这种情况下return语句后不能再有其它的语句了
    2016-01-01
  • 使用java判断101-200之间有多少个素数并输出

    使用java判断101-200之间有多少个素数并输出

    在编程中,处理数学问题是一个常见的任务,本文将介绍如何使用Java语言来判断101到200之间的整数中有多少个是素数,并输出这些素数,需要的小伙伴可以了解下
    2026-01-01
  • 在springboot中如何使用线程池

    在springboot中如何使用线程池

    在SpringBoot中,可以通过定义ThreadPoolTaskExecutor的Bean并使用@Autowired注入来使用线程池,具体步骤包括创建ThreadPoolTaskExecutor的Bean配置,本文给大家介绍springboot使用线程池的例子,感兴趣的朋友跟随小编一起看看吧
    2024-09-09

最新评论