两个配置文件,一个properties和一个xml
properties:
- #============================================================================
- # 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
- #线程优先级别,一般为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
- #如果jobs.xml中存在调度器中已经有的job,true为覆盖
- org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
- org.quartz.plugin.jobInitializer.failOnFileNotFound = true
- #扫描jobs.xml的时间间隔
- org.quartz.plugin.jobInitializer.scanInterval = 10000
- org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
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>
- <!-- job-detail.name不能有重复,不然会执行最后一个与之相同的job-->
- <name>ScanDirectory1</name>
- <group>DEFAULT</group>
- <description>
- A job that scans a directory for files
- </description>
- <job-class>
- com.haso.quartz.ScanDirectoryJob
- </job-class>
- <volatility>false</volatility>
- <durability>false</durability>
- <recover>false</recover>
- <job-data-map allows-transient-data="true">
- <entry>
- <key>SCAN_DIR</key>
- <value>E:/Tomcat/conf</value>
- </entry>
- </job-data-map>
- </job-detail>
- <trigger>
- <cron>
- <name>cron_time</name>
- <group>DEFAULT</group>
- <job-name>ScanDirectory1</job-name>
- <job-group>DEFAULT</job-group>
- <!-- 每天15点28分执行此JOB -->
- <cron-expression>0 28 15 * * ?</cron-expression>
- </cron>
- </trigger>
- </job>
- <job>
- <job-detail>
- <name>ScanDirectory2</name>
- <group>DEFAULT</group>
- <description>
- A job that scans a directory for files
- </description>
- <job-class>
- com.haso.quartz.ScanDirectoryJob
- </job-class>
- <volatility>false</volatility>
- <durability>false</durability>
- <recover>false</recover>
- <job-data-map allows-transient-data="true">
- <entry>
- <key>SCAN_DIR</key>
- <value>E:/Tomcat/conf2</value>
- </entry>
- </job-data-map>
- </job-detail>
- <trigger>
- <simple>
- <name>scanTrigger2</name>
- <group>DEFAULT</group>
- <job-name>ScanDirectory2</job-name>
- <job-group>DEFAULT</job-group>
- <start-time>2008-09-03T14:43:00</start-time>
- <!-- repeat indefinitely every 10 seconds -->
- <repeat-count>-1</repeat-count>
- <repeat-interval>11000</repeat-interval>
- </simple>
- </trigger>
- </job>
- </quartz>
- package com.haso.quartz;
- import java.util.Date;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.quartz.SchedulerException;
- import org.quartz.Trigger;
- import org.quartz.TriggerUtils;
- import org.quartz.impl.StdSchedulerFactory;
- import com.haso.utils.Utils;
- public class SimpleScheduler {
- public static void main(String[] args)
- {
- SimpleScheduler simple = new SimpleScheduler();
- try
- {
- // Create a Scheduler and schedule the Job
- Scheduler scheduler = simple.createScheduler();
- // simple.scheduleJob(scheduler);
- // Start the Scheduler running
- scheduler.start();
- System.out.println("Scheduler started at " + Utils.dateToStr(new Date(), "yyyy-MM-dd hh:mm:ss"));
- } catch (SchedulerException ex) {
- ex.printStackTrace();
- }
- }
- public Scheduler createScheduler() throws SchedulerException
- {//创建调度器
- return StdSchedulerFactory.getDefaultScheduler();
- }
- }