Quartz的Properties文件解析
阿新 • • 發佈:2018-04-05
分享 spa exceptio 次數 HR 詳解 art 函數 wrap
將可變信息放在properties文件是使配置更加靈活。
1.文檔位置和加載順序
1. StdSchedulerFactory默認加載quartz包下的quartz.properties文件,如果我們在項目下面新建一個quartz.properties文件,會優先加載我們的配置文件。
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
2. 現在我們測試我們自己在工程目錄下新建一個quartz.propertis文件是否優先讀取我們的配置文件:
內容:主要是將線程數量修改為-5
# Default Properties file for use by StdSchedulerFactory # to create a Quartz Scheduler Instance, ifa 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: -5 org.quartz.threadPool.threadPriority: 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true org.quartz.jobStore.misfireThreshold: 60000 org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
3.我們再次啟動quartz任務:
Job定義:
package cn.qlq.quartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //打印當前的時間 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); System.out.println("current exec time is :"+sf.format(date)); } }
package cn.qlq.quartz; import static org.quartz.JobBuilder.newJob; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) { try { // 1. 創建一個JodDetail實例 將該實例與Hello job class綁定 (鏈式寫法) JobDetail jobDetail = newJob(HelloJob.class) // 定義Job類為HelloQuartz類,這是真正的執行邏輯所在 .withIdentity("myJob") // 定義name/group .build(); // 打印當前的時間 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); System.out.println("current time is :" + sf.format(date)); // 2. 2018年內每天11點18開始執行,每隔5s執行一次 CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1")// 定義名字和組 .withSchedule( //定義任務調度的時間間隔和次數 CronScheduleBuilder .cronSchedule("* * * * * ? *") ) .build(); // 3. 創建scheduler SchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); // 4. 將trigger和jobdetail加入這個調度 scheduler.scheduleJob(jobDetail, trigger); // 5. 啟動scheduler scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } }
結果: (報錯,說線程數必須大於0)
current time is :2018-04-05 12:45:55 org.quartz.SchedulerConfigException: Thread count must be > 0 at org.quartz.simpl.SimpleThreadPool.initialize(SimpleThreadPool.java:245) at org.quartz.impl.StdSchedulerFactory.instantiate(StdSchedulerFactory.java:1273) at org.quartz.impl.StdSchedulerFactory.getScheduler(StdSchedulerFactory.java:1502) at cn.qlq.quartz.HelloScheduler.main(HelloScheduler.java:39)
二、quartz.properties文件詳解:
1.quartz.properties組成部分
調度器屬性 線程池屬性 作業存儲設置 插件配置
2. 調度器屬性
3.線程池屬性
threadCount:工作線程數量
threadPriority:工作線程優先級
org.quartz.threadPool.class:配置線程池實現類
4.作業存儲設置
描述了在調度器實例的生命周期中,Job和Trigger信息是如何被存儲的
5. 插件配置
滿足特定需求用到的Quartz插件的配置
6.最後附一個項目中常用的配置quartz.properties
# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#
# ===========================================================================
# Configure Main Scheduler Properties 調度器屬性
# ===========================================================================
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.instanceid:AUTO
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
# ===========================================================================
# Configure ThreadPool 線程池屬性
# ===========================================================================
#線程池的實現類(一般使用SimpleThreadPool即可滿足幾乎所有用戶的需求)
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
#指定線程數,至少為1(無默認值)(一般設置為1-100直接的整數合適)
org.quartz.threadPool.threadCount: 10
#設置線程的優先級(最大為java.lang.Thread.MAX_PRIORITY 10,最小為Thread.MIN_PRIORITY 1,默認為5)
org.quartz.threadPool.threadPriority: 5
#設置SimpleThreadPool的一些屬性
#設置是否為守護線程
#org.quartz.threadpool.makethreadsdaemons = false
#org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
#org.quartz.threadpool.threadsinheritgroupofinitializingthread=false
#線程前綴默認值是:[Scheduler Name]_Worker
#org.quartz.threadpool.threadnameprefix=swhJobThead;
# 配置全局監聽(TriggerListener,JobListener) 則應用程序可以接收和執行 預定的事件通知
# ===========================================================================
# Configuring a Global TriggerListener 配置全局的Trigger監聽器
# MyTriggerListenerClass 類必須有一個無參數的構造函數,和 屬性的set方法,目前2.2.x只支持原始數據類型的值(包括字符串)
# ===========================================================================
#org.quartz.triggerListener.NAME.class = com.swh.MyTriggerListenerClass
#org.quartz.triggerListener.NAME.propName = propValue
#org.quartz.triggerListener.NAME.prop2Name = prop2Value
# ===========================================================================
# Configuring a Global JobListener 配置全局的Job監聽器
# MyJobListenerClass 類必須有一個無參數的構造函數,和 屬性的set方法,目前2.2.x只支持原始數據類型的值(包括字符串)
# ===========================================================================
#org.quartz.jobListener.NAME.class = com.swh.MyJobListenerClass
#org.quartz.jobListener.NAME.propName = propValue
#org.quartz.jobListener.NAME.prop2Name = prop2Value
# ===========================================================================
# Configure JobStore 存儲調度信息(工作,觸發器和日歷等)
# ===========================================================================
# 信息保存時間 默認值60秒
org.quartz.jobStore.misfireThreshold: 60000
#保存job和Trigger的狀態信息到內存中的類
org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
# ===========================================================================
# Configure SchedulerPlugins 插件屬性 配置
# ===========================================================================
# 自定義插件
#org.quartz.plugin.NAME.class = com.swh.MyPluginClass
#org.quartz.plugin.NAME.propName = propValue
#org.quartz.plugin.NAME.prop2Name = prop2Value
#配置trigger執行歷史日誌(可以看到類的文檔和參數列表)
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin
org.quartz.plugin.triggHistory.triggerFiredMessage = Trigger {1}.{0} fired job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy}
org.quartz.plugin.triggHistory.triggerCompleteMessage = Trigger {1}.{0} completed firing job {6}.{5} at {4, date, HH:mm:ss MM/dd/yyyy} with resulting trigger instruction code: {9}
#配置job調度插件 quartz_jobs(jobs and triggers內容)的XML文檔
#加載 Job 和 Trigger 信息的類 (1.8之前用:org.quartz.plugins.xml.JobInitializationPlugin)
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
#指定存放調度器(Job 和 Trigger)信息的xml文件,默認是classpath下quartz_jobs.xml
org.quartz.plugin.jobInitializer.fileNames = my_quartz_job2.xml
#org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
#自動掃描任務單並發現改動的時間間隔,單位為秒
org.quartz.plugin.jobInitializer.scanInterval = 10
#覆蓋任務調度器中同名的jobDetail,避免只修改了CronExpression所造成的不能重新生效情況
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
# ===========================================================================
# Sample configuration of ShutdownHookPlugin ShutdownHookPlugin插件的配置樣例
# ===========================================================================
#org.quartz.plugin.shutdownhook.class = \org.quartz.plugins.management.ShutdownHookPlugin
#org.quartz.plugin.shutdownhook.cleanShutdown = true
#
# Configure RMI Settings 遠程服務調用配置
#
#如果你想quartz-scheduler出口本身通過RMI作為服務器,然後設置“出口”標誌true(默認值為false)。
#org.quartz.scheduler.rmi.export = false
#主機上rmi註冊表(默認值localhost)
#org.quartz.scheduler.rmi.registryhost = localhost
#註冊監聽端口號(默認值1099)
#org.quartz.scheduler.rmi.registryport = 1099
#創建rmi註冊,false/never:如果你已經有一個在運行或不想進行創建註冊
# true/as_needed:第一次嘗試使用現有的註冊,然後再回來進行創建
# always:先進行創建一個註冊,然後再使用回來使用註冊
#org.quartz.scheduler.rmi.createregistry = never
#Quartz Scheduler服務端端口,默認是隨機分配RMI註冊表
#org.quartz.scheduler.rmi.serverport = 1098
#true:鏈接遠程服務調度(客戶端),這個也要指定registryhost和registryport,默認為false
# 如果export和proxy同時指定為true,則export的設置將被忽略
#org.quartz.scheduler.rmi.proxy = false
Quartz的Properties文件解析