quartz實現定時功能例項詳解(servlet定時器配置方法)
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>config-file</param-name>
<param-value>quartz.properties</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
listener配置可以看原始碼,主要的上面的引數配置為<context-param>,再加一個<listener>.
上面提到了quartz.properties,這是自行指定的,Quartz提供了一個預設的配置檔案,可以滿足基本的j2se應用,如果在web應用中,我們想把job,trigger配置都寫到檔案中,就需要自己來寫,並指定在初始化時載入我們自己的quratz.properties,位置放在classes下。
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5
#============================================================================
# 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.fileName = /scheduler/quartz_jobs.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
quartz要使用外掛來載入自己的xml配置檔案,上邊我們指定了在初始化時載入classes/scheduler/quartz_jobs.xml,預設的是載入classes/quartz_jobs.xml檔案。
quartz_jobs.xml檔案:
<?xml version='1.0' encoding='utf-8'?>
<quartz>
<job>
<job-detail>
<name>test</name>
<group>DEFAULT</group>
<description>testJobhere</description>
<job-class>TestJob</job-class>
<job-data-map allows-transient-data="true">
<entry>
<key>name</key>
<value>test</value>
</entry>
</job-data-map>
</job-detail>
<trigger>
<cron>
<name>testCron</name>
<group>DEFAULT</group>
<job-name>test</job-name>
<job-group>DEFALUT</job-group>
<cron-expression>0/3 * * * * ?</cron-expression>
</cron>
</trigger>
</job>
</quartz>