@Scheduled注解的使用這里不詳細說明,直接對8個參數進行講解。
參數詳解
1. cron
該參數接收一個cron表達式,cron表達式是一個字符串,字符串以5或6個空格隔開,分開共6或7個域,每一個域代表一個含義。
cron表達式語法
[秒] [分] [小時] [日] [月] [周] [年]
注:[年]不是必須的域,可以省略[年],則一共6個域
序號 | 說明 | 必填 | 允許填寫的值 | 允許的通配符 |
---|---|---|---|---|
1 | 秒 | 是 | 0-59 | , - * / |
2 | 分 | 是 | 0-59 | , - * / |
3 | 時 | 是 | 0-23 | , - * / |
4 | 日 | 是 | 1-31 | , - * / |
5 | 月 | 是 | 1-12 / JAN-DEC | , - * ? / L W |
6 | 周 | 是 | 1-7 or SUN-SAT | , - * ? / L # |
7 | 年 | 否 | 1970-2099 | , - * / |
通配符說明:
-
*
表示所有值。 例如:在分的字段上設置 *,表示每一分鐘都會觸發。 -
?
表示不指定值。使用的場景為不需要關心當前設置這個字段的值。例如:要在每月的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 * * ?
2. zone
時區,接收一個java.util.TimeZone#ID。cron表達式會基于該時區解析。默認是一個空字符串,即取服務器所在地的時區。比如我們一般使用的時區Asia/Shanghai。該字段我們一般留空。
3. fixedDelay
上一次執行完畢時間點之后多長時間再執行。如:
1
|
@Scheduled (fixedDelay = 5000 ) //上一次執行完畢時間點之后5秒再執行 |
4. fixedDelayString
與 3. fixedDelay 意思相同,只是使用字符串的形式。唯一不同的是支持占位符。如:
@Scheduled(fixedDelayString = "5000") //上一次執行完畢時間點之后5秒再執行
占位符的使用(配置文件中有配置:time.fixedDelay=5000):
1
2
3
4
|
@Scheduled (fixedDelayString = "${time.fixedDelay}" ) void testFixedDelayString() { System.out.println( "Execute at " + System.currentTimeMillis()); } |
運行結果:
5. fixedRate
上一次開始執行時間點之后多長時間再執行。如:
1
|
@Scheduled (fixedRate = 5000 ) //上一次開始執行時間點之后5秒再執行 |
6. fixedRateString
與 5. fixedRate 意思相同,只是使用字符串的形式。唯一不同的是支持占位符。
7. initialDelay
第一次延遲多長時間后再執行。如:
1
|
@Scheduled (initialDelay= 1000 , fixedRate= 5000 ) //第一次延遲1秒后執行,之后按fixedRate的規則每5秒執行一次 |
8. initialDelayString
與 7. initialDelay 意思相同,只是使用字符串的形式。唯一不同的是支持占位符。
That's all ! Thanks for reading.
附:
截至spring-context-4.3.14.RELEASE的源碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/** * 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注解使用誤區
強烈建議同胞們看。
在使用spring @Scheduled注解時很多人都為cron表達式無法進行配置進行煩惱吧,為何不像quartz般能在applicationContext中進行配置。
告訴大家其實是能applicationContext進行配置。
xml:
1
2
3
4
5
6
7
|
< context:annotation-config /> <!--spring掃描注解的配置 --> < context:component-scan base-package = "com.baodian.bdweb.timer" /> <!-- 開啟這個配置,spring才能識別@Scheduled注解 --> < task:annotation-driven scheduler = "qbScheduler" mode = "proxy" /> < task:scheduler id = "qbScheduler" pool-size = "10" /> |
java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@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())); } } |
上面這一種方式是大家最頭痛的問題吧,如果測試人員需要測試這個任務,你需要不停生成class文件給測試人員替換,是不是非常郁悶,其實我前期也被逼瘋了。我前期一直使用的Quartz,因此對該注解不是非常熟悉,趁著這個閑時想看看這個注解,結果一百度,卻找不到想要的結果。。換上我的谷歌繼續,功夫不負有心人,找到了一篇該注解的詳解,因此有了這一篇文章。廢話不多說,看原子彈來了!
xml:
1
2
3
4
5
6
7
8
9
10
|
< 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:
1
2
3
4
5
6
7
8
9
|
@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())); } } |
1
2
3
4
5
6
7
8
9
|
@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())); } } |
是不是很簡單,嘿嘿嘿!原子彈就造到這里了,配置很簡單在這里就不細說了,主要就是想吐槽一下百度,嘿嘿嘿!
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/1defb0f22ed1