Quartz實例簡介

項目簡稱webapp
1.將Quartz的quartz-all-1.5.2.jar文件放入webapp的WEB-INF\lib下
2.在webapp的web.xml中加入:
<servlet>
  <servlet-name>QuartzInitializer</servlet-name>
  <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
3.然後新建一個quartz.properties,放在webapp的classpath下面,即src下:
#
# Configure Main Scheduler Properties
#
org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = one
#
# Configure ThreadPool
#
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount =  5
org.quartz.threadPool.threadPriority = 4
#
# Configure JobStore
#
org.quartz.jobStore.misfireThreshold = 5000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
# ===========================================================================
# Configure SchedulerPlugins  ===============================================
# ===========================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin
org.quartz.plugin.triggHistory.triggerFiredMessage = Trigger {1}.{0} fired job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy}
org.quartz.plugin.triggHistory.triggerCompleteMessage = Trigger {1}.{0} completed firing job {6}.{5} at {4, date, HH:mm:ss MM/dd/yyyy} with resulting trigger instruction code: {9}

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
#定義quartz_reminder.xml(見下一步)的路徑
org.quartz.plugin.jobInitializer.fileName = /quartz_reminder.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true

org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true

4.新建一個quartz_reminder.xml,其中定義job和trigger的信息:
<?xml version="1.0" encoding="UTF-8"?>
<quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        overwrite-existing-jobs="true">
    <job>
        <job-detail>
            <!-- job 的名字 -->
            <name>test-job</name>
            <group>DEFAULT</group>
            <description>The job description</description>
            <!-- job類的路徑加類名 -->
            <job-class>com.joysen.rule.scheduler.MyJob</job-class>
            <!-- job-data-map allows-transient-data="false">
                <entry>
                    <key>burger-type</key>
                    <value>hotdog</value>
                </entry>
                <entry>
                    <key>dressing-list</key>
                    <value>ketchup,mayo</value>
                </entry>
            </job-data-map -->
        </job-detail>
        <trigger>
            <cron>
                <!-- trigger 的名字 -->
                <name>test-trigger</name>
                <group>DEFAULT</group>
                <!-- trigger對應的job的相關信息 -->
                <job-name>test-job</job-name>
                <job-group>DEFAULT</job-group>
                <!-- 每5秒執行一次 -->
                <cron-expression>0/5 * * * * ?</cron-expression>
            </cron>
        </trigger>
    </job>
</quartz>
注:
cron-expression表示執行job的時間

5.然後在com.joysen.rule.scheduler下面新建MyJob.java文件
MyJob必須實現Quartz的Job接口
public class MyJob implements Job {
  public void execute(JobExecutionContext context) throws JobExecutionException {
  //在控制檯輸出如下信息。 
  System.out.println("Quartz Test");
  }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章