/
//下面网站:可以参考
http://blog.csdn.net/szwangdf/article/details/6158646
<!-- 实现规则服务实时加载的定时服务配置文件 -->
<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".
下面有两个类:用的是 CronTrigger 方法写了例子:
package com.test.haoTest;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class TestHelloWorldJob implements Job {
public int count=0;
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException{
//实现你的业务逻辑
System.out.println("Hello" +count);
}
}
package com.test.haoTest;
import java.util.Date;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.expression.ParseException;
public class TestHelloWorldJobScheduling {
public TestHelloWorldJobScheduling() {
// TODO Auto-generated constructor stub
}
public Integer count=0;
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
System.out.println("== ok!");
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
JobDetail jobDetail = new JobDetail("testHelloWorldJob",
Scheduler.DEFAULT_GROUP, TestHelloWorldJob.class);
jobDetail.getJobDataMap().put("key", String.valueOf("test"));
// 这个是定时器每隔1秒执行TestHelloWorldJob.class 一次 并打印出"Hello!"
// Trigger trigger = new SimpleTrigger("simpleTrigger",
// Scheduler.DEFAULT_GROUP, new Date(), null,
// SimpleTrigger.REPEAT_INDEFINITELY, 2000);
// CronTrigger triggers = new CronTrigger(String.valueOf(CronTrigger.INSTRUCTION_RE_EXECUTE_JOB) ,Scheduler.DEFAULT_GROUP,String.valueOf(CronTrigger.INSTRUCTION_RE_EXECUTE_JOB) ,"0 1 0 * * ?");
// CronTrigger triggers1 = new CronTrigger("myTrigger", Scheduler.DEFAULT_GROUP, "1? 0 0 0 * MON,FRI");
try {
//周一到周五在 每天下午的17点到18点每隔一分钟就调用TestHelloWorldJob.java 并打印出"Hello!"
Trigger cTrigger = new CronTrigger("myTrigger",
Scheduler.DEFAULT_GROUP, "0 0/1 17 ? * MON,FRI");
// 每隔1秒调用TestHelloWorldJob.java 并打印出"Hello!"
Trigger cTrigger = new CronTrigger("myTrigger",
Scheduler.DEFAULT_GROUP, "0/1 * * * * ?");
scheduler.scheduleJob(jobDetail, cTrigger);
} catch (ParseException ex) {
ex.printStackTrace();
}
// scheduler.scheduleJob(jobDetail, trigger);
}
}