然后看下Spring配置文件quartz.xml内容:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="myScheduler" lazy-init="false" autowire="no"
- class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="myJobTrigger" />
- </list>
- </property>
- </bean>
- <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
- <ref bean="myJobDetail" />
- </property>
- <property name="cronExpression">
- <value>00,10,20,30,40,50 * * * * ?</value>
- </property>
- </bean>
- <bean id="myJobDetail"
- class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject">
- <ref bean="myJob" />
- </property>
- <property name="targetMethod">
- <value>scan</value>
- </property>
- </bean>
- <bean id="myJob" class="org.shirdrn.spring.scheduler.quartz.cron.MyQuartzJob" />
- </beans>
通过Spring的org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean来设置目标定时任务处理内容,如处理类和处理方法,通过org.springframework.scheduling.quartz.CronTriggerBean的cronExpression属性来配置定时任务,非常灵活。
上述配置,定时任务会自动调用MyQuartzJob类的scan方法来执行任务。
对于cronExpression属性的值,包括以下7 个字段:
- 秒
- 分
- 小时
- 月内日期
- 月
- 周内日期
- 年(可选字段)
有关各个字段可以设置的值:
- Cron 触发器利用一系列特殊字符,如下所示:
- ?反斜线(/)字符表示增量值。例如,在秒字段中“5/15”代表从第 5 秒开始,每 15 秒一次。
- ?问号(?)字符和字母 L 字符只有在月内日期和周内日期字段中可用。问号表示这个字段不包含具体值。所以,如果指定月内日期,可以在周内日期字段中插入“?”,表示周内日期值无关紧要。字母 L 字符是 last 的缩写。放在月内日期字段中,表示安排在当月最后一天执行。在周内日期字段中,如果“L”单独存在,就等于“7”,否则代表当月内周内日期的最后一个实例。所以“0L”表示安排在当月的最后一个星期日执行。
- ?在月内日期字段中的字母(W)字符把执行安排在最靠近指定值的工作日。把“1W”放在月内日期字段中,表示把执行安排在当月的第一个工作日内。
- ?井号(#)字符为给定月份指定具体的工作日实例。把“MON#2”放在周内日期字段中,表示把任务安排在当月的第二个星期一。
- ?星号(*)字符是通配字符,表示该字段可以接受任何可能的值。
更多信息可以查阅相关资料。
启动定时任务,测试代码如下所示:
- package org.shirdrn.spring.scheduler.quartz.cron;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class ContextInitializer {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "org/shirdrn/spring/scheduler/quartz/cron/quartz.xml");
- ctx.getBean("myScheduler");
- }
- }
