Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的日程序表。Jobs可以做成标准的Java组件或 EJBs。
Quartz是一个任务日程管理系统,一个在预先确定(被纳入日程)的时间到达时,负责执行(或者通知)其他软件组件的系统。
Quartz用一个小Java库发布文件(.jar文件),这个库文件包含了所有Quartz核心功能。这些功能的主要接口(API)是Scheduler接口。它提供了简单的操作,例如:将任务纳入日程或者从日程中取消,开始/停止/暂停日程进度。
quartz下载: http://www.quartz-scheduler.org/download/index.html
Spring3中整合Quartz
虽然Quartz已经发布了2.X版本,但是Spring3目前只能整合Quartz1.8.5及以下版本。
项目结构图
1.作业调度类的实现
package org.dennist.jobs;
import org.apache.log4j.Logger;
import org.dennist.service.SystemTimeHandler;
/**
*
*
*
* @version : 1.1
*
* @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a>
*
* @since : 1.0 创建时间: 2013-7-8 上午09:49:20
*
* TODO : org.dennist.jobs.TimeQuartzJob.java
*
*/
public class TimeQuartzJob {
private final Logger logger = Logger.getLogger(TimeQuartzJob.class);
private SystemTimeHandler systemTimeHandler;
public void noticeSystemTime(){
logger.info("定时器执行,调用系统时间服务类中的系统时间通知方法");
systemTimeHandler.noticeSystemTime();
}
public SystemTimeHandler getSystemTimeHandler() {
return systemTimeHandler;
}
public void setSystemTimeHandler(SystemTimeHandler systemTimeHandler) {
this.systemTimeHandler = systemTimeHandler;
}
}
作业调度类中调用实现了SystemTimeHandler接口的类SystemTimeHandlerImpl,实现定时向控制台输出格式化的系统时间,SystemTimeHandlerImpl的类实现如下
package org.dennist.service.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import org.dennist.jobs.TimeQuartzJob;
import org.dennist.service.SystemTimeHandler;
/**
*
*
*
* @version : 1.1
*
* @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a>
*
* @since : 1.0 创建时间: 2013-7-8 上午09:55:10
*
* TODO : org.dennist.service.impl.SystemTimeHandlerImpl.java
*
*/
public class SystemTimeHandlerImpl implements SystemTimeHandler{
private final Logger logger = Logger.getLogger(TimeQuartzJob.class);
public void noticeSystemTime() {
logger.info("notice system time to all users");
SimpleDateFormat format = new SimpleDateFormat("现在时间是: yyyy/MM/dd hh:mm:ss");
System.out.println(format.format(new Date()).toString());
}
}
2.Web.xml中配置spring上下文监听和quartz初始化监听
<?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"> <display-name>spring-quartz</display-name> <context-param> <param-name>webAppRootKey</param-name> <param-value>quartzapp</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>3000</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-context.xml</param-value> </context-param> <!-- Quartz初始化监听 --> <listener> <listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class> </listener> <!-- 注入 字符编码过滤器 --> <filter> <filter-name>encodingFilter</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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>