quartz時(shí)間表達(dá)式Cron詳解
cron的表達(dá)式被用來(lái)配置CronTrigger實(shí)例。
cron的表達(dá)式是字符串,實(shí)際上是由七子表達(dá)式,描述個(gè)別細(xì)節(jié)的時(shí)間表。
這些子表達(dá)式是分開(kāi)的空白,代表:
- Seconds
- Minutes
- Hours
- Day-of-Month
- Month
- Day-of-Week
- Year (可選字段)
例? "0 0 12 ? * WED" 在每星期三下午12:00 執(zhí)行,個(gè)別子表達(dá)式可以包含范圍;
例如,在前面的例子里("WED")可以替換成 "MON-FRI", "MON, WED, FRI"甚至"MON-WED,SAT".“*” 代表整個(gè)時(shí)間段.
每一個(gè)字段都有一套可以指定有效值,如
Seconds (秒) :可以用數(shù)字0-59 表示, Minutes(分) :可以用數(shù)字0-59 表示, Hours(時(shí)) :可以用數(shù)字0-23表示, Day-of-Month(天) :可以用數(shù)字1-31 中的任一一個(gè)值,但要注意一些特別的月份 Month(月) :可以用0-11 或用字符串 “JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC” 表示 Day-of-Week(每周) :可以用數(shù)字1-7表示(1 = 星期日)或用字符口串“SUN, MON, TUE, WED, THU, FRI and SAT”表示 “*”:指定所有的值,比如,Minutes 設(shè)置為 *,表示每分鐘 ? “/”:為特別單位,表示為“每”如“0/15”表示每隔15分鐘執(zhí)行一次,“0”表示為從“0”分開(kāi)始, “3/20”表示表示每隔20分鐘執(zhí)行一次,“3”表示從第3分鐘開(kāi)始執(zhí)行 “?”:表示每月的某一天,或第周的某一天 “L”:用于每月,或每周,表示為每月的最后一天,或每個(gè)月的最后星期幾如“6L”表示“每月的最后一個(gè)星期五” “W”:表示為最近工作日,如“15W”放在每月(day-of-month)字段上表示為“到本月15日最近的工作日” “#”:是用來(lái)指定“的”每月第n個(gè)工作日,例 在每周(day-of-week)這個(gè)字段中內(nèi)容為"6#3" or "FRI#3" 則表示“每月第三個(gè)星期五”
注意:? Day-of-Month 和 Day-of-Week不可同時(shí)為 *
,否則報(bào)錯(cuò):
java.lang.RuntimeException: CronExpression '0/2 * * * * * *' is invalid. at org.quartz.CronScheduleBuilder.cronSchedule(CronScheduleBuilder.java:111) at quartz.QuartzExample.getTrigger(QuartzExample.java:110) at quartz.QuartzExample.run(QuartzExample.java:74) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.text.ParseException: Support for specifying both a day-of-week AND a day-of-month parameter is not implemented. at org.quartz.CronExpression.buildExpression(CronExpression.java:511) at org.quartz.CronExpression.<init>(CronExpression.java:276) at org.quartz.CronScheduleBuilder.cronSchedule(CronScheduleBuilder.java:107) ... 25 more Disconnected from the target VM, address: '127.0.0.1:61437', transport: 'socket' Process finished with exit code -1
CronExpression 源碼:
protected void buildExpression(String expression) throws ParseException { expressionParsed = true; try { if (seconds == null) { seconds = new TreeSet<Integer>(); } if (minutes == null) { minutes = new TreeSet<Integer>(); } if (hours == null) { hours = new TreeSet<Integer>(); } if (daysOfMonth == null) { daysOfMonth = new TreeSet<Integer>(); } if (months == null) { months = new TreeSet<Integer>(); } if (daysOfWeek == null) { daysOfWeek = new TreeSet<Integer>(); } if (years == null) { years = new TreeSet<Integer>(); } int exprOn = SECOND; StringTokenizer exprsTok = new StringTokenizer(expression, " \t", false); while (exprsTok.hasMoreTokens() && exprOn <= YEAR) { String expr = exprsTok.nextToken().trim(); // throw an exception if L is used with other days of the month if(exprOn == DAY_OF_MONTH && expr.indexOf('L') != -1 && expr.length() > 1 && expr.contains(",")) { throw new ParseException("Support for specifying 'L' and 'LW' with other days of the month is not implemented", -1); } // throw an exception if L is used with other days of the week if(exprOn == DAY_OF_WEEK && expr.indexOf('L') != -1 && expr.length() > 1 && expr.contains(",")) { throw new ParseException("Support for specifying 'L' with other days of the week is not implemented", -1); } if(exprOn == DAY_OF_WEEK && expr.indexOf('#') != -1 && expr.indexOf('#', expr.indexOf('#') +1) != -1) { throw new ParseException("Support for specifying multiple \"nth\" days is not implemented.", -1); } StringTokenizer vTok = new StringTokenizer(expr, ","); while (vTok.hasMoreTokens()) { String v = vTok.nextToken(); storeExpressionVals(0, v, exprOn); } exprOn++; } if (exprOn <= DAY_OF_WEEK) { throw new ParseException("Unexpected end of expression.", expression.length()); } if (exprOn <= YEAR) { storeExpressionVals(0, "*", YEAR); } TreeSet<Integer> dow = getSet(DAY_OF_WEEK); TreeSet<Integer> dom = getSet(DAY_OF_MONTH); // Copying the logic from the UnsupportedOperationException below boolean dayOfMSpec = !dom.contains(NO_SPEC); boolean dayOfWSpec = !dow.contains(NO_SPEC); if (!dayOfMSpec || dayOfWSpec) { if (!dayOfWSpec || dayOfMSpec) { throw new ParseException( "Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.", 0); } } } catch (ParseException pe) { throw pe; } catch (Exception e) { throw new ParseException("Illegal cron expression format (" + e.toString() + ")", 0); } } CronExpression 部分代碼
1)Cron表達(dá)式的格式:
秒 分 時(shí) 日 月 周 年(可選)。
字段名 | 允許的值 | 允許的特殊字符 |
---|---|---|
秒 | 0-59 | , - * / |
分 | 0-59 | , - * / |
小時(shí) | 0-23 | , - * / |
日 | 1-31 | , - * ? / L W C |
月 | 1-12 or JAN-DEC | , - * / |
周幾 | 1-7 or SUN-SAT | , - * ? / L C # |
年(可選字段) | empty, 1970-2099 | , - * / |
- “?”字符:表示不確定的值
- “,”字符:指定數(shù)個(gè)值
- “-”字符:指定一個(gè)值的范圍
- “/”字符:指定一個(gè)值的增加幅度。n/m表示從n開(kāi)始,每次增加m
- “L”字符:用在日表示一個(gè)月中的最后一天,用在周表示該月最后一個(gè)星期X
- “W”字符:指定離給定日期最近的工作日(周一到周五)
- “#”字符:表示該月第幾個(gè)周X。6#3表示該月第3個(gè)周五
2)Cron表達(dá)式范例:
每隔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 * * ? 每天的7點(diǎn)到21點(diǎn)都執(zhí)行一次:0 0 7-21 * * ?
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
HandlerMapping之RequestMappingHandlerMapping作用詳解
這篇文章主要介紹了HandlerMapping之RequestMappingHandlerMapping作用詳解,HandlerMapping是用來(lái)尋找Handler的,并不與Handler的類(lèi)型或者實(shí)現(xiàn)綁定,而是根據(jù)需要定義的,那么為什么要單獨(dú)給@RequestMapping實(shí)現(xiàn)一個(gè)HandlerMapping,需要的朋友可以參考下2023-10-10淺談MyBatis循環(huán)Map(高級(jí)用法)
這篇文章主要介紹了淺談MyBatis循環(huán)Map(高級(jí)用法),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Java中將接口返回的字節(jié)串轉(zhuǎn)為文件詳解
這篇文章主要給大家介紹了關(guān)于Java中將接口返回的字節(jié)串轉(zhuǎn)為文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-11-11Java?HashTable與Collections.synchronizedMap源碼深入解析
HashTable是jdk?1.0中引入的產(chǎn)物,基本上現(xiàn)在很少使用了,但是會(huì)在面試中經(jīng)常被問(wèn)到。本文就來(lái)帶大家一起深入了解一下Hashtable,需要的可以參考一下2022-11-11詳解Spring Security的formLogin登錄認(rèn)證模式
對(duì)于一個(gè)完整的應(yīng)用系統(tǒng),與登錄驗(yàn)證相關(guān)的頁(yè)面都是高度定制化的,非常美觀而且提供多種登錄方式。這就需要Spring Security支持我們自己定制登錄頁(yè)面,也就是本文給大家介紹的formLogin模式登錄認(rèn)證模式,感興趣的朋友跟隨小編一起看看吧2019-11-11詳解如何判斷Java線(xiàn)程池任務(wù)已執(zhí)行完
線(xiàn)程池的使用并不復(fù)雜,麻煩的是如何判斷線(xiàn)程池中的任務(wù)已經(jīng)全部執(zhí)行完了,所以接下來(lái),我們就來(lái)看看如何判斷線(xiàn)程中的任務(wù)是否已經(jīng)全部執(zhí)行完吧2023-08-08Java實(shí)現(xiàn)的漢語(yǔ)拼音工具類(lèi)完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的漢語(yǔ)拼音工具類(lèi),結(jié)合完整實(shí)例形式分析了java基于pinyin4j包實(shí)現(xiàn)編碼轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11JDK17、JDK19、JDK1.8輕松切換(無(wú)坑版,小白也可以看懂!)
在做不同的java項(xiàng)目時(shí)候,因項(xiàng)目需要很可能來(lái)回切換jdk版本,下面這篇文章主要介紹了JDK17、JDK19、JDK1.8輕松切換的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02java二叉樹(shù)的幾種遍歷遞歸與非遞歸實(shí)現(xiàn)代碼
這篇文章主要介紹了java二叉樹(shù)的幾種遍歷遞歸與非遞歸實(shí)現(xiàn)代碼,需要的朋友可以參考下2020-12-12