一、新增pom依賴html
除了按照《quartz2.3.0系列目錄——帶您由淺入深全面掌握quartz2.3.0》添加依賴以外,pom.xml裏新增長依賴:java
<dependency> <groupId>opensymphony</groupId> <artifactId>quartz-all</artifactId> <version>1.6.3</version> </dependency> <dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency>
二、quartz.properties配置express
#============================================================================ # Configure Main Scheduler Properties #============================================================================ org.quartz.scheduler.instanceName: TestScheduler 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 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.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames: quartz_data.xml org.quartz.plugin.jobInitializer.failOnFileNotFound: true org.quartz.plugin.jobInitializer.scanInterval: 120 org.quartz.plugin.jobInitializer.wrapInUserTransaction: false
三、quartz_data.xml配置函數
<?xml version="1.0" encoding="UTF-8"?> <job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd" version="1.8"> <pre-processing-commands> <delete-jobs-in-group>*</delete-jobs-in-group> <!-- 清除調度器中的全部做業 --> <delete-triggers-in-group>*</delete-triggers-in-group> <!-- 清除調度器中的全部觸發器 --> </pre-processing-commands> <processing-directives> <!-- 若是調度程序中有任何同名的做業/觸發器(與此文件中相同),會覆蓋它們 --> <overwrite-existing-data>true</overwrite-existing-data> <!-- 若是調度程序中有任何同名的做業/觸發器(與此文件中相同),而且覆蓋爲false,則忽略它們,而不是生成錯誤 --> <ignore-duplicates>false</ignore-duplicates> </processing-directives> <schedule> <job> <name>TestJob1</name> <job-class>org.quartz.examples.example10.SimpleJob</job-class> </job> <job> <name>TestDurableJob</name> <job-class>org.quartz.examples.example10.SimpleJob</job-class> <durability>true</durability> <recover>false</recover> </job> <trigger> <simple> <name>TestSimpleTrigger1AtFiveSecondInterval</name> <job-name>TestJob1</job-name> <repeat-count>-1</repeat-count> <!-- repeat indefinitely --> <repeat-interval>5000</repeat-interval> <!-- every 5 seconds --> </simple> </trigger> <job> <name>TestJob2</name> <group>GroupOfTestJob2</group> <description>This is the description of TestJob2</description> <job-class>org.quartz.examples.example10.SimpleJob</job-class> <durability>false</durability> <recover>true</recover> <job-data-map> <entry> <key>someKey</key> <value>someValue</value> </entry> <entry> <key>someOtherKey</key> <value>someOtherValue</value> </entry> </job-data-map> </job> <trigger> <simple> <name>TestSimpleTrigger2AtTenSecondIntervalAndFiveRepeats</name> <group>GroupOfTestJob2Triggers</group> <job-name>TestJob2</job-name> <job-group>GroupOfTestJob2</job-group> <start-time>2010-02-09T10:15:00</start-time> <misfire-instruction>MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT</misfire-instruction> <repeat-count>5</repeat-count> <repeat-interval>10000</repeat-interval> </simple> </trigger> <trigger> <cron> <name>TestCronTrigger2AtEveryMinute</name> <group>GroupOfTestJob2Triggers</group> <job-name>TestJob2</job-name> <job-group>GroupOfTestJob2</job-group> <job-data-map> <entry> <key>someKey</key> <value>overriddenValue</value> </entry> <entry> <key>someOtherKey</key> <value>someOtherOverriddenValue</value> </entry> </job-data-map> <cron-expression>0 * * ? * *</cron-expression> </cron> </trigger> <trigger> <cron> <name>TestCronTrigger2AtEveryMinuteOnThe45thSecond</name> <group>GroupOfTestJob2Triggers</group> <job-name>TestJob2</job-name> <job-group>GroupOfTestJob2</job-group> <start-time>2010-02-09T12:26:00.0</start-time> <end-time>2012-02-09T12:26:00.0</end-time> <misfire-instruction>MISFIRE_INSTRUCTION_SMART_POLICY</misfire-instruction> <cron-expression>45 * * ? * *</cron-expression> <time-zone>America/Los_Angeles</time-zone> </cron> </trigger> </schedule> </job-scheduling-data>
四、任務類,xml配置文件裏指向的job任務類spa
package org.quartz.examples.example10; import java.util.Date; import java.util.Set; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * job任務類 */ public class SimpleJob implements Job { private static Logger LOG = LoggerFactory.getLogger(SimpleJob.class); // 必需要有public修飾的無參構造函數 public SimpleJob() { } // 定時器執行方法 @SuppressWarnings("unchecked") public void execute(JobExecutionContext context) throws JobExecutionException { JobKey jobKey = context.getJobDetail().getKey(); LOG.info("--------- job任務執行: " + jobKey + " executing at " + new Date() + ", fired by: " + context.getTrigger().getKey()); if (context.getMergedJobDataMap().size() > 0) { Set<String> keys = context.getMergedJobDataMap().keySet(); for (String key : keys) { String val = context.getMergedJobDataMap().getString(key); LOG.info("--------- job任務執行。 - jobDataMap entry: " + key + " = " + val); } } context.setResult("hello"); } }
五、PlugInExample調度器類code
package org.quartz.examples.example10; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SchedulerMetaData; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 這個示例將生成大量要運行的做業 */ public class PlugInExample { public void run() throws Exception { Logger log = LoggerFactory.getLogger(PlugInExample.class); // 初始化一個調度工廠,並實例化一個調度類 SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = null; try { sched = sf.getScheduler(); } catch (NoClassDefFoundError e) { log.error(" 沒法加載類——極可能類路徑上沒有jta.jar。 若是在examples/lib文件夾中沒有,請將它添加到這裏,以便運行這個示例。", e); return; } log.info("----------- 不調度任何做業——依賴於XML定義 "); sched.start(); Thread.sleep(300L * 1000L); sched.shutdown(true); SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); } public static void main(String[] args) throws Exception { PlugInExample example = new PlugInExample(); example.run(); } }