最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中@Scheduled注解的參數(shù)詳解

 更新時間:2023年11月10日 09:05:51   作者:日G一卒  
這篇文章主要介紹了Spring中@Scheduled注解的參數(shù)詳解,@Scheduled注解的使用這里不詳細說明,@Scheduled注解有幾個參數(shù)需要說明一下,直接對8個參數(shù)進行講解,需要的朋友可以參考下

@Scheduled注解的參數(shù)

@Scheduled注解的使用這里不詳細說明,直接對8個參數(shù)進行講解。

cron

該參數(shù)接收一個cron表達式,cron表達式是一個字符串,字符串以5或6個空格隔開,分開共6或7個域,每一個域代表一個含義。

cron表達式語法

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

注:[年]不是必須的域,可以省略[年],則一共6個域

在這里插入圖片描述

通配符說明:

  1. *表示所有值。 例如:在分的字段上設置 *,表示每一分鐘都會觸發(fā)。
  2. ? 表示不指定值。使用的場景為不需要關(guān)心當前設置這個字段的值。例如:要在每月的10號觸發(fā)一個操作,但不關(guān)心是周幾,所以需要周位置的那個字段設置為”?” 具體設置為 0 0 0 10 * ?
  3. -表示區(qū)間。例如 在小時上設置 “10-12”,表示 10,11,12點都會觸發(fā)。
  4. , 表示指定多個值,例如在周字段上設置 “MON,WED,FRI” 表示周一,周三和周五觸發(fā)
  5. / 用于遞增觸發(fā)。如在秒上面設置”5/15” 表示從5秒開始,每增15秒觸發(fā)(5,20,35,50)。 在月字段上設置’1/3’所示每月1號開始,每隔三天觸發(fā)一次。
  6. L 表示最后的意思。在日字段設置上,表示當月的最后一天(依據(jù)當前月份,如果是二月還會依據(jù)是否是潤年[leap]), 在周字段上表示星期六,相當于”7”或”SAT”。如果在”L”前加上數(shù)字,則表示該數(shù)據(jù)的最后一個。例如在周字段上設置”6L”這樣的格式,則表示“本月最后一個星期五”
  7. W 表示離指定日期的最近那個工作日(周一至周五). 例如在日字段上置”15W”,表示離每月15號最近的那個工作日觸發(fā)。如果15號正好是周六,則找最近的周五(14號)觸發(fā), 如果15號是周未,則找最近的下周一(16號)觸發(fā).如果15號正好在工作日(周一至周五),則就在該天觸發(fā)。如果指定格式為 “1W”,它則表示每月1號往后最近的工作日觸發(fā)。如果1號正是周六,則將在3號下周一觸發(fā)。(注,”W”前只能設置具體的數(shù)字,不允許區(qū)間”-“)。
  8. #序號(表示每月的第幾個周幾),例如在周字段上設置”6#3”表示在每月的第三個周六.注意如果指定”#5”,正好第五周沒有周六,則不會觸發(fā)該配置(用在母親節(jié)和父親節(jié)再合適不過了) ;小提示:’L’和 ‘W’可以一組合使用。如果在日字段上設置”LW”,則表示在本月的最后一個工作日觸發(fā);周字段的設置,若使用英文字母是不區(qū)分大小寫的,即MON與mon相同。

示例

  • 每隔5秒執(zhí)行一次:*/5 * * * * ?
  • 每隔1分鐘執(zhí)行一次:0 */1 * * * ?
  • 每天23點執(zhí)行一次:0 0 23 * * ?
  • 每天凌晨1點執(zhí)行一次:0 0 1 * * ?
  • 每月1號凌晨1點執(zhí)行一次:0 0 1 1 * ?
  • 每月最后一天23點執(zhí)行一次:0 0 23 L * ?
  • 每周星期天凌晨1點實行一次:0 0 1 ? * L
  • 在26分、29分、33分執(zhí)行一次:0 26,29,33 * * * ?
  • 每天的0點、13點、18點、21點都執(zhí)行一次:0 0 0,13,18,21 * * ?
  • cron表達式使用占位符 另外,cron屬性接收的cron表達式支持占位符。

eg:

配置文件:

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

每5秒執(zhí)行一次:

    @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

時區(qū),接收一個java.util.TimeZone#ID。cron表達式會基于該時區(qū)解析。

默認是一個空字符串,即取服務器所在地的時區(qū)。

比如我們一般使用的時區(qū)Asia/Shanghai。該字段我們一般留空。

fixedDelay

上一次執(zhí)行完畢時間點之后多長時間再執(zhí)行。如:

@Scheduled(fixedDelay = 5000) //上一次執(zhí)行完畢時間點之后5秒再執(zhí)行

fixedDelayString

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

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

@Scheduled(fixedDelayString = "5000") //上一次執(zhí)行完畢時間點之后5秒再執(zhí)行

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

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

