Cron 表達(dá)式是表示時(shí)間周期的字符串,常用于各種定時(shí)任務(wù)解決方案蠕蚜,本文中代碼示例基于 Spring @Scheduled 定時(shí)器
語(yǔ)法格式
Cron 字符串包含 6 或 7 個(gè)域麸塞,域之間使用空格分隔。
包含 6 個(gè)域的 Cron 表達(dá)式語(yǔ)法格式:
Seconds Minutes Hours DayofMonth Month DayofWeek
包含 7 個(gè)域的 Cron 表達(dá)式語(yǔ)法格式:
Seconds Minutes Hours DayofMonth Month DayofWeek Year
有效字符范圍
每個(gè)域可使用的有效字符包括:
Seconds:0~59 的整數(shù),或
,
-
*
/
四個(gè)字符
Minutes:同 Seconds 域一致
Hours:0~23的整數(shù)兴垦,或,
-
*
/
四個(gè)字符
DayofMonth:0~31的整數(shù),或,
-
*
/
?
L
W
C
八個(gè)字符
Month:1~12的整數(shù)字柠,或 JAN ~ DEC探越,或,
-
*
/
四個(gè)字符
DayofWeek:1~7的整數(shù),或 SUN ~ SAT窑业,或,
-
*
/
?
L
C
#
八個(gè)字符钦幔,注意整數(shù) 1 代表星期日,每周第一天從周日開(kāi)始
Year:1970~2099常柄,或,
-
*
/
四個(gè)字符
特殊字符說(shuō)明
(1) ,
用于分隔枚舉值鲤氢,如在 Seconds 域使用 10,15,25
表示在第 10 秒、15 秒和 25 秒觸發(fā)一次西潘,示例代碼如下:
@Scheduled(cron = "10,15,25 * * * * ?")
public void scheduledTask() {
System.out.println("Task executed at " + LocalDateTime.now());
}
運(yùn)行日志:
2018-08-01 22:42:53.322 INFO 18140 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Starting DemoSpringBootScheduledApplication on LAPTOP-C375ASPB with PID 18140 (D:\JYL\DEV\IdeaProjects\demo\demo-spring-boot-scheduled\target\classes started by Ji in D:\JYL\DEV\IdeaProjects\demo)
2018-08-01 22:42:53.326 INFO 18140 --- [ main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
2018-08-01 22:42:53.382 INFO 18140 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@77e4c80f: startup date [Wed Aug 01 22:42:53 CST 2018]; root of context hierarchy
2018-08-01 22:42:54.517 INFO 18140 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-08-01 22:42:54.538 INFO 18140 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
2018-08-01 22:42:54.563 INFO 18140 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Started DemoSpringBootScheduledApplication in 1.521 seconds (JVM running for 1.976)
Task executed at 2018-08-01T22:43:10.007
Task executed at 2018-08-01T22:43:15.001
Task executed at 2018-08-01T22:43:25.001
Task executed at 2018-08-01T22:44:10.001
Task executed at 2018-08-01T22:44:15.001
Task executed at 2018-08-01T22:44:25.001
Task executed at 2018-08-01T22:45:10.001
Task executed at 2018-08-01T22:45:15.001
Task executed at 2018-08-01T22:45:25.001
......
(2) -
用于表示范圍卷玉,如在 Seconds 域使用 30-45
表示在第 30 秒至 45 秒范圍內(nèi),每秒觸發(fā)一次喷市,示例代碼如下:
@Scheduled(cron = "30-45 * * * * ?")
public void scheduledTask() {
System.out.println("Task executed at " + LocalDateTime.now());
}
運(yùn)行日志:
2018-08-01 22:46:51.549 INFO 11236 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Starting DemoSpringBootScheduledApplication on LAPTOP-C375ASPB with PID 11236 (D:\JYL\DEV\IdeaProjects\demo\demo-spring-boot-scheduled\target\classes started by Ji in D:\JYL\DEV\IdeaProjects\demo)
2018-08-01 22:46:51.552 INFO 11236 --- [ main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
2018-08-01 22:46:51.600 INFO 11236 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@b7f23d9: startup date [Wed Aug 01 22:46:51 CST 2018]; root of context hierarchy
2018-08-01 22:46:52.524 INFO 11236 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-08-01 22:46:52.544 INFO 11236 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
2018-08-01 22:46:52.566 INFO 11236 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Started DemoSpringBootScheduledApplication in 1.29 seconds (JVM running for 1.72)
Task executed at 2018-08-01T22:47:30.007
Task executed at 2018-08-01T22:47:31.002
Task executed at 2018-08-01T22:47:32.001
Task executed at 2018-08-01T22:47:33.001
Task executed at 2018-08-01T22:47:34.001
Task executed at 2018-08-01T22:47:35.001
Task executed at 2018-08-01T22:47:36.001
Task executed at 2018-08-01T22:47:37.001
Task executed at 2018-08-01T22:47:38.002
Task executed at 2018-08-01T22:47:39.001
Task executed at 2018-08-01T22:47:40.001
Task executed at 2018-08-01T22:47:41
Task executed at 2018-08-01T22:47:42.002
Task executed at 2018-08-01T22:47:43.002
Task executed at 2018-08-01T22:47:44.001
Task executed at 2018-08-01T22:47:45.002
Task executed at 2018-08-01T22:48:30.001
Task executed at 2018-08-01T22:48:31
Task executed at 2018-08-01T22:48:32.002
Task executed at 2018-08-01T22:48:33.001
Task executed at 2018-08-01T22:48:34.001
Task executed at 2018-08-01T22:48:35.001
Task executed at 2018-08-01T22:48:36.001
Task executed at 2018-08-01T22:48:37.001
Task executed at 2018-08-01T22:48:38.002
Task executed at 2018-08-01T22:48:39.001
Task executed at 2018-08-01T22:48:40.001
Task executed at 2018-08-01T22:48:41.001
Task executed at 2018-08-01T22:48:42.001
Task executed at 2018-08-01T22:48:43.002
Task executed at 2018-08-01T22:48:44
Task executed at 2018-08-01T22:48:45.001
......
(3) *
匹配所在域有效范圍內(nèi)的任意值相种,如在 Minutes 域使用則每分鐘觸發(fā)一次,示例代碼如下:
@Scheduled(cron = "0 * * * * ?")
public void scheduledTask() {
System.out.println("Task executed at " + LocalDateTime.now());
}
運(yùn)行日志:
2018-08-01 22:50:18.396 INFO 12632 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Starting DemoSpringBootScheduledApplication on LAPTOP-C375ASPB with PID 12632 (D:\JYL\DEV\IdeaProjects\demo\demo-spring-boot-scheduled\target\classes started by Ji in D:\JYL\DEV\IdeaProjects\demo)
2018-08-01 22:50:18.402 INFO 12632 --- [ main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
2018-08-01 22:50:18.491 INFO 12632 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@709ba3fb: startup date [Wed Aug 01 22:50:18 CST 2018]; root of context hierarchy
2018-08-01 22:50:20.218 INFO 12632 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-08-01 22:50:20.260 INFO 12632 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
2018-08-01 22:50:20.288 INFO 12632 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Started DemoSpringBootScheduledApplication in 2.406 seconds (JVM running for 3.198)
Task executed at 2018-08-01T22:51:00.007
Task executed at 2018-08-01T22:52:00.001
Task executed at 2018-08-01T22:53:00.001
......
(4) /
表示從起始時(shí)間開(kāi)始品姓,每隔固定時(shí)間觸發(fā)一次寝并,如在 Seconds 域使用 10/15
表示每分鐘內(nèi)第 10 秒觸發(fā)以后,后續(xù)每隔 15 秒觸發(fā)一次腹备,即每分鐘第 10 秒食茎、第 25 秒、第 40 秒和第 55 秒各觸發(fā)一次馏谨。注意:這種間隔循環(huán)只在每分鐘內(nèi)執(zhí)行别渔,即第 55 秒時(shí)中斷每 15 秒的間隔循環(huán),等到下一分鐘的第 10 秒再次執(zhí)行。
@Scheduled(cron = "10/15 * * * * ?")
public void scheduledTask() {
System.out.println("Task executed at " + LocalDateTime.now());
}
運(yùn)行日志:
2018-08-01 22:56:12.082 INFO 14064 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Starting DemoSpringBootScheduledApplication on LAPTOP-C375ASPB with PID 14064 (D:\JYL\DEV\IdeaProjects\demo\demo-spring-boot-scheduled\target\classes started by Ji in D:\JYL\DEV\IdeaProjects\demo)
2018-08-01 22:56:12.085 INFO 14064 --- [ main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
2018-08-01 22:56:12.131 INFO 14064 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1dd92fe2: startup date [Wed Aug 01 22:56:12 CST 2018]; root of context hierarchy
2018-08-01 22:56:12.962 INFO 14064 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-08-01 22:56:12.976 INFO 14064 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
2018-08-01 22:56:12.988 INFO 14064 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Started DemoSpringBootScheduledApplication in 1.191 seconds (JVM running for 1.68)
Task executed at 2018-08-01T22:56:25.005
Task executed at 2018-08-01T22:56:40.001
Task executed at 2018-08-01T22:56:55.001
Task executed at 2018-08-01T22:57:10.001
Task executed at 2018-08-01T22:57:25.002
Task executed at 2018-08-01T22:57:40.002
Task executed at 2018-08-01T22:57:55
Task executed at 2018-08-01T22:58:10.002
......
(5) ?
只能用于 DayofMonth
和 DayofWeek
這兩個(gè)域哎媚,表示匹配任意值喇伯,如 DayofMonth
域使用固定值 10(表示每月第 10 天觸發(fā)),則 DayofWeek
使用 ?
拨与,示例代碼如下:
@Scheduled(cron = "* * * 10 * ?")
public void scheduledTask() {
System.out.println("Task executed at " + LocalDateTime.now());
}