欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring task @Scheduled注解各參數(shù)的用法

 更新時(shí)間:2021年10月18日 09:53:23   作者:sprinkle_liz  
這篇文章主要介紹了spring task @Scheduled注解各參數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

@Scheduled注解的使用這里不詳細(xì)說(shuō)明,直接對(duì)8個(gè)參數(shù)進(jìn)行講解。

參數(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ō)明 必填 允許填寫的值 允許的通配符
1 0-59 , - * /
2 0-59 , - * /
3 時(shí) 0-23 , - * /
4 1-31 , - * /
5 1-12 / JAN-DEC , - * ? / L W
6 1-7 or SUN-SAT , - * ? / L #
7 1970-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 * * ?

2. zone

時(shí)區(qū),接收一個(gè)java.util.TimeZone#ID。cron表達(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.
 *
 * <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注解使用誤區(qū)

強(qiáng)烈建議同胞們看。

在使用spring @Scheduled注解時(shí)很多人都為cron表達(dá)式無(wú)法進(jìn)行配置進(jìn)行煩惱吧,為何不像quartz般能在applicationContext中進(jìn)行配置。

告訴大家其實(shí)是能applicationContext進(jìn)行配置。

xml:

<context:annotation-config />  
<!--spring掃描注解的配置   -->  
<context:component-scan base-package="com.baodian.bdweb.timer" /> 
    
<!--  開(kāi)啟這個(gè)配置,spring才能識(shí)別@Scheduled注解     -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
<task:scheduler id="qbScheduler" pool-size="10"/>

java:

@Component("Taskaa")
public class Taskaa{
 
 @Scheduled(cron = "01 50 15 * * ?")
 public void show(){
  SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  System.out.println("Taskaa:"+s.format(new Date()));  
 }
}<pre name="code" class="java">@Component("Taskbb")
public class Taskbb{
 
 @Scheduled(cron = "01 49 15 * * ?")
 public void show(){
  SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  System.out.println("Taskbb:"+s.format(new Date()));  
 }
}

上面這一種方式是大家最頭痛的問(wèn)題吧,如果測(cè)試人員需要測(cè)試這個(gè)任務(wù),你需要不停生成class文件給測(cè)試人員替換,是不是非常郁悶,其實(shí)我前期也被逼瘋了。我前期一直使用的Quartz,因此對(duì)該注解不是非常熟悉,趁著這個(gè)閑時(shí)想看看這個(gè)注解,結(jié)果一百度,卻找不到想要的結(jié)果。。換上我的谷歌繼續(xù),功夫不負(fù)有心人,找到了一篇該注解的詳解,因此有了這一篇文章。廢話不多說(shuō),看原子彈來(lái)了!

xml:

<context:annotation-config />  
<!--spring掃描注解的配置   -->  
<context:component-scan base-package="com.baodian.bdweb.timer" /> 
    
<bean id="Taskaa" class="com.baodian.bdweb.timer.Taskaa" />  
<bean id="Taskbb" class="com.baodian.bdweb.timer.Taskbb" />  
<task:scheduled-tasks>  
     <task:scheduled ref="Taskaa" method="show" cron="01 38 15 * * ?" />  
     <task:scheduled ref="Taskbb" method="show" cron="01 39 15 * * ?" /> 
</task:scheduled-tasks>

java:

@Component("Taskaa")
public class Taskaa{
 
 @Scheduled
 public void show(){
  SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  System.out.println("Taskaa:"+s.format(new Date()));  
 }
}
@Component("Taskbb")
public class Taskbb{
 
 @Scheduled
 public void show(){
  SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  System.out.println("Taskbb:"+s.format(new Date()));  
 }
}

是不是很簡(jiǎn)單,嘿嘿嘿!原子彈就造到這里了,配置很簡(jiǎn)單在這里就不細(xì)說(shuō)了,主要就是想吐槽一下百度,嘿嘿嘿!

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • JavaMail實(shí)現(xiàn)發(fā)送郵件功能

    JavaMail實(shí)現(xiàn)發(fā)送郵件功能

    這篇文章主要為大家詳細(xì)介紹了JavaMail實(shí)現(xiàn)發(fā)送郵件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Java8時(shí)間api之LocalDate/LocalDateTime的用法詳解

    Java8時(shí)間api之LocalDate/LocalDateTime的用法詳解

    在項(xiàng)目中,時(shí)間的使用必不可少,而java8之前的時(shí)間api?Date和Calander等在使用上存在著很多問(wèn)題,于是,jdk1.8引進(jìn)了新的時(shí)間api-LocalDateTime,本文就來(lái)講講它的具體使用吧
    2023-05-05
  • Spring Boot 的創(chuàng)建和運(yùn)行示例代碼詳解

    Spring Boot 的創(chuàng)建和運(yùn)行示例代碼詳解

    Spring Boot 的誕生是為了簡(jiǎn)化Spring程序的開(kāi)發(fā),今天給大家介紹下Spring Boot 的創(chuàng)建和運(yùn)行,主要包括Spring Boot基本概念和springboot優(yōu)點(diǎn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-07-07
  • springboot?正確的在異步線程中使用request的示例代碼

    springboot?正確的在異步線程中使用request的示例代碼

    這篇文章主要介紹了springboot中如何正確的在異步線程中使用request,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • Java編程實(shí)現(xiàn)軌跡壓縮之Douglas-Peucker算法詳細(xì)代碼

    Java編程實(shí)現(xiàn)軌跡壓縮之Douglas-Peucker算法詳細(xì)代碼

    這篇文章主要介紹了Java編程實(shí)現(xiàn)軌跡壓縮之Douglas-Peucker算法詳細(xì)代碼,具有一定借鑒價(jià)值,需要的朋友可以參考。
    2017-11-11
  • 微信小程序 springboot后臺(tái)如何獲取用戶的openid

    微信小程序 springboot后臺(tái)如何獲取用戶的openid

    這篇文章主要介紹了微信小程序 springboot后臺(tái)如何獲取用戶的openid,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Spring教程之refresh()執(zhí)行邏輯淺析

    Spring教程之refresh()執(zhí)行邏輯淺析

    這篇文章主要給大家介紹了關(guān)于Spring教程之refresh()執(zhí)行邏輯的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java實(shí)現(xiàn)萬(wàn)年歷效果

    Java實(shí)現(xiàn)萬(wàn)年歷效果

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)萬(wàn)年歷效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • java實(shí)現(xiàn)中綴表達(dá)式轉(zhuǎn)后綴的方法

    java實(shí)現(xiàn)中綴表達(dá)式轉(zhuǎn)后綴的方法

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)中綴表達(dá)式轉(zhuǎn)后綴的表達(dá)式方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • mybatis插入與批量插入返回ID的原理詳解

    mybatis插入與批量插入返回ID的原理詳解

    這篇文章主要給大家介紹了關(guān)于mybatis插入與批量插入返回ID的原理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07

最新評(píng)論