|
@@ -0,0 +1,167 @@
|
|
|
+package com.zx.dataservice.config;
|
|
|
+
|
|
|
+import com.alibaba.druid.pool.DruidDataSource;
|
|
|
+import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
|
|
+import com.jfinal.plugin.activerecord.Db;
|
|
|
+import com.jfinal.plugin.activerecord.Record;
|
|
|
+import com.zx.dataservice.utils.PropertiesUtil;
|
|
|
+import com.zx.dataservice.utils.ZXOptions;
|
|
|
+import org.springframework.boot.ApplicationArguments;
|
|
|
+import org.springframework.boot.ApplicationRunner;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
+import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
|
|
|
+
|
|
|
+import javax.annotation.PreDestroy;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created on 2020/12/04.
|
|
|
+ *
|
|
|
+ * @author qlm
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+public class JfinalActiveRecordConfig implements ApplicationRunner {
|
|
|
+ /**
|
|
|
+ * 1数据源名称
|
|
|
+ */
|
|
|
+ private static final String GP_DATA_SOURCE_CONFIG = "gp";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 2数据源名称
|
|
|
+ */
|
|
|
+ private static final String ZQ_DATA_SOURCE_CONFIG = "zq";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Bean(name = "gpDataSource")
|
|
|
+// @ConfigurationProperties(prefix = "spring.datasource.gp")
|
|
|
+ public DruidDataSource gpDataSource()throws SQLException {
|
|
|
+ DruidDataSource dataSource = new DruidDataSource();
|
|
|
+ dataSource.setUrl(PropertiesUtil.getProperty("spring.datasource.test1.jdbc-Url"));
|
|
|
+ dataSource.setUsername(PropertiesUtil.getProperty("spring.datasource.test1.username"));
|
|
|
+ dataSource.setPassword(PropertiesUtil.getProperty("spring.datasource.test1.password"));
|
|
|
+ dataSource.setDriverClassName(PropertiesUtil.getProperty("spring.datasource.test1.driver-class-name"));
|
|
|
+ dataSource.setInitialSize(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.initialSize")));
|
|
|
+ dataSource.setMinIdle(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.minIdle")));
|
|
|
+ dataSource.setMaxActive(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.maxActive")));
|
|
|
+ dataSource.setMaxWait(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.maxWait")));
|
|
|
+ dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(PropertiesUtil.getProperty("spring.druid.timeBetweenEvictionRunsMillis")));
|
|
|
+ dataSource.setMinEvictableIdleTimeMillis(Long.parseLong(PropertiesUtil.getProperty("spring.druid.minEvictableIdleTimeMillis")));
|
|
|
+ dataSource.setValidationQuery(PropertiesUtil.getProperty("spring.druid.validationQuery"));
|
|
|
+ dataSource.setTestWhileIdle(Boolean.parseBoolean(PropertiesUtil.getProperty("spring.druid.testWhileIdle")));
|
|
|
+ dataSource.setTestOnBorrow(Boolean.parseBoolean(PropertiesUtil.getProperty("spring.druid.testOnBorrow")));
|
|
|
+ dataSource.setTestOnReturn(Boolean.parseBoolean(PropertiesUtil.getProperty("spring.druid.testOnReturn")));
|
|
|
+ dataSource.setPoolPreparedStatements(Boolean.parseBoolean(PropertiesUtil.getProperty("spring.druid.poolPreparedStatements")));
|
|
|
+ dataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.maxPoolPreparedStatementPerConnectionSize")));
|
|
|
+ dataSource.setRemoveAbandoned(Boolean.parseBoolean(PropertiesUtil.getProperty("spring.druid.removeAbandoned")));
|
|
|
+ dataSource.setRemoveAbandonedTimeout(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.removeAbandonedTimeout")));
|
|
|
+ dataSource.setFilters(PropertiesUtil.getProperty("spring.druid.filters"));
|
|
|
+ return dataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "zqDataSource")
|
|
|
+// @ConfigurationProperties(prefix = "spring.datasource.zq")
|
|
|
+ public DruidDataSource zqDataSource()throws SQLException {
|
|
|
+ DruidDataSource dataSource = new DruidDataSource();
|
|
|
+ dataSource.setUrl(PropertiesUtil.getProperty("spring.datasource.test2.jdbc-Url"));
|
|
|
+ dataSource.setUsername(PropertiesUtil.getProperty("spring.datasource.test2.username"));
|
|
|
+ dataSource.setPassword(PropertiesUtil.getProperty("spring.datasource.test2.password"));
|
|
|
+ dataSource.setDriverClassName(PropertiesUtil.getProperty("spring.datasource.test2.driver-class-name"));
|
|
|
+ dataSource.setInitialSize(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.initialSize")));
|
|
|
+ dataSource.setMinIdle(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.minIdle")));
|
|
|
+ dataSource.setMaxActive(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.maxActive")));
|
|
|
+ dataSource.setMaxWait(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.maxWait")));
|
|
|
+ dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(PropertiesUtil.getProperty("spring.druid.timeBetweenEvictionRunsMillis")));
|
|
|
+ dataSource.setMinEvictableIdleTimeMillis(Long.parseLong(PropertiesUtil.getProperty("spring.druid.minEvictableIdleTimeMillis")));
|
|
|
+ dataSource.setValidationQuery(PropertiesUtil.getProperty("spring.druid.validationQuery"));
|
|
|
+ dataSource.setTestWhileIdle(Boolean.parseBoolean(PropertiesUtil.getProperty("spring.druid.testWhileIdle")));
|
|
|
+ dataSource.setTestOnBorrow(Boolean.parseBoolean(PropertiesUtil.getProperty("spring.druid.testOnBorrow")));
|
|
|
+ dataSource.setTestOnReturn(Boolean.parseBoolean(PropertiesUtil.getProperty("spring.druid.testOnReturn")));
|
|
|
+ dataSource.setPoolPreparedStatements(Boolean.parseBoolean(PropertiesUtil.getProperty("spring.druid.poolPreparedStatements")));
|
|
|
+ dataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.maxPoolPreparedStatementPerConnectionSize")));
|
|
|
+ dataSource.setRemoveAbandoned(Boolean.parseBoolean(PropertiesUtil.getProperty("spring.druid.removeAbandoned")));
|
|
|
+ dataSource.setRemoveAbandonedTimeout(Integer.parseInt(PropertiesUtil.getProperty("spring.druid.removeAbandonedTimeout")));
|
|
|
+ return dataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 主数据源 gp
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public ActiveRecordPlugin initgpActiveRecord() throws SQLException{
|
|
|
+ ActiveRecordPlugin arp = new ActiveRecordPlugin(GP_DATA_SOURCE_CONFIG, gpTransactionAwareDataSourceProxy());
|
|
|
+ arp.start();
|
|
|
+ return arp;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 业务数据源 zq
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public ActiveRecordPlugin initzqActiveRecord()throws SQLException {
|
|
|
+ ActiveRecordPlugin arp = new ActiveRecordPlugin(ZQ_DATA_SOURCE_CONFIG, zqTransactionAwareDataSourceProxy());
|
|
|
+ arp.start();
|
|
|
+ return arp;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置数据源代理
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public TransactionAwareDataSourceProxy gpTransactionAwareDataSourceProxy() throws SQLException{
|
|
|
+ TransactionAwareDataSourceProxy transactionAwareDataSourceProxy = new TransactionAwareDataSourceProxy();
|
|
|
+ transactionAwareDataSourceProxy.setTargetDataSource(gpDataSource());
|
|
|
+ return transactionAwareDataSourceProxy;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public TransactionAwareDataSourceProxy zqTransactionAwareDataSourceProxy() throws SQLException {
|
|
|
+ TransactionAwareDataSourceProxy transactionAwareDataSourceProxy = new TransactionAwareDataSourceProxy();
|
|
|
+ transactionAwareDataSourceProxy.setTargetDataSource(zqDataSource());
|
|
|
+ return transactionAwareDataSourceProxy;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置事务管理
|
|
|
+ */
|
|
|
+ @Bean(name="mainDataSourceTransactionManager")
|
|
|
+ public DataSourceTransactionManager gpDataSourceTransactionManager() throws SQLException{
|
|
|
+ DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
|
|
|
+ dataSourceTransactionManager.setDataSource(gpTransactionAwareDataSourceProxy());
|
|
|
+ return dataSourceTransactionManager;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name="zqDataSourceTransactionManager")
|
|
|
+ public DataSourceTransactionManager zqDataSourceTransactionManager() throws SQLException{
|
|
|
+ DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
|
|
|
+ dataSourceTransactionManager.setDataSource(zqTransactionAwareDataSourceProxy());
|
|
|
+ return dataSourceTransactionManager;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run(ApplicationArguments args) throws Exception {
|
|
|
+
|
|
|
+ List<Record> records = Db.use("gp").find("select * from sys_config ");
|
|
|
+ for (Record record:records) {
|
|
|
+ ZXOptions.set(record.getStr("config_key"),record.getStr("config_value"));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @PreDestroy
|
|
|
+ public void destory() throws Exception {
|
|
|
+// QuantUtil quantUtil = new QuantUtil();
|
|
|
+// quantUtil.toGetCsqCancel(0);
|
|
|
+// System.out.println("取消所有订阅~");
|
|
|
+ }
|
|
|
+}
|