[quote]
Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在Spring中怎样配置Quartz.
[/quote]
被调度的类:
配置文件quartz.xml:
测试程序:
配置文件web.xml:
所需jar包:
Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在Spring中怎样配置Quartz.
[/quote]
被调度的类:
package timer.quartz;
import java.util.Date;
import org.apache.log4j.Logger;
/**
* @Title: QuartzJob.java
* @Package timer.quartz
* @Description: TODO
* @author ZHAOXQ@EHOO.CN
* @date 2011-8-3 下午12:05:02
* @version V1.0
*/
public class QuartzJob {
private Logger logger = Logger.getLogger(this.getClass().getName());
public void work() {
logger.info("******开始进行任务调度******");
//业务逻辑
System.out.println("当前时间: " + new Date());
}
}
配置文件quartz.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="timer.quartz.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">
<!-- 每隔10秒执行一次 -->
<value>0/10 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime" />
</list>
</property>
</bean>
</beans>
测试程序:
package main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Title: QuartzJobMain.java
* @Package main
* @Description: TODO
* @author ZHAOXQ@EHOO.CN
* @date 2011-8-3 下午12:03:54
* @version V1.0
*/
public class QuartzJobMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("quartz.xml");
//如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
// context.getBean("startQuertz");
}
}
配置文件web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>webapps</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/quartz.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
所需jar包: