项目中经常要使用到定时器,其中CronExpression配置非常重要。现在就配置说明详细解说一下:

CronExpression表达式是由6个必需字段(秒、分、时、日、月、周)和一个可选字段(年)通过空格组成。

cronExpression表达式组成说明
序号 字段名 允许值 允许特殊字符
1 0-59 , - * /
2 0-59 , - * /
3 0-23 , - * /
4 1-31 , - * ? / L W
5 1-12 or JAN-DEC , - * /
6 1-7 or SUN-SAT , - * ? / L #
7 年(可选字段) empty, 1970-2199 , - * /

 

 

 

 

 

 

 

 

 

 

 

 

 

 

下面对特殊字符表达意思进行解说:

* 代表所有值,比如一分钟里代表每一个分钟。

? 只允许使用在日和周的表达式中,代表不定值。使用的场景为不需要关心当前设置这个字段的值,例如:要在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为"?" 具体设置为 0 0 0 10 * ?

-  代表在一个范围里,比如10-12  代表10、11、12

,代表指定多个值,比如 在周字段配置 1,3,5 代表周一、周三、周五

/  代表递增触发,比如在秒字段 0/15  代表0,15,30,45  每15秒执行一次。 5/15代表 5,20,35,50

L  只允许使用在日和周的表达式中,L是last的简称,配置在日中代表一个月的最后一天,比如1月则为31日,2月非闰年则为28日,闰年为29日那天。  

    配置在周字段里,代表7或者星期六。6L代表这个月的最后一个星期五。

W 只允许使用在日字段里,代表最近的工作日(周一到周五),比如15w, 这个月的15号是星期六,则14号(星期五)触发,如果是星期日,则16号(星期一)触发,如果是星期二,则星期二触发。

LW也可以联合使用,如果配置LW,则代表这个月最后的工作日。

# 值允许使用在周字段里,比如6#3 代表这个月的第三个星期五 (6代表星期五,#3代表这个月的第三个)

注意:月和周字段是不区分大小写(JAN Jan  MON mon)

          配置指定日和周字段里,不完整,需要在其中一个用?代替配置。

         溢出范围是支持的,但是溢出范围过大可能会出问题。比如配置22-2  晚上10点到凌晨2点, NOV-FEB 代表11月-2月。这个是支持的,但14-6,这个就溢出太对,可能会出问题。

 

下面就例子进行说明:

0 0 12 * * ?   每天中午十二点触发

0 15 10 ? * *   每天早上10:15触发 

0 15 10 * * ?  每天早上10:15触发

0 15 10 * * ? *   每天早上10:15触发

0 15 10 * * ? 2005   2005年的每天早上10:15触发

0 * 14 * * ?   每天从下午2点开始到2点59分每分钟一次触发

0 0/5 14 * * ?  每天从下午2点开始到2:55分结束每5分钟一次触发

0 0/5 14,18 * * ?  每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 

0 0-5 14 * * ?  每天14:00至14:05每分钟一次触发

0 10,44 14 ? 3 WED   三月的每周三的14:10和14:44触发 

0 15 10 ? * MON-FRI   每个周一、周二、周三、周四、周五的10:15触发 

 0 15 10 15 * ?     每月15号上午10点15分触发

0 15 10 L * ?   每月最后一天的10点15分触发 

0 15 10 ? * 6L  每月最后一周的星期五的10点15分触发

0 15 10 ? * 6L 2002-2005    从2002年到2005年每月最后一周的星期五的10点15分触发

0 15 10 ? * 6#3  每月的第三周的星期五10点15分触发开始触发 

 

具体可参照quartz源码

