背景:
之前项目里面把quartz相关的表跟业务数据库(涉及系统业务的库)融合在一起,后面需要把quartz单独拎出来放在一个数据库里面,
旧的数据源配置(application.properties),无其他,程序可以跑:
spring.datasource.password=root spring.datasource.username=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/fdata?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
quartz独立数据库:
1、增加quartz数据源配置(application.properties):
# quartz datasource conf spring.datasource.quartz.password=root spring.datasource.quartz.username=root spring.datasource.quartz.driver-class-name=com.mysql.jdbc.Driver spring.datasource.quartz.url=jdbc:mysql://localhost:3306/fdata_quartz_test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
2、增加DataSourceConfig类
加入了上述配置但是这样配置的datasouce并不生效,用的还是原来的数据库。查了些网上的资料,要给scheduler单独的dataSource,而且发现springboot文档里有
"To have Quartz use a DataSource other than the application’s main DataSource, declare a DataSource bean, annotating its @Bean method with @QuartzDataSource. Doing so ensures that the Quartz-specific DataSource is used by both the SchedulerFactoryBean and for schema initialization."
翻译:要让Quartz使用除应用程序主数据源之外的其他数据源,需要声明一个数据源bean,并用@QuartzDataSource注释它的@Bean方法。这样做可以确保SchedulerFactoryBean和模式初始化都使用特定于quartzs的数据源。
所以增加 DataSourceConfig类,如下:
1 package com.xx.fdata.console.conf; 2 import javax.sql.DataSource; 3 import lombok.extern.slf4j.Slf4j; 4 import org.apache.commons.dbcp.BasicDataSource; 5 import org.springframework.boot.autoconfigure.quartz.QuartzDataSource; 6 import org.springframework.boot.context.properties.ConfigurationProperties; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 import org.springframework.context.annotation.Primary; 10 11 @Slf4j 12 @Configuration 13 public class DataSourceConfig { 14 15 /** 16 * 数据源配置的前缀,必须与application.properties中配置的对应数据源的前缀一致 17 */ 18 private static final String BUSINESS_DATASOURCE_PREFIX = "spring.datasource"; 19 20 private static final String QUARTZ_DATASOURCE_PREFIX = "spring.datasource.quartz"; 21 22 @Primary 23 @Bean 24 @ConfigurationProperties(prefix = BUSINESS_DATASOURCE_PREFIX) 25 public BasicDataSource businessDataSource() { 26 return new BasicDataSource(); 27 } 28 29 /** 30 * @QuartzDataSource 注解则是配置Quartz独立数据源的配置 31 */ 32 @Bean 33 @QuartzDataSource 34 @ConfigurationProperties(prefix = QUARTZ_DATASOURCE_PREFIX) 35 public DataSource quartzDataSource(){ 36 return new BasicDataSource(); 37 } 38 }
3、使用的是dbcp连接池,所以pom增加相应配置:
<dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency>
参考资料:
2、springboot 2.x 集成quartz持久化到一个单独的dataSource时遇到的坑
补充:
完成需求过程中最初是在quartz.properties里面增加quartzDataSource内容,但是没什么效果,还是用的业务数据库,后面就有了前面提到的DataSourceConfig类的诞生:
spring.quartz.properties.org.quartz.jobStore.dataSource=quartzDataSource #数据源 spring.quartz.properties.org.quartz.dataSource.quartzDataSource.driver=com.mysql.jdbc.Driver spring.quartz.properties.org.quartz.dataSource.quartzDataSource.URL=jdbc:mysql://localhost:3306/fdata_quartz_test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true spring.quartz.properties.org.quartz.dataSource.quartzDataSource.user=root spring.quartz.properties.org.quartz.dataSource.quartzDataSource.password=root spring.quartz.properties.org.quartz.dataSource.quartzDataSource.validationQuery=select 1