`
sundful
  • 浏览: 1233052 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

quartz的配置表达式

阅读更多

java服务自带了定时服务Timer,不过我在研究spring,所以就使用了quartz,因为spring良好的支持了quartz,使用起来还是相当的简单

Java代码 复制代码
  1. <!-- 实现规则服务实时加载的定时服务配置文件 -->   
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xmlns:tx="http://www.springframework.org/schema/tx"  
  7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">   
  10.        
  11.     <bean id="loadObject" class="com.sunyard.rule.global.ContextLoad"/>   
  12.        
  13.     <bean name="jobDetailBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">   
  14.         <property name="targetObject" ref="loadObject"/>   
  15.         <property name="targetMethod" value="load"/>   
  16.     </bean>   
  17.        
  18.     <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">   
  19.        <property name="jobDetail">   
  20.            <ref bean="jobDetailBean" />   
  21.        </property>   
  22.        <!--    
  23.             下面表示每天晚上21点执行   
  24.             配置具体方法见《定时服务配置说明》   
  25.        -->   
  26.        <property name="cronExpression">   
  27.            <value>0 0 21 * * ?</value>   
  28.        </property>   
  29.     </bean>   
  30.        
  31.     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">   
  32.         <property name="triggers">   
  33.             <list>   
  34.                 <ref bean="cronTrigger" />   
  35.             </list>   
  36.         </property>   
  37.     </bean>   
  38. </beans>  
<!-- 实现规则服务实时加载的定时服务配置文件 -->

<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
	<bean id="loadObject" class="com.sunyard.rule.global.ContextLoad"/>
	
	<bean name="jobDetailBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	  	<property name="targetObject" ref="loadObject"/>
  		<property name="targetMethod" value="load"/>
	</bean>
	
	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
       <property name="jobDetail">
           <ref bean="jobDetailBean" />
       </property>
       <!-- 
       		下面表示每天晚上21点执行
       		配置具体方法见《定时服务配置说明》
       -->
       <property name="cronExpression">
           <value>0 0 21 * * ?</value>
       </property>
    </bean>
	
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	    <property name="triggers">
	        <list>
	            <ref bean="cronTrigger" />
	        </list>
	    </property>
	</bean>
</beans>



id="loadObject"是定时服务类;
name="jobDetailBean"用来指定定时服务的方法;
id="cronTrigger"配置定时服务的时间;
最后一个bean用来设置定时服务队列。
(解释比较直白)

下面介绍一下定时服务时间的配置,也可以为以后做备忘:
表达式意义:
秒 0-59 , - * /  
分 0-59 , - * /  
小时 0-23 , - * /  
日期 1-31 , - * ? / L W C  
月份 1-12 或者 JAN-DEC , - * /  
星期 1-7 或者 SUN-SAT , - * ? / L C #  
年(可选) 留空, 1970-2099 , - * /  
例子:
"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 * 

特殊字符说明:
"*"字符被用来指定所有的值。如:"*"在分钟的字段域里表示"每分钟"。
"?"字符只在日期域和星期域中使用。它被用来指定"非明确的值"。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。
月份中的日期和星期中的日期这两个元素时互斥的一起应该通过设置一个问号(?)来表明不想设置那个字段
"-"字符被用来指定一个范围。如:"10-12"在小时域意味着"10点、11点、12点"。
","字符被用来指定另外的值。如:"MON,WED,FRI"在星期域里表示"星期一、星期三、星期五".
"/"字符用来指定具体的增量。如"0/15"在秒中的配置表示"从0秒开始每隔15秒",即"0,15,30,45"。
如"5/15"在秒中的配置表示"从5秒开始每隔15秒",即"5,20,35,50"。"*/5"和"0/5"是一个效果。
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".

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics