Quartz任务调度快速入门进阶五——Quartz.properties配置文件

892 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

Quartz.properties

默认路径:quartz-2.3.0中的org.quartz中的quartz.properties

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

可以在项目的资源下添加quartz.properties文件,去覆盖底层的配置文件

组成部分

  • 调度器属性 org.quartz.scheduler.instanceName属性用来区分特定的调度器实例,可以按照功能用途来给调度器起名。

org.quartz.scheduler.instanceId属性和前者一样,也允许任何字符串,但这个值必须在所有调度器实例中是唯一的,尤其是在一个集群环境中,作为集群的唯一key。假如想Quartz生成该值,可以设置为AUTO。

  • 线程池属性 threadCount 处理Job的线程个数,至少为1,但最多的个数不要超过100,在多数机器上设置该值超过100的话就会显得相当不适用,特别是在Job执行时间较长的情况下

threadPriority 线程的优先级,优先级别高的线程比级别低的线程优先得到执行。最小为1,最大为10,默认为5。

org.quartz.threadPool.class 一个实现了org.quartz.sp.ThreadPool接口的类,Quartz自带的线程池实现类是org.quartz.simpl.SimpleThreadPool

  • 作业存储设置 描述了在调度器实例的生命周期中,Job和Trigger信息是如何被存储的。

  • 插件配置 满足特定需求用到的Quartz插件的配置。

示例:

#===========================================
#Configure Main Scheduler Properties        调度器属性
#===========================================
#调度器的实例名
org.quartz.scheduler.instanceName = QuartzScheduler
#调度器的示例ID,大多数情况设置为auto即可
org.quartz.scheduler.instanceId = AUTO

#===========================================
#Configure ThreadPool           线程池属性
#处理Job的线程个数,至少为1,但最多的话最好不要超过100,在多数机器上设置该值超过100的话就会显得相当不适用,特别是在Job执行时间较长的情况下
org.quartz.threadPool.threadCount = 5
#线程的优先级,优先级别高的线程比级别低的线程优先得到执行。最小为1,最大为10,默认为5
org.quartz.threadPool.threadPriority = 5
#一个实现了 org.quartz.spi.ThreadPool 接口的类,Quartz 自带的线程池实现类是 org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool

#===========================================
#Configure JobStore         作业存储设置
#===========================================
#要使 Job 存储在内存中需要通过设置,org.quartz.jobStore.class 属性为 org.quartz.simpl.RAMJobStore
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

#===========================================
#Configure Plugine         插件配置
#===========================================
org.quartz.plugin.jobInitalizer.class = org.quartz.plugins.xml.JobInitalizationPlugin

org.quartz.plugin.jobInitalizer.overWriteExistingJobs = true
org.quartz.plugin.jobInitalizer.failOnFileNotFound = true
org.quartz.plugin.jobInitalizer.validating = false

也可以通过代码设置quartz.properties文件的内容:

public class QuartzProperties {
    public static void main(String[] args){
        // 创建工厂示例
        StdSchedulerFactory factory = new StdSchedulerFactory();
        
        // 创建配置工厂的属性对象
        Properties props = new Properties();
        props.put(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, "org.quartz.simpl.SimpleThreadPool"); // 线程池定义
        prop.put("org.quartz.threadPool.threadCount", "5"); // 默认Scheduler的线程数
        
        try{
            // 使用定义的属性初始化工厂
            factory.initialize(props);
            
            Scheduler scheduler = factory.getScheduler();
            
            scheduler.start();
        }catch(SchedulerException e){
            e.printStackTrace();
        }
    }
}