Quartz配置文件(quartz.properties)如何根据数据库的类型动态地获取jobstore驱动程序类

时间:2014-06-04 09:31:41

标签: web-applications quartz-scheduler

我在我的Web应用程序中使用Quartz,Jobstore为JDBC-JobStoreCMT,而不是默认的RAMJobStore。我的应用程序将根据客户使用Mssql或Oracle数据库。 每次我将数据库从Oracle更改为Mssql或其他时,我都必须更改quartz.properties文件中的 driverDelegateClass 属性值。我的quartz.properties文件是这样的

MSSQL -

# 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: DatabaseScheduler
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


# Changes for JDBCJobStoreTX

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreCMT
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.dataSource = quartzDataSource
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory

ORACLE -

org.quartz.scheduler.instanceName: NDFSScheduler
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: 25
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

org.quartz.jobStore.misfireThreshold: 60000


# Changes for JDBCJobStoreTX

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreCMT
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
org.quartz.jobStore.dataSource = quartzDataSource
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory

有没有办法,我不需要进行此设置,石英会根据数据库自动选择正确的 driverDelegateClass 。或者从不同文件中获取此属性(driverDelegateClass)的任何方式?

1 个答案:

答案 0 :(得分:2)

假设您使用StdSchedulerFactory#getScheduler方法在代码中以编程方式实例化Quartz调度程序实例,那么您可以使用以下两个StdSchedulerFactory构造函数之一,它允许您传递一组不同的属性(想想quartz.properties)。

StdSchedulerFactory(Properties props) 
StdSchedulerFactory(String fileName) 

注意:fileName可以是位于应用程序类路径上的资源,也可以是位于应用程序外部的文件。

在您的代码中,您可以根据应用程序的配置选择不同的quartz.properties文件(例如quartz-mssql.properties,quqrtz-oracle.properties),轻松地在各种数据库配置文件之间切换。

如果您使用Spring(即SchedulerFactoryBean类)来构造Quartz调度程序实例,那么您可以使用PropertyPlaceholderConfigurer来实现相同的功能。