cron 是一個已經存在了很長時間的 UNIX 工具,是以它的排程能力很強大 并得到證明。CronTrigger 類基于 cron 的排程功能。
CronTrigger 使用“cron 表達式”,它能夠建立觸發時間表,例如:“在上午 8:00 每個 周一至周五“或”每月最後一個星期五淩晨 1:30”。
cron 表達式很強大,但可能會非常令人困惑。你可以使用這個工具直接進行轉換
線上Cron表達式生成器 (qqe2.com)
https://cron.qqe2.com/
1、cron 表達式
文法:秒 分 時 日 月 周 年(Spring 不支援)
Cron Trigger TutorialA full-featured, Java-based, In-process job scheduler.
http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html
特殊字元: ,:枚舉;
(cron="7,9,23 * * * * ?"):任意時刻的 7,9,23 秒啟動這個任務;
-:範圍:
(cron="7-20 * * * * ?"):任意時刻的 7-20 秒之間,每秒啟動一次
*:任意; 指定位置的任意時刻都可以
/:步長;
(cron="7/5 * * * * ?"):第 7 秒啟動,每 5 秒一次;
(cron="*/5 * * * * ?"):任意秒啟動,每 5 秒一次; ?:(出現在日和周幾的位置):為了防止日和周沖突,在周和日上如果要寫通配符使 用? (cron="* * * 1 * ?"):每月的 1 号,啟動這個任務;
L:(出現在日和周的位置)”,
last:最後一個
(cron="* * * ? * 3L"):每月的最後一個周二
W:
Work Day:工作日
(cron="* * * W * ?"):每個月的工作日觸發
(cron="* * * LW * ?"):每個月的最後一個工作日觸發
#:第幾個
(cron="* * * ? * 5#2"):每個月的第 2 個周 4
2、cron 示例
0 0 12 * * ? | Fire at 12pm (noon) every day |
0 15 10 ? * * | Fire at 10:15am every day |
0 15 10 * * ? | Fire at 10:15am every day |
0 15 10 * * ? * | Fire at 10:15am every day |
0 15 10 * * ? 2005 | Fire at 10:15am every day during the year 2005 |
0 * 14 * * ? | Fire every minute starting at 2pm and ending at 2:59pm, every day |
0 0/5 14 * * ? | Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day |
0 0/5 14,18 * * ? | Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day |
0 0-5 14 * * ? | Fire every minute starting at 2pm and ending at 2:05pm, every day |
0 10,44 14 ? 3 WED | Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. |
0 15 10 ? * MON-FRI | Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday |
0 15 10 15 * ? | Fire at 10:15am on the 15th day of every month |
0 15 10 L * ? | Fire at 10:15am on the last day of every month |
0 15 10 L-2 * ? | Fire at 10:15am on the 2nd-to-last last day of every month |
0 15 10 ? * 6L | Fire at 10:15am on the last Friday of every month |
0 15 10 ? * 6L | Fire at 10:15am on the last Friday of every month |
0 15 10 ? * 6L 2002-2005 | Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005 |
0 15 10 ? * 6#3 | Fire at 10:15am on the third Friday of every month |
0 0 12 1/5 * ? | Fire at 12pm (noon) every 5 days every month, starting on the first day of the month. |
0 11 11 11 11 ? | Fire every November 11th at 11:11am. |