quartz在application中的使用
项目结构图:
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的classpath 11 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 @Override 10 public void execute(JobExecutionContext arg0) throws JobExecutionException { 11 System.out.println("Hello World! " + num++); 12 } 13 }
quartz_jobs.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.opensymphony.com/quartz/JobSchedulingData 5 http://www.opensymphony.com/quartz/xml/job_scheduling_data_1_5.xsd" 6 version="2.0.2"> 7 8 <!-- 每3秒执行一次 --> 9 <job> 10 <job-detail> 11 <name>MyQuartzJob</name> 12 <description></description> 13 <group>Server</group> 14 <job-class>task.MyQuartzJob</job-class> 15 <job-data-map allows-transient-data="true" /> 16 </job-detail> 17 <trigger> 18 <cron> 19 <name>MyQuartzJobbTriger</name> 20 <group>Server</group> 21 <job-name>MyQuartzJob</job-name> 22 <job-group>Server</job-group> 23 <cron-expression>0/3 * * * * ?</cron-expression> 24 </cron> 25 </trigger> 26 </job> 27 28 </quartz>
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.xml org.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