CronTrigger类

  1 /*
  2  * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
  3  * 
  4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  5  * use this file except in compliance with the License. You may obtain a copy 
  6  * of the License at 
  7  * 
  8  *   http://www.apache.org/licenses/LICENSE-2.0 
  9  *   
 10  * Unless required by applicable law or agreed to in writing, software 
 11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 13  * License for the specific language governing permissions and limitations 
 14  * under the License.
 15  * 
 16  */
 17 
 18 package org.quartz;
 19 
 20 import java.util.Calendar;
 21 import java.util.TimeZone;
 22 
 23 /**
 24  * The public interface for inspecting settings specific to a CronTrigger, .
 25  * which is used to fire a <code>{@link org.quartz.Job}</code>
 26  * at given moments in time, defined with Unix 'cron-like' schedule definitions.
 27  * 
 28  * <p>
 29  * For those unfamiliar with "cron", this means being able to create a firing
 30  * schedule such as: "At 8:00am every Monday through Friday" or "At 1:30am
 31  * every last Friday of the month".
 32  * </p>
 33  * 
 34  * <p>
 35  * The format of a "Cron-Expression" string is documented on the 
 36  * {@link org.quartz.CronExpression} class.
 37  * </p>
 38  * 
 39  * <p>
 40  * Here are some full examples: <br><table cellspacing="8">
 41  * <tr>
 42  * <th align="left">Expression</th>
 43  * <th align="left">&nbsp;</th>
 44  * <th align="left">Meaning</th>
 45  * </tr>
 46  * <tr>
 47  * <td align="left"><code>"0 0 12 * * ?"</code></td>
 48  * <td align="left">&nbsp;</th>
 49  * <td align="left"><code>Fire at 12pm (noon) every day</code></td>
 50  * </tr>
 51  * <tr>
 52  * <td align="left"><code>"0 15 10 ? * *"</code></td>
 53  * <td align="left">&nbsp;</th>
 54  * <td align="left"><code>Fire at 10:15am every day</code></td>
 55  * </tr>
 56  * <tr>
 57  * <td align="left"><code>"0 15 10 * * ?"</code></td>
 58  * <td align="left">&nbsp;</th>
 59  * <td align="left"><code>Fire at 10:15am every day</code></td>
 60  * </tr>
 61  * <tr>
 62  * <td align="left"><code>"0 15 10 * * ? *"</code></td>
 63  * <td align="left">&nbsp;</th>
 64  * <td align="left"><code>Fire at 10:15am every day</code></td>
 65  * </tr>
 66  * <tr>
 67  * <td align="left"><code>"0 15 10 * * ? 2005"</code></td>
 68  * <td align="left">&nbsp;</th>
 69  * <td align="left"><code>Fire at 10:15am every day during the year 2005</code>
 70  * </td>
 71  * </tr>
 72  * <tr>
 73  * <td align="left"><code>"0 * 14 * * ?"</code></td>
 74  * <td align="left">&nbsp;</th>
 75  * <td align="left"><code>Fire every minute starting at 2pm and ending at 2:59pm, every day</code>
 76  * </td>
 77  * </tr>
 78  * <tr>
 79  * <td align="left"><code>"0 0/5 14 * * ?"</code></td>
 80  * <td align="left">&nbsp;</th>
 81  * <td align="left"><code>Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day</code>
 82  * </td>
 83  * </tr>
 84  * <tr>
 85  * <td align="left"><code>"0 0/5 14,18 * * ?"</code></td>
 86  * <td align="left">&nbsp;</th>
 87  * <td align="left"><code>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</code>
 88  * </td>
 89  * </tr>
 90  * <tr>
 91  * <td align="left"><code>"0 0-5 14 * * ?"</code></td>
 92  * <td align="left">&nbsp;</th>
 93  * <td align="left"><code>Fire every minute starting at 2pm and ending at 2:05pm, every day</code>
 94  * </td>
 95  * </tr>
 96  * <tr>
 97  * <td align="left"><code>"0 10,44 14 ? 3 WED"</code></td>
 98  * <td align="left">&nbsp;</th>
 99  * <td align="left"><code>Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.</code>
100  * </td>
101  * </tr>
102  * <tr>
103  * <td align="left"><code>"0 15 10 ? * MON-FRI"</code></td>
104  * <td align="left">&nbsp;</th>
105  * <td align="left"><code>Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday</code>
106  * </td>
107  * </tr>
108  * <tr>
109  * <td align="left"><code>"0 15 10 15 * ?"</code></td>
110  * <td align="left">&nbsp;</th>
111  * <td align="left"><code>Fire at 10:15am on the 15th day of every month</code>
112  * </td>
113  * </tr>
114  * <tr>
115  * <td align="left"><code>"0 15 10 L * ?"</code></td>
116  * <td align="left">&nbsp;</th>
117  * <td align="left"><code>Fire at 10:15am on the last day of every month</code>
118  * </td>
119  * </tr>
120  * <tr>
121  * <td align="left"><code>"0 15 10 ? * 6L"</code></td>
122  * <td align="left">&nbsp;</th>
123  * <td align="left"><code>Fire at 10:15am on the last Friday of every month</code>
124  * </td>
125  * </tr>
126  * <tr>
127  * <td align="left"><code>"0 15 10 ? * 6L"</code></td>
128  * <td align="left">&nbsp;</th>
129  * <td align="left"><code>Fire at 10:15am on the last Friday of every month</code>
130  * </td>
131  * </tr>
132  * <tr>
133  * <td align="left"><code>"0 15 10 ? * 6L 2002-2005"</code></td>
134  * <td align="left">&nbsp;</th>
135  * <td align="left"><code>Fire at 10:15am on every last Friday of every month during the years 2002, 2003, 2004 and 2005</code>
136  * </td>
137  * </tr>
138  * <tr>
139  * <td align="left"><code>"0 15 10 ? * 6#3"</code></td>
140  * <td align="left">&nbsp;</th>
141  * <td align="left"><code>Fire at 10:15am on the third Friday of every month</code>
142  * </td>
143  * </tr>
144  * </table>
145  * </p>
146  * 
147  * <p>
148  * Pay attention to the effects of '?' and '*' in the day-of-week and
149  * day-of-month fields!
150  * </p>
151  * 
152  * <p>
153  * <b>NOTES:</b>
154  * <ul>
155  * <li>Support for specifying both a day-of-week and a day-of-month value is
156  * not complete (you'll need to use the '?' character in on of these fields).
157  * </li>
158  * <li>Be careful when setting fire times between mid-night and 1:00 AM -
159  * "daylight savings" can cause a skip or a repeat depending on whether the
160  * time moves back or jumps forward.</li>
161  * </ul>
162  * </p>
163  * 
164  * @see CronScheduleBuilder
165  * @see TriggerBuilder
166  * 
167  * @author jhouse
168  * @author Contributions from Mads Henderson
169  */
170 public interface CronTrigger extends Trigger {
171 
172     public static final long serialVersionUID = -8644953146451592766L;
173     
174     /**
175      * <p>
176      * Instructs the <code>{@link Scheduler}</code> that upon a mis-fire
177      * situation, the <code>{@link CronTrigger}</code> wants to be fired now
178      * by <code>Scheduler</code>.
179      * </p>
180      */
181     public static final int MISFIRE_INSTRUCTION_FIRE_ONCE_NOW = 1;
182     
183     /**
184      * <p>
185      * Instructs the <code>{@link Scheduler}</code> that upon a mis-fire
186      * situation, the <code>{@link CronTrigger}</code> wants to have it's
187      * next-fire-time updated to the next time in the schedule after the
188      * current time (taking into account any associated <code>{@link Calendar}</code>,
189      * but it does not want to be fired now.
190      * </p>
191      */
192     public static final int MISFIRE_INSTRUCTION_DO_NOTHING = 2;
193 
194     public String getCronExpression();
195 
196     /**
197      * <p>
198      * Returns the time zone for which the <code>cronExpression</code> of
199      * this <code>CronTrigger</code> will be resolved.
200      * </p>
201      */
202     public TimeZone getTimeZone();
203 
204     public String getExpressionSummary();
205 
206     TriggerBuilder<CronTrigger> getTriggerBuilder();
207 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-10
  • 2021-12-05
  • 2021-10-04
  • 2022-12-23
  • 2021-09-13
  • 2021-09-24
猜你喜欢
  • 2021-11-10
  • 2022-12-23
  • 2021-09-29
  • 2021-09-24
  • 2022-02-22
  • 2021-09-29
  • 2021-09-11
相关资源
相似解决方案