<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    yxhxj2006

    常用鏈接

    統計

    最新評論

    Quartz 框架快速入門(三)

      在前面兩篇文章中簡單介紹了在java應用程序中如何使用Quartz框架,這一篇中我們將看到如何在web環境下通過配置文件來完成Quartz的后臺作業調度,而不必手工去創建Trigger和Scheduler,其步驟如下:
    首先創建一個Web項目,將quartz-1.6.0.jar,以及lib目錄下面core下所有jar,optional目錄下的所有commons-beanutils.jar和commons-digester-1.7.jar,build目錄下的jta.jar都放入Web項目的WEB-INF"lib目錄下。
    創建一個簡單的job類HelloWorld,它的功能很簡單,就是輸出當前的時間,代碼如下:

    package com.vista.quartz;

    import java.util.Date;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;

    public class Helloworld implements Job
    {
        private static Log logger = LogFactory.getLog(Helloworld.class);//日志記錄器
        public Helloworld()
        {
        }
        public void execute(JobExecutionContext context) throws JobExecutionException 
        {
            logger.info("Hello World! - " + new Date()); 
        }
    }

    然后按照上一篇文章《Quartz 框架快速入門(二)》中所講述的內容編寫quartz.properties文件。如果啟動項目的時候,Quartz沒有在工程中找到該文件,就會從自己的jar包下面讀取其默認的properties文件,其內容如下

    #============================================================================
    # Configure Main Scheduler Properties  
    #============================================================================
    org.quartz.scheduler.instanceName = QuartzScheduler
    org.quartz.scheduler.instanceId = AUTO
    #============================================================================
    # Configure ThreadPool  
    #============================================================================
    org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
    org.quartz.threadPool.threadCount = 5
    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.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
    org.quartz.plugin.jobInitializer.fileNames = jobs.xml
    org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
    org.quartz.plugin.jobInitializer.failOnFileNotFound = true
    org.quartz.plugin.jobInitializer.scanInterval = 10
    org.quartz.plugin.jobInitializer.wrapInUserTransaction = false

    然后編寫任務配置文件jobs.xml,內容如下:

    <?xml version='1.0' encoding='utf-8'?>
    <quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.opensymphony.com/quartz/JobSchedulingData
      http://www.opensymphony.com/quartz/xml/job_scheduling_data_1_5.xsd"
      version="1.5">   
     <job>      
        <job-detail>      
         <name>HelloWorld</name>      
         <group>DEFAULT</group>      
         <description>      
               A job that just for test       
         </description>      
         <job-class>      
                com.vista.quartz.Helloworld      
         </job-class>      
         <volatility>false</volatility>      
         <durability>false</durability>      
         <recover>false</recover>          
      </job-detail>      
      <trigger>      
        <simple>      
         <name>HelloTrigger1</name>      
         <group>DEFAULT</group>      
         <job-name>HelloWorld</job-name>      
         <job-group>DEFAULT</job-group>      
         <start-time>2008-09-03T15:56:30</start-time>      
         <!-- repeat indefinitely every 10 seconds -->      
         <repeat-count>-1</repeat-count>      
         <repeat-interval>10000</repeat-interval>      
        </simple>      
      </trigger>      
    </job>      
    </quartz>

    可以看到,在配置文件中把jobdetail和trigger都作了完整的定義,并組合成一個job。下面,我們把上面兩個文件都放入/WEB-INF/classes目錄下,然后按照api中的說明修改一下web.xml,內容如下

    <?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">
        <servlet>  
            <servlet-name>QuartzInitializer</servlet-name>  
            <display-name>Quartz Initializer Servlet</display-name>
            <servlet-class>  
                 org.quartz.ee.servlet.QuartzInitializerServlet  
            </servlet-class>  
            <load-on-startup>1</load-on-startup>  
            <init-param>  
                <param-name>config-file</param-name>  
                <param-value>/quartz.properties</param-value>  
            </init-param>  
            <init-param>  
                <param-name>shutdown-on-unload</param-name>  
                <param-value>true</param-value>  
           </init-param>  
        </servlet>  
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    這樣,在啟動Tomcat的時候,QuartzInitializerServlet這個Servlet就會自動讀取quartz.properties這個配置文件,并初始化調度信息,啟動Scheduler。
    啟動tomcat后,就可以看到輸出的結果:
     

    posted on 2012-09-14 18:29 奮斗成就男人 閱讀(170) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲乱码中文论理电影| 伊人婷婷综合缴情亚洲五月| 亚洲精品国产专区91在线| 国产一区二区三区免费观看在线| 久久亚洲精品无码播放| 成人一级免费视频| 亚洲综合国产一区二区三区| 国产精品内射视频免费| 亚洲无码在线播放| 特级无码毛片免费视频尤物| 久久亚洲AV成人无码国产| 永久黄色免费网站| 2020亚洲男人天堂精品| 日韩电影免费在线| 成人精品综合免费视频| 亚洲AV无码久久| 最近最新高清免费中文字幕| 亚洲AV无码久久久久网站蜜桃| 老司机永久免费网站在线观看| 黄色网址免费在线| 亚洲精品无码专区久久久| 99免费在线观看视频| 国产成人精品日本亚洲专一区| 精品国产免费观看久久久| 二级毛片免费观看全程| 亚洲av无码国产精品夜色午夜 | 亚洲国产中文v高清在线观看| 一级有奶水毛片免费看| 亚洲精品美女久久久久| 免费高清在线影片一区| 久久不见久久见免费影院www日本| 色婷婷亚洲十月十月色天| 成人免费a级毛片| 中文字幕成人免费高清在线| 亚洲日本在线免费观看| 国产一区二区三区免费视频| 国产精品免费高清在线观看| 亚洲女子高潮不断爆白浆| 亚洲乱亚洲乱妇无码麻豆| 69成人免费视频无码专区| 国产精品午夜免费观看网站 |