Quartz Scheduler: How to dynamically read a quartz property from Java via API?

微笑、不失礼 提交于 2019-12-08 05:40:24

问题


Quartz is usually configured via quartz.properties on the classpath.

e.g.:

org.quartz.scheduler.instanceName = BagginsScheduler
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool   
org.quartz.threadPool.threadCount=5  
org.quartz.threadPool.threadPriority=1  

From within the same application that will run the Quartz jobs, I'd like to read out the properties.

Reading the scheduler name is easy:

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();  
String name = scheduler.getSchedulerName();

But how can I read the `threadPriority' property?

The following does not work:

scheduler.getContext().getString("org.quartz.threadPool.threadPriority");

UPDATED Solution: It seems that the property can't be read via Quartz API, you have to go via regular Properties:

Properties prop = new Properties();
prop.load(AnyClassUsedByJVM.class.getClassLoader().getResourceAsStream("quartz.properties"));
String prio = prop.getProperty("org.quartz.threadPool.threadPriority");

This works fine.


回答1:


You can just add that property to your quartz.properties. For example:

org.quartz.threadPool.threadPriority=3

For more information, see here and configuration documentation

EDIT: To read properties at runtime, you can use Properties. Here's a sample snippet of code you can use:

Properties p = new Properties();
p.load("/tmp/quartz.properties"); // path to your properties file
System.out.println(p.getProperty("org.quartz.threadPool.threadPriority"); // prints 3


来源:https://stackoverflow.com/questions/14103150/quartz-scheduler-how-to-dynamically-read-a-quartz-property-from-java-via-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!