Spring的scheduling.quartz包中對(duì)Quartz框架進(jìn)行了封裝,使得開發(fā)時(shí)不用寫任何QuartSpring的代碼就可以實(shí)現(xiàn)定時(shí)任務(wù)。Spring通過JobDetailBean,MethodInvokingJobDetailFactoryBean實(shí)現(xiàn)Job的定義。后者更加實(shí)用,只需指定要運(yùn)行的類,和該類中要運(yùn)行的方法即可,Spring將自動(dòng)生成符合Quartz要求的JobDetail。
在上一篇文章《Quartz 框架快速入門(三)》中我們將示例遷移到Web環(huán)境下了,但使用的是Quartz的啟動(dòng)機(jī)制,這一篇中我們將讓W(xué)eb服務(wù)器啟動(dòng)Spring,通過Spring的配置文件來進(jìn)行任務(wù)的調(diào)度
1,創(chuàng)建一個(gè)Web項(xiàng)目,加入spring.jar,quartz-1.6.0.jar,commons-collections.jar,jta.jar ,commons-logging.jar這幾個(gè)包.
2,創(chuàng)建一個(gè)類,在類中添加一個(gè)方法execute,我們將對(duì)這個(gè)方法進(jìn)行定時(shí)調(diào)度.
package com.vista.quartz;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloWorld
{
private static Log logger = LogFactory.getLog(HelloWorld.
class);
//日志記錄器
public HelloWorld()
{
}
public void execute()
{
logger.info("Kick your ass and Fuck your mother! - " +
new Date());
}
}
2. Spring配置文件applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 要調(diào)用的工作類 -->
<bean id="quartzJob"
class="com.vista.quartz.HelloWorld"></bean>
<!-- 定義調(diào)用對(duì)象和調(diào)用對(duì)象的方法 -->
<bean id="jobtask"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 調(diào)用的類 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 調(diào)用類中的方法 -->
<property name="targetMethod">
<value>execute</value>
</property>
</bean>
<!-- 定義觸發(fā)時(shí)間 -->
<bean id="doTime"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask"/>
</property>
<!-- cron表達(dá)式 -->
<property name="cronExpression">
<value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>
</property>
</bean>
<!-- 總管理類 如果將lazy-init='false'那么容器啟動(dòng)就會(huì)執(zhí)行調(diào)度程序 -->
<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>
3,先在控制臺(tái)中對(duì)上面的代碼進(jìn)行測(cè)試,我們要做的只是加載Spring的配置文件就可以了,代碼如下:
package com.vista.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
public static void main(String[] args)
{
System.out.println("Test start.");
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
//如果配置文件中將startQuertz bean的lazy-init設(shè)置為false 則不用實(shí)例化
//context.getBean("startQuertz");
System.out.print("Test end..");
}
}
4,然后將Web.xml修改如下,讓tomcat在啟動(dòng)時(shí)去初始化Spring:
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>SpringContextServlet</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.jsp</welcome-file>
</welcome-file-list>
</web-app>
5,最后啟動(dòng)Tomcat,測(cè)試結(jié)果如下圖所示: