码迷,mamicode.com
首页 > 其他好文 > 详细

quartz的使用

时间:2015-10-30 18:52:37      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

项目结构图:

技术分享

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  

 

  

quartz的使用

标签:

原文地址:http://www.cnblogs.com/TheoryDance/p/4923801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!