如何确保使用quartz.properties?

时间:2013-02-05 12:35:14

标签: java threadpool quartz-scheduler

我在quartz.properties文件中设置了以下属性:

org.quartz.threadPool.threadCount = 60
org.quartz.scheduler.batchTriggerAcquisitionMaxCount = 60
但是,出于某种原因,显然它没有生效。因为当我启动我的应用程序时,日志显示它仍然在池中使用1个线程:

[main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor 
[main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 
[main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.1.1 created. 
[main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
[main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.1.1) 'QuartzScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0   
Using **thread pool 'org.quartz.simpl.SimpleThreadPool' - with 1 threads.**

我知道,quartz.properties需要在类路径中找到。而我刚刚做到了 为什么没有检测到这个文件的任何其他原因?或检测到但线程数未正确设置?

由于

3 个答案:

答案 0 :(得分:7)

对于那些使用Spring + Quartz并且quartz.properties文件不起作用的人(即在启动应用程序时被忽略):

由Spring Factory Bean(org.quartz.Scheduler)实例化的Quartz Scheduler(org.springframework.scheduling.quartz.SchedulerFactoryBean)默认情况下不会从类路径中读取quartz.properties文件,因为它在Quartz文档中说过 - 您需要设置手动引用:

[如果是Java配置]:

@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
    schedulerFactoryBean.setConfigLocation(new ClassPathResource("quartz.properties"));
    // ...
}

[如果是XML配置]:

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="configLocation" value="classpath:quartz.properties" />
    // ...
</bean>

答案 1 :(得分:1)

哎呀,我发现了问题,实际上代码是通过在代码中创建属性类的实例来覆盖属性文件配置。所以答案是这一行:

sf = new StdSchedulerFactory("conf/quartz.properties");

答案 2 :(得分:0)

如果有人仍在寻找答案,他们可以使用以下代码段

@Bean                                                                           
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
    SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
    scheduler.setTriggers(jobOneTrigger());
    scheduler.setQuartzProperties(quartzProperties());
    scheduler.setJobDetails(jobOneDetail());
    return scheduler;
}

@Bean
public Properties quartzProperties() throws IOException {
    PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
    propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
    propertiesFactoryBean.afterPropertiesSet();
    return propertiesFactoryBean.getObject();
}