参考资料:
1、SPring Bean注入作业的两种方式:
方式一:@1、Spring使用JobDetailBean类,继承QuartzJobBean,重写
protected void executeInternal(JobExecutionContext context)方法
@2、 或实现Job接口,在execute方法中调用执行任务
方式二:Spring使用MethodInvokingJobDetailFactoryBean,普通Java类都可
2、方式一的实现:
java 代码
public class AlterJob extends QuartzJobBean{
//业务实现
public void work() {
System.out.println("执行调度任务:"+new Date());
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
this.work();
}
//实现Job接口
// public void execute(JobExecutionContext arg0) throws JobExecutionException {
// this.work();
// }
}
applicationContext.xml的配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
">
<!-- 注入执行任务Bean -->
<bean name="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailBean" >
<property name="jobClass" value="com.boonya.quartz.job.AlterJob" />
<property name="jobDataAsMap">
<map>
<entry key="size" value="10"></entry>
</map>
</property>
<property name="applicationContextJobDataKey" value="applicationContext"/>
</bean>
<!-- 触发器 -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="startDelay" value="20000"/>
<property name="repeatInterval" value="60000"/>
<property name="jobDetail" ref="jobDetailBean"/>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
<!-- 指向我们的任务 -->
<property name="jobDetail" ref="jobDetailBean" />
<!-- 每分钟10秒运行1次 -->
<property name="cronExpression" value="0/10 * * * * ?" />
</bean>
<!-- 调度器 -->
<!-- <bean id="startCornQuartzJob" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name="triggers">
<list>
触发器列表
<ref bean="cronTrigger" />
</list>
</property>
<property name="configLocation" value="classpath:quartz.properties" />
<property name="autoStartup" value="true"/>
<property name="schedulerName" value="cronScheduler" />
</bean> -->
<bean id="startSimpleQuartzJob" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger"/>
<ref bean="cronTrigger" />
</list>
</property>
<property name="autoStartup" value="true"/>
<property name="schedulerName" value="cronScheduler" />
</bean>
</beans>
注意:这里任务启动的时候startCornQuartzJob和startSimpleQuartzJob不能同时都开放,否则抛出异常。
3、方式2的实现:
java代码
public class QuartzJob {
public void work(){
System.out.println("work执行调度任务:"+new Date());
}
}
applicationContext.xml/applicationContext-jobs.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 要调用的工作类 -->
<bean id="quartzJob" class="com.boonya.quartz.job.QuartzJob"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobTask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
</beans>
注:方式2的配置可以作为外部文件导入到applicationContext.xml中去。
测试方法二:
public static void main(String[] args) {
System.out.println("----------Test start.-------\n");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-jobs.xml");
//如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
context.getBean("startQuartz");
}
4、web.xml配置{跟以前没有区别}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 指定spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--加载多个spring配置文件 -->
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 定义SPRING监听器,加载spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- 字符过滤器 -->
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5、需要的Jar包:spring.jar,quartz.jar,若涉及持久化操作需要jpa.jar....等等,根据自己项目的需要添加。
搞了一天终于出来了,记于此处!
6.Cron表达式
Quartz使用类似于Linux下的Cron表达式定义时间规则,Cron表达式由6或7个由空格分隔的时间字段组成,如表1所示:
Cron表达式时间字段
位置 | 时间域名 | 允许值 | 允许的特殊字符 |
1 | 秒 | 0-59 | , - * / |
2 | 分钟 | 0-59 | , - * / |
3 | 小时 | 0-23 | , - * / |
4 | 日期 | 1-31 | , - * ? / L W C |
5 | 月份 | 1-12 | , - * / |
6 | 星期 | 1-7 | , - * ? / L C # |
7 | 年(可选) | 空值1970-2099 | , - * / |
Cron表达式的时间字段除允许设置数值外,还可使用一些特殊的字符,提供列表、范围、通配符等功能,细说如下:
●星号(*):可用在所有字段中,表示对应时间域的每一个时刻,例如,*在分钟字段时,表示“每分钟”;
●问号(?):该字符只在日期和星期字段中使用,它通常指定为“无意义的值”,相当于点位符;
●减号(-):表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12;
●逗号(,):表达一个列表值,如在星期字段中使用“MON,WED,FRI”,则表示星期一,星期三和星期五;
●斜杠(/):x/y表达一个等步长序列,x为起始值,y为增量步长值。如在分钟字段中使用0/15,则表示为0,15,30和45秒,而5/15在分钟字段中表示5,20,35,50,你也可以使用*/y,它等同于0/y;
●L:该字符只在日期和星期字段中使用,代表“Last”的意思,但它在两个字段中意思不同。L在日期字段中,表示这个月 份的最后一天,如一月的31号,非闰年二月的28号;如果L用在星期中,则表示星期六,等同于7。但是,如果L出现在星期字段里,而且在前面有一个数值 X,则表示“这个月的最后X天”,例如,6L表示该月的最后星期五;
●W:该字符只能出现在日期字段里,是对前导日期的修饰,表示离该日期最近的工作日。例如15W表示离该月15号最近的工 作日,如果该月15号是星期六,则匹配14号星期五;如果15日是星期日,则匹配16号星期一;如果15号是星期二,那结果就是15号星期二。但必须注意 关联的匹配日期不能够跨月,如你指定1W,如果1号是星期六,结果匹配的是3号星期一,而非上个月最后的那天。W字符串只能指定单一日期,而不能指定日期 范围;
●LW组合:在日期字段可以组合使用LW,它的意思是当月的最后一个工作日;
●井号(#):该字符只能在星期字段中使用,表示当月某个工作日。如6#3表示当月的第三个星期五(6表示星期五,#3表示当前的第三个),而4#5表示当月的第五个星期三,假设当月没有第五个星期三,忽略不触发;
● C:该字符只在日期和星期字段中使用,代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。例如5C在日期字段中就相当于日历5日以后的第一天。1C在星期字段中相当于星期日后的第一天。
Cron表达式对特殊字符的大小写不敏感,对代表星期的缩写英文大小写也不敏感。
表2下面给出一些完整的Cron表示式的实例:
Cron表示式示例
表示式 | 说明 |
"0 0 12 * * ? " | 每天12点运行 |
"0 15 10 ? * *" | 每天10:15运行 |
"0 15 10 * * ?" | 每天10:15运行 |
"0 15 10 * * ? *" | 每天10:15运行 |
"0 15 10 * * ? 2008" | 在2008年的每天10:15运行 |
"0 * 14 * * ?" | 每天14点到15点之间每分钟运行一次,开始于14:00,结束于14:59。 |
"0 0/5 14 * * ?" | 每天14点到15点每5分钟运行一次,开始于14:00,结束于14:55。 |
"0 0/5 14,18 * * ?" | 每天14点到15点每5分钟运行一次,此外每天18点到19点每5钟也运行一次。 |
"0 0-5 14 * * ?" | 每天14:00点到14:05,每分钟运行一次。 |
"0 10,44 14 ? 3 WED" | 3月每周三的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 2007-2009" | 在2007,2008,2009年每个月的最后一个星期五的10:15分运行。 |
"0 15 10 ? * 6#3" | 每月第三个星期五的10:15分运行。 |