配置文件源码解析
博主的前一篇博文主要介绍了quartz的入门使用,现在博主就来讲解一下quartz配置文件的加载流程。
代码回顾
public class Main {
public static void main(String[] args) throws SchedulerException {
//创建一个jobDetail的实例,将该实例与HelloJob Class绑定
JobDetail jobDetail = JobBuilder.newJob(TestJob.class).withIdentity("myJob").build();
//创建一个Trigger触发器的实例,定义该job立即执行,并且每2秒执行一次,一直执行
SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger").startNow().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever()).build();
//创建schedule实例
StdSchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail,trigger);
}
}
在上面的StdSchedulerFactory对象实例化的地方,打上断点,然后在debug模式下,就可以查看到quartz是怎么样加载配置文件的。
配置文件的加载流程
public void initialize() throws SchedulerException
{
// short-circuit if already initialized
if(cfg != null)
{
return;
}
if(initException != null)
{
throw initException;
}
//查看系统变量是否指定了配置文件的路径
String requestedFile = System.getProperty(PROPERTIES_FILE);
String propFileName = requestedFile != null ? requestedFile : "quartz.properties";
//没有则在当前项目的根目录下查找
File propFile = new File(propFileName);
Properties props = new Properties();
InputStream in = null;
try
{
if(propFile.exists())
{
try
{
if(requestedFile != null)
{
propSrc = "specified file: '" + requestedFile + "'";
}
else
{
propSrc = "default file in current working dir: 'quartz.properties'";
} in = new BufferedInputStream(new FileInputStream(propFileName));
props.load( in );
}
catch(IOException ioe)
{
initException = new SchedulerException("Properties file: '" + propFileName + "' could not be read.", ioe);
throw initException;
}
}
else if(requestedFile != null)
{ in = Thread.currentThread().getContextClassLoader().getResourceAsStream(requestedFile);
if( in == null)
{
initException = new SchedulerException("Properties file: '" + requestedFile + "' could not be found.");
throw initException;
}
propSrc = "specified file: '" + requestedFile + "' in the class resource path."; in = new BufferedInputStream( in );
try
{
props.load( in );
}
catch(IOException ioe)
{
initException = new SchedulerException("Properties file: '" + requestedFile + "' could not be read.", ioe);
throw initException;
}
}
else
{
propSrc = "default resource file in Quartz package: 'quartz.properties'";
//使用类加载器加载
ClassLoader cl = getClass().getClassLoader();
if(cl == null) cl = findClassloader();
if(cl == null) throw new SchedulerConfigException("Unable to find a class loader on the current thread or class."); in = cl.getResourceAsStream("quartz.properties");
if( in == null)
{ in = cl.getResourceAsStream("/quartz.properties");
}
if( in == null)
{ in = cl.getResourceAsStream("org/quartz/quartz.properties");
}
if( in == null)
{
initException = new SchedulerException("Default quartz.properties not found in class path");
throw initException;
}
try
{
props.load( in );
}
catch(IOException ioe)
{
initException = new SchedulerException("Resource properties file: 'org/quartz/quartz.properties' " + "could not be read from the classpath.", ioe);
throw initException;
}
}
}
finally
{
if( in != null)
{
try
{ in .close();
}
catch(IOException ignore)
{ /* ignore */ }
}
}
initialize(overrideWithSysProps(props));
}
通过查看源码可知,quartz的配置文件加载流程如下:
- 查看是否有系统变量指定了配置文件路径
- 在当前项目下面查找名为quartz.properties的文件。
- 通过类加载器加载当前classpath路径下的quartz.properties,/quartz.properties,org/quartz/quartz.properties的资源文件。
这里需要注意的是,一旦自己指定了quartz.properties文件,那么quartz默认提供的配置将不会被加载,因此也就不会生效。
查看quartz默认配置文件
分析上面的源码,我们可以在quartzjar包中的org/quartz目录下找到对应的配置文件quartz.properties。
# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#实例名称
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
#远程方法调用
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
#是否将任务放在事务中执行
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
#线程池
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
#线程个数
org.quartz.threadPool.threadCount: 10
#线程优先级
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
#失火的阈值
org.quartz.jobStore.misfireThreshold: 60000
#使用内存方式存储
org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore