Quartz Scheduler:如何通过API从Java动态读取quartz属性?

Quartz通常通过类路径上的quartz.properties进行配置。

例如:

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

从运行Quartz作业的同一个应用程序中, 我想读出属性。

读取调度程序名称很简单:

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

但是我如何阅读`threadPriority’属性?

以下不起作用:

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

更新的解决方案 :似乎无法通过Quartz API读取属性,您必须通过常规Properties

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

这很好用。

您只需将该属性添加到quartz.properties 。 例如:

 org.quartz.threadPool.threadPriority=3 

有关更多信息,请参阅此处和配置文档

编辑 :要在运行时读取属性,可以使用“ 属性” 。 以下是您可以使用的示例代码段:

 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