天天看點

Quartz 架構快速入門(四)

  Spring的scheduling.quartz包中對Quartz架構進行了封裝,使得開發時不用寫任何QuartSpring的代碼就可以實作定時任務。Spring通過JobDetailBean,MethodInvokingJobDetailFactoryBean實作Job的定義。後者更加實用,隻需指定要運作的類,和該類中要運作的方法即可,Spring将自動生成符合Quartz要求的JobDetail。

在上一篇文章《Quartz 架構快速入門(三)》中我們将示例遷移到Web環境下了,但使用的是Quartz的啟動機制,這一篇中我們将讓Web伺服器啟動Spring,通過Spring的配置檔案來進行任務的排程

1,建立一個Web項目,加入spring.jar,quartz-1.6.0.jar,commons-collections.jar,jta.jar ,commons-logging.jar這幾個包.

     2,建立一個類,在類中添加一個方法execute,我們将對這個方法進行定時排程.

複制代碼

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">

    <!-- 要調用的工作類 -->

    <bean id="quartzJob" class="com.vista.quartz.HelloWorld"></bean>

    <!-- 定義調用對象和調用對象的方法 -->

    <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

        <!-- 調用的類 -->

        <property name="targetObject">

            <ref bean="quartzJob"/>

        </property>

        <!-- 調用類中的方法 -->

        <property name="targetMethod">

             <value>execute</value>

    </bean>

    <!-- 定義觸發時間 -->

    <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">

        <property name="jobDetail">

            <ref bean="jobtask"/>

        <!-- cron表達式 -->

        <property name="cronExpression">

            <value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>

    <!-- 總管理類 如果将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>

</beans>

3,先在控制台中對上面的代碼進行測試,我們要做的隻是加載Spring的配置檔案就可以了,代碼如下:

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設定為false 則不用執行個體化

            //context.getBean("startQuertz");

         System.out.print("Test end..");

4,然後将Web.xml修改如下,讓tomcat在啟動時去初始化Spring:

<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,最後啟動Tomcat,測試結果如下圖所示:

本文轉自Phinecos(洞庭散人)部落格園部落格,原文連結:http://www.cnblogs.com/phinecos/archive/2008/09/03/1283376.html,如需轉載請自行聯系原作者