<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringBoot 中定時(shí)執(zhí)行注解(@Scheduled、@EnableScheduling)

          共 6377字,需瀏覽 13分鐘

           ·

          2021-01-19 04:26

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”

          優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

          ? 作者?|? 野狼谷

          來(lái)源 |? urlify.cn/qqMBnq

          76套java從入門到精通實(shí)戰(zhàn)課程分享

          項(xiàng)目開(kāi)發(fā)中經(jīng)常需要執(zhí)行一些定時(shí)任務(wù),比如需要在每天凌晨時(shí)候,分析一次前一天的日志信息。Spring為我們提供了異步執(zhí)行任務(wù)調(diào)度的方式,提供TaskExecutor 、TaskScheduler 接口。

          SpringBoot中使用兩個(gè)注解:@EnableScheduling、@Scheduled來(lái)簡(jiǎn)單實(shí)現(xiàn)定時(shí)任務(wù)。

          @Scheduled參數(shù)詳解

          1. cron

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

          cron表達(dá)式語(yǔ)法
          [秒] [分] [小時(shí)] [日] [月] [周] [年]

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

          序號(hào)說(shuō)明必填允許填寫的值允許的通配符
          10-59, - * /
          20-59, - * /
          3時(shí)0-23, - * /
          41-31, - * ? / L W
          51-12 / JAN-DEC, - * /
          61-7 or SUN-SAT, - * ? / L #
          71970-2099, - * /
          通配符說(shuō)明:
          • *?表示所有值。例如:在分的字段上設(shè)置 *,表示每一分鐘都會(huì)觸發(fā)。

          • ??表示不指定值。使用的場(chǎng)景為不需要關(guān)心當(dāng)前設(shè)置這個(gè)字段的值。例如:要在每月的10號(hào)觸發(fā)一個(gè)操作,但不關(guān)心是周幾,所以需要周位置的那個(gè)字段設(shè)置為”?” 具體設(shè)置為 0 0 0 10 * ?

          • -?表示區(qū)間。例如 在小時(shí)上設(shè)置 “10-12”,表示 10,11,12點(diǎn)都會(huì)觸發(fā)。

          • ,?表示指定多個(gè)值,例如在周字段上設(shè)置 “MON,WED,FRI” 表示周一,周三和周五觸發(fā)

          • /?用于遞增觸發(fā)。如在秒上面設(shè)置”5/15” 表示從5秒開(kāi)始,每增15秒觸發(fā)(5,20,35,50)。在月字段上設(shè)置’1/3’所示每月1號(hào)開(kāi)始,每隔三天觸發(fā)一次。

          • L?表示最后的意思。在日字段設(shè)置上,表示當(dāng)月的最后一天(依據(jù)當(dāng)前月份,如果是二月還會(huì)依據(jù)是否是潤(rùn)年[leap]), 在周字段上表示星期六,相當(dāng)于”7”或”SAT”。如果在”L”前加上數(shù)字,則表示該數(shù)據(jù)的最后一個(gè)。例如在周字段上設(shè)置”6L”這樣的格式,則表示“本月最后一個(gè)星期五”

          • W?表示離指定日期的最近那個(gè)工作日(周一至周五). 例如在日字段上置”15W”,表示離每月15號(hào)最近的那個(gè)工作日觸發(fā)。如果15號(hào)正好是周六,則找最近的周五(14號(hào))觸發(fā), 如果15號(hào)是周未,則找最近的下周一(16號(hào))觸發(fā).如果15號(hào)正好在工作日(周一至周五),則就在該天觸發(fā)。如果指定格式為 “1W”,它則表示每月1號(hào)往后最近的工作日觸發(fā)。如果1號(hào)正是周六,則將在3號(hào)下周一觸發(fā)。(注,”W”前只能設(shè)置具體的數(shù)字,不允許區(qū)間”-“)。

          • #?序號(hào)(表示每月的第幾個(gè)周幾),例如在周字段上設(shè)置”6#3”表示在每月的第三個(gè)周六.注意如果指定”#5”,正好第五周沒(méi)有周六,則不會(huì)觸發(fā)該配置(用在母親節(jié)和父親節(jié)再合適不過(guò)了) ;小提示:’L’和 ‘W’可以一組合使用。如果在日字段上設(shè)置”LW”,則表示在本月的最后一個(gè)工作日觸發(fā);周字段的設(shè)置,若使用英文字母是不區(qū)分大小寫的,即MON與mon相同。

          示例

          每隔5秒執(zhí)行一次:*/5 * * * * ?

          每隔1分鐘執(zhí)行一次:0 */1 * * * ?

          每天23點(diǎn)執(zhí)行一次:0 0 23 * * ?

          每天凌晨1點(diǎn)執(zhí)行一次:0 0 1 * * ?

          每月1號(hào)凌晨1點(diǎn)執(zhí)行一次:0 0 1 1 * ?

          每月最后一天23點(diǎn)執(zhí)行一次:0 0 23 L * ?

          每周星期天凌晨1點(diǎn)實(shí)行一次:0 0 1 ? * L

          在26分、29分、33分執(zhí)行一次:0 26,29,33 * * * ?

          每天的0點(diǎn)、13點(diǎn)、18點(diǎn)、21點(diǎn)都執(zhí)行一次:0 0 0,13,18,21 * * ?

          cron表達(dá)式使用占位符

          另外,cron屬性接收的cron表達(dá)式支持占位符。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());
          ????}

          2. zone

          時(shí)區(qū),接收一個(gè)java.util.TimeZone#IDcron表達(dá)式會(huì)基于該時(shí)區(qū)解析。默認(rèn)是一個(gè)空字符串,即取服務(wù)器所在地的時(shí)區(qū)。比如我們一般使用的時(shí)區(qū)Asia/Shanghai。該字段我們一般留空。

          3. fixedDelay

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

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

          4. fixedDelayString

          與?3. fixedDelay?意思相同,只是使用字符串的形式。唯一不同的是支持占位符。如:

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

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

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

          運(yùn)行結(jié)果:

          占位符的使用

          5. fixedRate

          上一次開(kāi)始執(zhí)行時(shí)間點(diǎn)之后多長(zhǎng)時(shí)間再執(zhí)行。如:

          @Scheduled(fixedRate?=?5000)?//上一次開(kāi)始執(zhí)行時(shí)間點(diǎn)之后5秒再執(zhí)行

          6. fixedRateString

          與?5. fixedRate?意思相同,只是使用字符串的形式。唯一不同的是支持占位符。

          7. initialDelay

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

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

          8. 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.
          ?*
          ?*?

          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.
          ?*
          ?*?

          Processing?of?{@code?@Scheduled}?annotations?is?performed?by
          ?*?registering?a?{@link?ScheduledAnnotationBeanPostProcessor}.?This?can?be
          ?*?done?manually?or,?more?conveniently,?through?the?{@code?}
          ?*?element?or?@{@link?EnableScheduling}?annotation.
          ?*
          ?*?

          This?annotation?may?be?used?as?a?meta-annotation?to?create?custom
          ?*?composed?annotations?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?"";

          }






          粉絲福利:Java從入門到入土學(xué)習(xí)路線圖

          ??????

          ??長(zhǎng)按上方微信二維碼?2 秒


          感謝點(diǎn)贊支持下哈?

          瀏覽 43
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  午夜成人社区 | 丁香婷婷综合网 | 激情入口| 国产精品一区二区三区四区 | 国产毛片AV |