`

spring quartz cronExpression配置实例及说明

阅读更多

 

转载地址:http://faceye.com/haipeng/article/40288083252175550125217f5dd90009.html

 

在quartz跟spring进行完美整合以后, 越来越多后台工作任务,交给了quartz

在quartz中,一项比较关键的配置是cronExpression的配置

它直接决定着任务的执行频率以及执行规则.

看看对cronExpression的配置说明:

字段   允许值   允许的特殊字符
  0-59   , - * /
  0-59   , - * /
小时   0-23   , - * /
日期   1-31   , - * ? / L W C
月份   1-12 或者 JAN-DEC   , - * /
星期   1-7 或者 SUN-SAT   , - * ? / L C #
年(可选)   留空, 1970-2099   , - * /

看看官方文档对此是怎么说的:

“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。

“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。
月份中的日期和星期中的日期这两个元素时互斥的一起应该通过设置一个问号(?)来表明不想设置那个字段


“-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。

“,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.
The '/' character is used to specify increments. For example "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". Specifying '*' before the '/' is equivalent to specifying 0 is the value to start with. Essentially, for each field in the expression, there is a set of numbers that can be turned on or off. For seconds and minutes, the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to 31, and for months 1 to 12. The "/" character simply helps you turn on every "nth" value in the given set. Thus "7/6" in the month field only turns on month "7", it does NOT mean every 6th month, please note that subtlety.

The 'L' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "last", but it has different meaning in each of the two fields. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.

L是‘last’的省略写法可以表示day-of-month和day-of-week域,但在两个字段中的意思不同,例如day-of-month域中表示一个月的最后一天,
如果在day-of-week域表示‘7’或者‘SAT’,如果在day-of-week域中前面加上数字,它表示一个月的最后几天,例如‘6L’就表示一个月的最后一个
星期五,

The 'W' character is allowed for the day-of-month field. This character is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month". So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify "1W" as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can only be specified when the day-of-month is a single day, not a range or list of days
.
The 'L' and 'W' characters can also be combined for the day-of-month expression to yield 'LW', which translates to "last weekday of the month".

The '#' character is allowed for the day-of-week field. This character is used to specify "the nth" XXX day of the month. For example, the value of "6#3" in the day-of-week field means the third Friday of the month (day 6 = Friday and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month.

The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".

表达式意义     
"0 0 12 * * ?"  每天中午 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 期间的每 1 分钟触发     
"0 0/5 14 * * ?"  在每天下午 2 点到下午 2 : 55 期间的每 5 分钟触发     
"0 0/5 14,18 * * ?"  在每天下午 2 点到 2 : 55 期间和下午 6 点到 6 : 55 期间的每 5 分钟触发     
"0 0-5 14 * * ?"  在每天下午 2 点到下午 2 : 05 期间的每 1 分钟触发     
"0 10,44 14 ? 3 WED"  每年三月的星期三的下午 2 : 10 和 2 : 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 触发     
每天早上6 点     
0   6  * * *     
每两个小时     
0  */ 2  * * *     
晚上11 点到早上 8 点之间每两个小时,早上八点     
0   23 - 7 / 2 , 8  * * *     
每个月的4 号和每个礼拜的礼拜一到礼拜三的早上 11 点     
0   11   4  *  1 - 3      
1 月 1 日早上 4 点     
0   4   1   1  *   

分享到:
评论

相关推荐

    Spring + Quartz配置实例(转载)

    标题 "Spring + Quartz配置实例(转载)" 暗示了我们将讨论如何在Java环境中整合Spring框架与Quartz作业调度库的使用。Quartz是一个开源的作业调度库,它允许开发者安排任务以在特定时间执行,而Spring框架则是一个...

    Spring线程时间配置:Quartz和CronExpression

    ### Spring线程时间配置:Quartz与CronExpression详解 在Spring框架中,为了实现定时任务的调度,引入了一个强大的工具——Quartz。Quartz是一个功能丰富的开源作业调度器,可以用于Java应用程序中的作业调度。它...

    spring-quartz简单实例

    在这个"spring-quartz简单实例"中,我们将探讨如何在Spring环境下配置和使用Quartz。 首先,我们需要引入Quartz和Spring的依赖。在Maven项目中,这通常通过在pom.xml文件中添加以下依赖来完成: ```xml ...

    Quartz在Spring中动态设置cronExpression

    Spring提供了一套机制来创建、管理和销毁Quartz中的Scheduler实例、Jobs和Triggers,这使得开发人员无需直接处理复杂的Quartz配置代码,而是可以通过XML或Java配置来声明式地定义这些组件。 #### 2. CronTrigger与...

    Spring Quartz 任务调度实例

    Spring Quartz实例提供了灵活的任务调度能力,适用于各种业务场景,如定期数据同步、定时发送邮件等。通过上述步骤,你可以创建自己的定时任务,并根据项目需求调整触发策略。这个实例包含了完整的配置和示例代码,...

    关于spring中quartz的配置

    在Spring配置文件(如`applicationContext.xml`)中,定义`SchedulerFactoryBean`来实例化和配置Quartz Scheduler: ```xml <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz....

    Spring中Quartz的配置实例

    在Spring框架中集成Quartz是一款常见的任务...在Spring中配置Quartz,首先需要在项目的类路径下添加Quartz库,包括`quartz.jar`和对应的数据库驱动。接着,在Spring配置文件中声明Scheduler工厂Bean,例如: ```xml ...

    spring集成quartz集群配置

    1. **并发控制**:Quartz允许配置每个作业的最大并发实例数,以避免多个实例同时执行同一作业。 2. **故障恢复策略**:通过配置`org.quartz.jobStore.isClustered=true`,确保集群节点间的心跳和失败检测。 3. **...

    spring quartz定时任务demo

    本文将详细解析“spring quartz定时任务demo”这个项目,它提供了实现Java定时任务的实例,便于快速理解和应用。 首先,Quartz是一个开源的作业调度框架,支持复杂的调度策略,可以创建、调度和执行重复或一次性...

    Spring quartz 定时 实例

    在上述实例中,我们看到如何配置和使用Spring Quartz来创建一个简单的定时任务。主要涉及以下几个关键点: 1. **依赖引入**:首先,我们需要引入必要的库,包括quartz.jar(Quartz核心库),spring.jar(Spring核心...

    Spring中Quartz的配置

    ### Spring中Quartz的配置详解 #### 一、前言 Spring框架因其强大的功能和灵活性在企业级应用开发中占据了一席之地。其中,对于定时任务的支持,Spring结合了Quartz这一优秀的开源任务调度框架,提供了更为灵活和...

    spring多个定时任务quartz配置

    4. **声明JobDetail**:在Spring配置文件中声明JobDetail,这会告诉Quartz如何实例化和执行Job。例如: ```xml <bean id="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> ...

    Spring整合Quartz定时任务实例

    当Spring与Quartz结合时,可以利用Spring的强大功能来管理和配置Quartz作业,使定时任务的管理变得更加简单和高效。 在Spring 4.2.1.RELEASE版本中,整合Quartz的过程主要涉及以下几个步骤: 1. **添加依赖**:...

    转:spring多个定时任务quartz配置

    而Spring的配置则负责任务的实例化、触发规则以及与Quartz的集成。 在实际开发中,可能还需要考虑任务的并发控制、异常处理、任务间的依赖关系等问题。例如,你可以通过`DisallowConcurrentExecution`注解确保同一...

    Spring quartz定时调度jar包

    将Spring与Quartz结合,可以方便地在Spring应用中配置和管理定时任务。首先,你需要在Spring的配置文件中声明一个`SchedulerFactoryBean`,这将初始化并管理Quartz调度器。然后,你可以定义`JobDetail`和`Trigger`,...

    spring quartz通过xml配置实现定时任务demo

    在Spring的配置文件(如`applicationContext.xml`)中,你需要声明一个`SchedulerFactoryBean`,它负责初始化和管理Quartz的`Scheduler`实例。同时,定义一个`JobDetailBean`来描述你的Job,并创建一个`...

    maven_spring_quartz.rar

    本教程将深入探讨如何在Spring项目中集成Quartz,通过实例解析实现过程。 Quartz是Java平台上的一款强大且灵活的作业调度框架,它支持复杂的调度逻辑,可以创建简单或复杂的任务调度。Spring则是一款全面的企业级...

    SpringQuartz定时器的配置[归类].pdf

    最后,配置`SchedulerFactoryBean`,这是Spring提供的一个工厂bean,用于创建并管理Quartz的Scheduler实例。你可以将之前定义的`JobDetailBean`和`CronTriggerBean`引用到`SchedulerFactoryBean`的`triggers`属性中...

Global site tag (gtag.js) - Google Analytics