Spring使用Quartz配置数据源的方法
一、使用quartz.properties文件配置数据源
(1)在quartz.properties文件中需做如下配置:
org.quartz.scheduler.instanceName = scheduler
org.quartz.scheduler.instanceId = AUTO
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.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.dataSource.myDS.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
org.quartz.dataSource.myDS.URL = jdbc:sqlserver://数据库所在的服务器的IP(比如:192.168.1.1):端口号(比如:1433);databaseName=数据库名称(比如:testDB)
org.quartz.dataSource.myDS.user = 用户名(比如:sa)
org.quartz.dataSource.myDS.password = 密码(比如:123456)
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery=select count(0) from
(2)在spring的配置文件中加入如下配置:
<!—管理类:配置Scheduler信息,与spring容器的生命周期建立联系等功能 -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
lazy-init="false" autowire="no">
<!—读取quartz.properties配置文件 -->
<property name="configLocation" value="classpath:quartz.properties"></property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="autoStartup" value="true" />
<property name="startupDelay" value="10" />
</bean>
(3)动态添加Job定时任务时,使用SchedulerFactory这个类的getScheduler()方法。
二、在Spring文件中通过dataSource指定一个Spring管理的数据源,如果指定了该属性,即使在quartz.properties文件中设定了读取DB的数据配置,也会被dataSource覆盖。
一、使用quartz.properties文件配置数据源
(1)在quartz.properties文件中需做如下配置:
org.quartz.scheduler.instanceName = scheduler
org.quartz.scheduler.instanceId = AUTO
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.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.dataSource.myDS.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
org.quartz.dataSource.myDS.URL = jdbc:sqlserver://数据库所在的服务器的IP(比如:192.168.1.1):端口号(比如:1433);databaseName=数据库名称(比如:testDB)
org.quartz.dataSource.myDS.user = 用户名(比如:sa)
org.quartz.dataSource.myDS.password = 密码(比如:123456)
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery=select count(0) from
(2)在spring的配置文件中加入如下配置:
<!—管理类:配置Scheduler信息,与spring容器的生命周期建立联系等功能 -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
lazy-init="false" autowire="no">
<!—读取quartz.properties配置文件 -->
<property name="configLocation" value="classpath:quartz.properties"></property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="autoStartup" value="true" />
<property name="startupDelay" value="10" />
</bean>
(3)动态添加Job定时任务时,使用SchedulerFactory这个类的getScheduler()方法。
二、在Spring文件中通过dataSource指定一个Spring管理的数据源,如果指定了该属性,即使在quartz.properties文件中设定了读取DB的数据配置,也会被dataSource覆盖。