项目结构图:
TestMain.java
1 package com; 2 import org.quartz.Scheduler; 3 import org.quartz.impl.StdSchedulerFactory; 4 5 public class TestMain { 6 7 public static void main(String[] args) { 8 9 try {10 // 设置quartz.properties的classpath11 System.setProperty("org.quartz.properties", "quartz/quartz.properties");12 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();13 scheduler.start();14 } catch (Exception ex) {15 ex.printStackTrace();16 }17 }18 }
MyQuartzJob.java
1 package task; 2 3 import org.quartz.Job; 4 import org.quartz.JobExecutionContext; 5 import org.quartz.JobExecutionException; 6 7 public class MyQuartzJob implements Job { 8 private static int num = 0; 9 @Override10 public void execute(JobExecutionContext arg0) throws JobExecutionException {11 System.out.println("Hello World! " + num++);12 }13 }
quartz_jobs.xml
1 27 8 9 10 27 2811 17MyQuartzJob 1213 Server 14task.MyQuartzJob 1516 18 2619 25MyQuartzJobbTriger 20Server 21MyQuartzJob 22Server 230/3 * * * * ? 24
quartz.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 = 10#线ç¨ä¼å级å«ï¼ä¸è¬ä¸º5 org.quartz.threadPool.threadPriority = 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true#============================================================================ # 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 # fs notice : the classpath of quartz_jobs.xmlorg.quartz.plugin.jobInitializer.fileNames = quartz/quartz_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 = 60000 org.quartz.plugin.jobInitializer.wrapInUserTransaction = false