QuartZ1.8基本配置例子

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 = 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.JobInitializationPlugin
#org.quartz.plugin.jobInitializer.fileNames = quartz_jobx.xml
#org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
#org.quartz.plugin.jobInitializer.failOnFileNotFound = true
#org.quartz.plugin.jobInitializer.scanInterval = 10
#org.quartz.plugin.jobInitializer.wrapInUserTransaction =false

#org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
#如果这里不指定,默认会读取WEB-INFO/CLASSES目录下的quartz_jobs.xml文件,名称不能改变
#如果有多个job xml文件,使用","隔开
org.quartz.plugin.jobInitializer.fileNames = quartz_jobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown =true

以上是配置属性,调用工厂的时候先调用quartz.properties文件

quartz_jobs.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">
 <schedule> 
  <job>
   <name>HelloWorldJob</name> 
   <group>DEFAULT</group>
   <description>
   	this is HelloWorld......
   </description>
   <job-class>org.xuliang.jobs.MyJob1</job-class> 
   <volatility>false</volatility> 
   <durability>false</durability> 
   <recover>false</recover>
  </job>
  <trigger>
	  <simple> 
	   <name>helloWorldTrigger</name> 
	   <job-name>HelloWorldJob</job-name> 
	   <start-time>2012-06-07T18:54:25</start-time> 
	   <repeat-count>5</repeat-count> 
	   <repeat-interval>1000</repeat-interval> 
	  </simple> 
  </trigger> 
 
  <job>
   <name>aa</name> 
   <group>DEFAULT</group>
   <job-class>org.xuliang.jobs.MyJob1</job-class> 
   <volatility>false</volatility> 
   <durability>false</durability> 
   <recover>false</recover>
  </job>
  <trigger>
	  <cron>
	  	<name>aa_trigg</name> 
	   <job-name>aa</job-name> 
	   <start-time>2012-06-15T00:54:25</start-time> 
	  	<cron-expression>* * * * * ?</cron-expression>
	  </cron> 
  </trigger>
 </schedule> 
</job-scheduling-data>


SimpleScheduler.java

package org.xuliang.tests;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.quartz.CronTrigger;
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 org.xuliang.jobs.MyJob1;

//此项目的例子,说明简单的Quartz运行的时候,2种方式运行Quartz的方式
//声明式:增加xml和properteis文件
//编程式:删除xml和properties文件,使用scheduleJob方法
public class SimpleScheduler {

	public void scheduleJob(Scheduler sch) throws SchedulerException{
		JobDetail jd = new JobDetail("ScanDirectory", Scheduler.DEFAULT_GROUP, MyJob1.class);
		jd.getJobDataMap().put("SCAN_DIR", "src");
		
		//每3秒钟执行一次
		Trigger trig = TriggerUtils.makeSecondlyTrigger(3);
		trig.setStartTime(new Date());//从现在开始
		trig.setName(jd.getName()+"-Trigger");
		
		sch.scheduleJob(jd, trig);
	}
	
	public void test1(Scheduler sch) throws SchedulerException, ParseException{
		JobDetail jd = new JobDetail("job1", Scheduler.DEFAULT_GROUP, MyJob1.class);
		
		CronTrigger ct = new CronTrigger(jd.getName()+"-trig",Scheduler.DEFAULT_GROUP,"50 * * * * ?");
		
		sch.scheduleJob(jd,ct);
	}
	
	//见源码,有X种方式获得工厂,注意工厂的构造函数,和initialize 方法
	public Scheduler getScheduler() throws SchedulerException{
		return new StdSchedulerFactory("quartz.properties").getScheduler();
		
//		return StdSchedulerFactory.getDefaultScheduler();
	}
	
	public static void main(String[] args) throws SchedulerException, ParseException {
		SimpleScheduler ss = new SimpleScheduler();
		Scheduler sch = ss.getScheduler(); 
//		ss.test1(sch);
		sch.start();
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值