運行結(jié)果:

在這里插入圖片描述

fixedRate

上一次開始執(zhí)行時間點之后多長時間再執(zhí)行。如:

@Scheduled(fixedRate = 5000) //上一次開始執(zhí)行時間點之后5秒再執(zhí)行

fixedRateString

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

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

initialDelay

第一次延遲多長時間后再執(zhí)行。如:

@Scheduled(initialDelay=1000, fixedRate=5000) //第一次延遲1秒后執(zhí)行,之后按fixedRate的規(guī)則每5秒執(zhí)行一次

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

到此這篇關(guān)于Spring中@Scheduled注解的參數(shù)詳解的文章就介紹到這了,更多相關(guān)@Scheduled注解的參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java 隨機生成6位短信驗證碼實例代碼

    java 隨機生成6位短信驗證碼實例代碼

    這篇文章主要介紹了java 隨機生成6位短信驗證碼的實例代碼,文中給大家擴展介紹了java隨機生成四位數(shù)字驗證碼的方法,需要的朋友可以參考下
    2019-12-12
  • java實現(xiàn)字符串四則運算公式解析工具類的方法

    java實現(xiàn)字符串四則運算公式解析工具類的方法

    今天小編就為大家分享一篇java實現(xiàn)字符串四則運算公式解析工具類的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • SpringAOP四種通知類型+環(huán)繞通知說明

    SpringAOP四種通知類型+環(huán)繞通知說明

    這篇文章主要介紹了SpringAOP四種通知類型+環(huán)繞通知說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java創(chuàng)建Web項目以及沒有web.xml的解決方法

    Java創(chuàng)建Web項目以及沒有web.xml的解決方法

    本文詳細介紹了如何在Eclipse中創(chuàng)建Web項目并自動生成web.xml,提供了直接生成、新建項目時勾選、手動創(chuàng)建Web.xml三種方法,重點在于web.xml的作用和常見問題解決方案,
    2026-01-01
  • springboot 如何解決cross跨域請求的問題

    springboot 如何解決cross跨域請求的問題

    這篇文章主要介紹了springboot 如何解決cross跨域請求的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 詳解Spring 參數(shù)驗證@Validated和@Valid的區(qū)別

    詳解Spring 參數(shù)驗證@Validated和@Valid的區(qū)別

    這篇文章主要介紹了詳解參數(shù)驗證 @Validated 和 @Valid 的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Java后端實現(xiàn)生成驗證碼圖片的示例代碼

    Java后端實現(xiàn)生成驗證碼圖片的示例代碼

    驗證碼是一種用于驗證用戶身份或確保用戶操作安全的技術(shù)手段,通常以圖形、聲音或文字的形式出現(xiàn),本文主要介紹了如何通過java實現(xiàn)生成驗證碼圖片,需要的可以參考下
    2023-12-12
  • 談談Java中try-catch-finally中的return語句

    談談Java中try-catch-finally中的return語句

    我們知道return語句用在某一個方法中,一是用于返回函數(shù)的執(zhí)行結(jié)果,二是用于返回值為void類型的函數(shù)中,僅僅是一個return語句(return ;),此時用于結(jié)束方法的執(zhí)行,也即此return后的語句將不會被執(zhí)行,當然,這種情況下return語句后不能再有其它的語句了
    2016-01-01
  • 使用java判斷101-200之間有多少個素數(shù)并輸出

    使用java判斷101-200之間有多少個素數(shù)并輸出

    在編程中,處理數(shù)學問題是一個常見的任務,本文將介紹如何使用Java語言來判斷101到200之間的整數(shù)中有多少個是素數(shù),并輸出這些素數(shù),需要的小伙伴可以了解下
    2026-01-01
  • 在springboot中如何使用線程池

    在springboot中如何使用線程池

    在SpringBoot中,可以通過定義ThreadPoolTaskExecutor的Bean并使用@Autowired注入來使用線程池,具體步驟包括創(chuàng)建ThreadPoolTaskExecutor的Bean配置,本文給大家介紹springboot使用線程池的例子,感興趣的朋友跟隨小編一起看看吧
    2024-09-09

最新評論

延寿县| 元氏县| 阿巴嘎旗| 双城市| 怀宁县| 镇平县| 黄浦区| 色达县| 若羌县| 利川市| 西充县| 娄底市| 鱼台县| 盐山县| 定西市| 禄丰县| 策勒县| 富源县| 裕民县| 内江市| 靖远县| 湘阴县| 许昌市| 浦北县| 灌南县| 盐津县| 拉孜县| 台北县| 石景山区| 壤塘县| 怀来县| 伊春市| 曲周县| 浮山县| 丰原市| 凤山县| 合水县| 江城| 绩溪县| 广水市| 梅河口市|