Quartz调用示例:
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
public class QuartzTest {
public static void main(String[] args) {
try {
// Grab the Scheduler instance from the Factory
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// and start it off
scheduler.start();
scheduler.shutdown();
} catch (SchedulerException se) {
se.printStackTrace();
}
}
}
可以看到,Quartz的核心调度器是通过工厂创建的。SchedulerFactory两种实现:
1.StdSchedulerFactory 根据Properties文件创造QuartzScheduler实例,支持一个JVM多实例创建。
默认的配置文件:quartz.properties.(优先级:当前工作目录>quarz包内)。也可以通过环境变量指定org.quartz.properties
2.DirectSchedulerFactory 工厂的单例实现。
工厂接口如下:
public interface SchedulerFactory{
//获取一个客户端可用的Scheduler
Scheduler getScheduler() throws SchedulerException;
Scheduler getScheduler(String schedName) throws SchedulerException;
Collection<Scheduler> getAllSchedulers() throws SchedulerException;
}
Std工厂构造Scheduler
获取默认调度器:getDefaultScheduler()
public Schedulerget Scheduler() throws SchedulerException{
if(cfg == null){
//加载配置文件
initialize();
}
//获得调度器仓库
SchedulerRepository schedRep = SchedulerRepository.getInstance();
//从仓库中加载配置名称的调度器,未配置获取默认名称"QuartzScheduler"的调度器
**Scheduler sched = schedRep.lookup(getSchedulerName());**
if(sched != null){
if(sched.isShutdown()){
schedRep.remove(getSchedulerName());
}else{
//返回已存在的实例
return sched;
}
}
//实例初始化
sched = instantiate();
return sched;
}
【单例】SchedulerRepository 调度器仓库,存放所有均由一个Classloader加载的调度器实例的引用,作用:保证调度器实例唯一性,防止GC,允许全局搜索。
内部用一个HashMap成员变量存放调度器实例。
private HashMap<String,Scheduler> schedulers;
提供lookup方法查找调度器 public synchronized Scheduler lookup(StringschedName){
return schedulers.get(schedName);
}
初始化Scheduler调度器实例,核心步骤【代码比较多】
JobStore js = null; //作业存储器
ThreadPool tp = null; //调度线程池
QuartzScheduler qs=null;//调度器实例
DBConnectionManager dbMgr = null;//jdbc管理
String instanceIdGeneratorClass=null;
Properties tProps = null; //线程执行器的一组配置信息
String userTXLocation=null;
Boolean wrapJobInTx = false; //是否将任务执行包装成事务
boolean autoId=false;
Long idleWaitTime = -1; //线程空闲时间
long dbFailureRetry=15000L;//15secs
String classLoadHelperClass;
String jobFactoryClass;
ThreadExecutor threadExecutor; //线程执行器
SchedulerRepository schedRep = SchedulerRepository.getInstance();
//GetSchedulerProperties
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//(1)此处代码为加载配置文件构造调度器信息
//(2)此处代码使用rmi获取远程调度器
//Get ThreadPool Properties
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//(2)此处代码为根据配置反射创建初始化线程池
//GetJobStoreProperties
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//(3)此处代码为JobStore配置
//Setup any DataSources
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//(4)此处代码为数据库配置
//注:**调度器必须资源管理类** --->这里用建造者模式改进~~
QuartzSchedulerResources rsrcs = new QuartzSchedulerResources();
rsrcs.setName(schedName); //调度器名称
rsrcs.setThreadName(threadName); //调度线程名称
rsrcs.setInstanceId(schedInstId); //实例ID
rsrcs.setJobRunShellFactory(jrsf); //作业执行终端工厂
rsrcs.setMakeSchedulerThreadDaemon(makeSchedulerThreadDaemon);
rsrcs.setThreadsInheritInitializersClassLoadContext(threadsInheritInitalizersClassLoader);
rsrcs.setRunUpdateCheck(!skipUpdateCheck);
rsrcs.setBatchTimeWindow(batchTimeWindow);
rsrcs.setMaxBatchSize(maxBatchSize);
rsrcs.setInterruptJobsOnShutdown(interruptJobsOnShutdown);
rsrcs.setInterruptJobsOnShutdownWithWait(interruptJobsOnShutdownWithWait);
rsrcs.setJMXExport(jmxExport);
rsrcs.setJMXObjectName(jmxObjectName);
SchedulerDetailsSetter.setDetails(tp,schedName,schedInstId);
//设置类执行器并初始化
rsrcs.setThreadExecutor(threadExecutor);
threadExecutor.initialize();
//设置线程池并初始化
rsrcs.setThreadPool(tp);
if(tp instanceof SimpleThreadPool){
if(threadsInheritInitalizersClassLoader)
((SimpleThreadPool)tp).setThreadsInheritContextClassLoaderOfInitializingThread(threadsInheritInitalizersClassLoader);
}
tp.initialize();
tpInited=true;
//配置jobstore
rsrcs.setJobStore(js);
//addplugins
for(inti=0;i<plugins.length;i++){
rsrcs.addSchedulerPlugin(plugins[i]);
}
//调度器实例正式初始化
qs = new QuartzScheduler(rsrcs,idleWaitTime,dbFailureRetry);
qsInited = true;
//CreateScheduler ref… 将qs传入StdScheduler,此方法只能获得默认的StdScheduler,StdScheduler为QuartzScheduler的包装类
Scheduler scheduler = instantiate(rsrcs,qs);