@Scheduled

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/249284.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/249284.shtml
英文地址,請注明出處:http://en.pswp.cn/news/249284.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

eclipse2019-03設置代碼編輯區背景為圖片

一、我的主題設置如下所示 二、找到如下所示或類似的文件夾 三、在該文件夾里的images文件夾里添加圖片 四、在CSS目錄下的e4-dark_win.css文件中添加如下代碼   .MPart StyledText {     background-image: url(./bg.jpg);     background-repeat: no-repeat;  …

idea集成gitlab使用ssh免密登錄

網上有很多介紹ssh免密登錄的文章&#xff0c;具體步驟如下&#xff1a; 1. 生成SSH Key ssh-keygen -t rsa -C "your_emailexample.com" 默認會在相應路徑下&#xff08;/your_home_path&#xff09;生成id_rsa和id_rsa.pub兩個文件&#xff0c;此時終端會顯示&…

vue從入門到精通之基礎篇(三)生命周期

生命周期 定義&#xff1a; 每個 Vue 實例在被創建時都要經過從創建倒掛載再到更新、卸載的一系列過程&#xff0c;同時在這個過程中也會運行一些叫做生命周期鉤子的函數&#xff0c;可以讓我們用自己注冊的js方法控制整個大局&#xff0c;在這些事件響應方法中的this直接指向…

libcurl庫進行http通訊網絡編程

https://www.cnblogs.com/lifan3a/articles/7479256.html

Java 開始

&#xff08;事先聲明&#xff1a;該文章并非完全是我自己的產出&#xff0c;更多的是我個人在看到資料后通過理解并記錄下來&#xff0c;作為自己閱讀后的一個筆記&#xff1b;我現在試圖對自己多年工作中的知識點做一個回顧&#xff0c;希望能融會貫通&#xff09; &#xff…

Java核心技術筆記——第 12 章 反射

轉載自&#xff1a;[https://www.cnblogs.com/chanshuyi/p/head_first_of_reflection.html] 12 反射 1. 引入反射 通常情況下&#xff0c;調用一個類的方法的步驟如下&#xff1a; 創建該類的對象。調用該對象的方法。通過這種方式調用方法時&#xff0c;必須要知道類的定義以及…

HTML5知識點匯總

1、HTML5新特性 用于繪畫的canvas標簽用于媒介回放的video和audio元素對本地離線儲存的更好支持新的特殊內容元素&#xff0c;如&#xff1a;article、footer、header、nav、section、aside、hgroup、figure新的表單控件&#xff0c;如&#xff1a;calendar、date、time、emai…

實用網站

https://blog.csdn.net/devcloud/article/details/103121883

網絡(圖)(數學)

轉載于:https://www.cnblogs.com/fengxunling/p/9781575.html

DES加解密時 Given final block not properly padded 的解決方案

事情的經過是這個樣子的。。。。。。 先說說問題是怎么出現的。根據客戶需求&#xff0c;需要完成一個一鍵登錄的功能&#xff0c;于是我的項目中就誕生了DesUtil&#xff0c;但是經過幾百次測試&#xff0c;發現有一個登錄直接報錯&#xff01;難道又遇到神坑啦&#xff01;&a…

java 算法優化向導

一.什么是數據結構&#xff1f;什么是算法 不必像學生時代深究定義。以一個簡單的例子說明。 數據結構&#xff0c;圖書館的書怎么擺列&#xff0c;按照書的類型&#xff0c;作者&#xff0c;出版時間&#xff0c;語言等等放置&#xff0c;這就是數據的結構。 算法&#xff0c…

appium工作原理

Appium原理 面試的時候&#xff0c;被問到appium原理&#xff0c;一點不會&#xff0c;實在尷尬。大家可以直接翻看原作https://blog.csdn.net/jffhy2017/article/details/69220719 appium運行時安裝的2個應用&#xff1a;Appium Settings和Unlock。 一、appium加載的過程圖解&…

LeetCode 21. Merge Two Sorted Lists

LeetCode 21. Merge Two Sorted Lists 分析 難度&#xff1a;易 題目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Out…

Mac OS X 下 TAR.GZ 方式安裝 MySQL

Mac OS X 下 TAR.GZ 方式安裝 MySQL 注意: 本篇文章適用與 MySQL 5.6 版本的安裝, 但已不再適用 5.7 的安裝, 5.7 的安裝方式請參見:《Mac OS X 下 TAR.GZ 方式安裝 MySQL 5.7》 在 Mac 系統上, 安裝 MySQL Server 一般是用 DMG 包在圖形化界面下按提示安裝, 此外 MySQL 還提供…

快排再改進

快排再改進 #include <iostream> using namespace std;void mySwap(int &a, int &b) {int temp a;a b;b temp; }void insertSort(int a[], int left, int right) {int tmp;int in 0;int out 0;for (out left 1; out < right; out) {tmp a[out];in ou…

【Linux基礎】crontab定時命令詳解

周期執行的任務一般由cron這個守護進程來處理[ps -ef|grep cron]。cron讀取一個或多個配置文件&#xff0c;這些配置文件中包含了命令行及其調用時間。cron的配置文件稱為“crontab”&#xff0c;是“cron table”的簡寫。 一、cron服務  cron是一個linux下 的定時執行工具&a…

5個Vue.js項目的令人敬畏的模板

開發人員查看使用SPA&#xff0c;Webpack&#xff0c;身份驗證&#xff0c;GraphQL&#xff0c;文檔和測試的Vue開發人員的資源。 你準備開始一個重要的Vue項目嗎&#xff1f;為了確保從堅實的基礎開始&#xff0c;您可以使用模板&#xff08;也就是樣板&#xff0c;骨架&#…

測試多個輸入條件的方法

轉載于:https://www.cnblogs.com/www-qcdwx-com/p/10641281.html

問題規模

問題規模本身并沒有非常精準的定義吧一般都是指運行時間t和輸入參數個數n的關系用O(n)表示比如max([x])就是O(n)而冒泡排序則是O(n^2)

SSM+mybatis單元測試

初學SSMmybatis單元測試遇到的問題&#xff0c;dao注入后為nullDao層注入失敗&#xff0c;查看后&#xff0c;發現注解都寫的無誤&#xff0c;經朋友的指點&#xff0c;在測試類上加了一句“RunWith(SpringJUnit4ClassRunner.class) ContextConfiguration(locations{“classpat…