您的位置:首页 > 编程语言 > Java开发

spring quartz实例及下载包

2013-12-12 16:36 204 查看
Spring3.0不支持Quartz2.0,因为org.quartz.CronTrigger在2.0从class变成了一个interface造成IncompatibleClassChangeError错误:

Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class

今天刚下载了Spring3.2.4,发现这个新版本已经支持quartz2.x了.以前的Spring版本只支持Quartz-1.8.x及以前的版本,做个小实例分享一下.

注:Spring3.2.4配置文件中使用CronTriggerFactoryBean来集成quartz2.x,使用CronTriggerBean来集成quartz1.8.x及以前版本.

1. main方法入口类: MainTest.java,内容如下:

package com.sse.quartz;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* Created on 2013-12-12
* <p>Title:       XXXX系统_[模块名]/p>
* <p>Description: [描述该类概要功能介绍]</p>
* <p>Copyright:   Copyright (c) 2011</p>
* <p>Company:     </p>
* <p>Department:  网站运维部</p>
* @author         zhl
*/
public class MainTest {

public static void main(String[] args) {
System.out.println(" Test start  . ");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
// context.getBean("startQuertz");
System.out.print(" Test end .. ");
}
}


2. applicationContext.xml配置文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的Schema信息 -->

<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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
<!-- 启动触发器的配置开始
<bean name="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myJobTrigger" />
</list>
</property>
</bean> -->
<!-- 启动触发器的配置结束 -->

<!-- 调度的配置开始 -->
<!--
quartz-1.8以前的配置
<bean id="myJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>0/1 * * * * ?</value>
</property>
</bean>
-->
<!-- quartz-2.x的配置
<bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>0/1 * * * * ?</value>
</property>
</bean> -->
<!-- 调度的配置结束 -->

<!-- job的配置开始
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="myJob" />
</property>
<property name="targetMethod">
<value>work</value>
</property>
</bean> -->
<!-- job的配置结束 -->

<!-- 工作的bean
<bean id="myJob" class="com.sse.quartz.MyJob" /> -->

<!-- 上边为bean配置,下边为配置文件配置,两个不可都放开(放开上边则调用work文法,放下下边则调用execute方法)
如果用上边配置则不需要实现job接口-->
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="configLocation" value="classpath:quartz.properties" />
</bean>

</beans>


3. quartz.properties 配置文件:

#============================================================================
# Quartz 调度任务所需的配置文件
#============================================================================
#org.quartz.scheduler.instanceName属性可为任何值,用在 JDBC JobStore 中来唯一标识实例,但是所有集群节点中必须相同。
org.quartz.scheduler.instanceName = QuartzScheduler
#org.quartz.scheduler.instanceId 属性为 AUTO即可,基于主机名和时间戳来产生实例 ID。
org.quartz.scheduler.instanceId = AUTO

#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
# 线程个数
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5

#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

#============================================================================
# Configure Plugins
#============================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
# org.quartz.plugins.xml.JobInitializationPlugin是Quartz自带的插件,
# 默认时,这个插件会在 classpath 中搜索名为 quartz_jobs.xml
# 的文件并从中加载Job 和 Trigger 信息
# v1.8之前用JobInitializationPlugin
#org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
#org.quartz.plugin.jobInitializer.fileNames  main启动此值则为quartz_jobs.xml,若为应用服务器启动则为/WEB-INF/classes/quartz_jobs.xml
org.quartz.plugin.jobInitializer.fileNames = quartz_jobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval =10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false

# 关闭quartz新版本检测功能
org.quartz.scheduler.skipUpdateCheck =true


4. quartz_jobs.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data
xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_2_0.xsd" version="2.0">
<schedule>

<!--配置 -->
<job>
<name>MyJob</name>
<group>SSE_GROUP</group>
<job-class>com.sse.quartz.MyJob</job-class>
<durability>false</durability>
<recover>false</recover>
</job>
<trigger>
<cron>
<name>MyJobTrigger</name>
<group>SSE_GROUP</group>
<job-name>MyJob</job-name>
<job-group>SSE_GROUP</job-group>
<job-data-map>
<entry>
<key>tradeDay</key>
<value>1</value>
</entry>
</job-data-map>
<!-- <cron-expression>0 0/2 * * * ?</cron-expression>
<cron-expression>0 0 10 * * ?</cron-expression> -->
<cron-expression>0/5 * * * * ?</cron-expression>
</cron>
</trigger>

</schedule>
</job-scheduling-data>


5. 被调度类MyJob.java, 内容如下:

package com.sse.quartz;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
*
* Created on 2013-12-12
* <p>Title:       XXXX系统_[模块名]/p>
* <p>Description: [描述该类概要功能介绍]</p>
* <p>Copyright:   Copyright (c) 2011</p>
* <p>Company:     </p>
* <p>Department:  网站运维部</p>
* @author         zhl
*/
public class MyJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("execute:" + new Date().toString());
}
//你的代码
public void work() {
System.out.println("date:" + new Date().toString());
}
}


6. web.xml 配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
<!-- Spring config start -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring config end -->

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


7. readme.txt :

此包有两种方式启动:
1. 因为是web工程,可直接发布到应用服务器,直接启动
2. 可找成jar包,调用main方法启动(com.sse.quartz.MainTest就是该类的main方法)

针对这两种启动方式主要修改quartz.properties中的org.quartz.plugin.jobInitializer.fileNames
若为main启动此值则为:quartz_jobs.xml,若为应用服务器启动则为:/WEB-INF/classes/quartz_jobs.xml
原因不用说,路径问题吧!

该工程目前启动为main方法启动

如果仔细看applicationContext.xml,把上边的放开也是一种quartz的调用方式,也是可以启动的
在该文件中有说明:
<!-- 上边为bean配置,下边为配置文件配置,两个不可都放开(放开上边则调用work文法,放下下边则调用execute方法)
如果用上边配置则不需要实现job接口, 在用下边方法配置时要添加aopalliance-1.0.jar,jta-1.1.jar-->

---------------------------


原码附上,地址:http://download.csdn.net/detail/gnolzhao/6704113

8、启动com.sse.quartz.MainTest的main方法,一切ok

9、关于触发器的时间控制说明:

先说干货:quartz_job.xml中的

<!-- <cron-expression>0 0/2 * * * ?</cron-expression>
<cron-expression>0 0 10 * * ?</cron-expression> -->
<cron-expression>0/5 * * * * ?</cron-expression>


就是对触发时间的描述,我这里采用的是CronTrigger的方式。下边详细描述了时间的控制。

Trigger是一个抽象类,它有三个子类:SimpleTrigger,CronTrigger和NthIncludedDayTrigger。前两个比较常用。

1。SimpleTrigger:这是一个非常简单的类,我们可以定义作业的触发时间,并选择性的设定重复间隔和重复次数。

2。CronTrigger:这个触发器的功能比较强大,而且非常灵活,但是你需要掌握有关Cron表达式的知识。如果你是一个Unix系统爱好者,你很可能已经具备这种知识,但是如果你不了解Cron表达式,请看下面的Cron详解:

Cron表达式由6或7个由空格分隔的时间字段组成,如表1所示: 表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表示式的实例:

表2 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分运行。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: