Spring Boot 2.0 Quartz - 使用非主数据源 [英] Spring Boot 2.0 Quartz - Use non-primary datasource

查看:103
本文介绍了Spring Boot 2.0 Quartz - 使用非主数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用 Quartz 作为调度程序.尝试使用 Spring Boot 2.0 功能.我在配置中有 2 个不同的数据源.一个用于应用程序,另一个用于调度程序.如何使用非主要数据源(在本例中为 schedulerDataSource)作为 Quartz 的数据源?请帮忙.

I used Quartz as scheduler in my application. Trying to use Spring boot 2.0 features. I have 2 different data sources in the configuration. One for application and another one for scheduler. How can I use non-primary data source (schedulerDataSource in this case) as data source for Quartz? Please help.

pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>       
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>wlthint3client</artifactId>
            <version>12.2.1.2</version>
            <scope>system</scope>
            <systemPath>C:/Oracle/products/mw_home/wlserver/server/lib/wlthint3client.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc</artifactId>
            <version>7</version>
            <scope>system</scope>
            <systemPath>C:/Oracle/products/mw_home/oracle_common/modules/oracle.jdbc/ojdbc7.jar</systemPath>
        </dependency>       
    </dependencies>

application.yml

spring:
  quartz:
    job-store-type: jdbc
    jdbc:
      initialize-schema: never
    properties:
      org:
        quartz:
          scheduler:
            instanceName: ETL
          threadPool:
            threadCount: 50
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
            tablePrefix: QRTZ_ 
            useProperties: true

scheduler:
  datasource:
    url: jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=XXXXXX)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XXXX)))
    username: scheduler
    password: XXXXXX
    driver-class-name: oracle.jdbc.OracleDriver

t3:
  datasource:
    url: jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=XXXXXX)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XXXXXX)))
    username: app
    password: XXXXXX
    driver-class-name: oracle.jdbc.OracleDriver

AppDataSource.java

@Configuration
public class AppDataSource 
{
    @Bean
    @Primary
    @ConfigurationProperties("t3.datasource")
    public DataSourceProperties t3DataSourceProperties() 
    {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("t3.datasource")
    public HikariDataSource t3DataSource() 
    {
        return t3DataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    @Bean
    @ConfigurationProperties("scheduler.datasource")
    public DataSourceProperties schedulerDataSourceProperties() 
    {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("scheduler.datasource")
    public HikariDataSource schedulerDataSource() 
    {
        return schedulerDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    @Bean
    public PlatformTransactionManager schedulerTransactionManager()
    {
        final DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(schedulerDataSource());

        return transactionManager;
    }
}

Application.java

package com.aaa.t3.starter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import com.spdji.aaa.jms.JmsService;

@ComponentScan("com.aaa.t3")
@SpringBootApplication(exclude = { ActiveMQAutoConfiguration.class, JmxAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
public class Application
{
    public static void main(String[] args) 
    {
        ApplicationContext context = SpringApplication.run(Application.class, args);
        JmsService jmsService = (JmsService) context.getBean("jmsService");
        jmsService.sendMessage();

        /*String[] beans = context.getBeanDefinitionNames();
        Arrays.stream(beans).sorted().forEach(System.out::println);*/
    }
}

尝试使用 SchedulerFactoryBeanCustomizer 修改数据源,但它仍然指向主数据源.

Tried to modify data source using SchedulerFactoryBeanCustomizer, but still it refers primary data source.

@Configuration
public class SchedulerConfig 
{
    private DataSource dataSource;

    @Autowired
    public SchedulerConfig(@Qualifier("schedulerDataSource") DataSource dataSource) 
    {
        this.dataSource = dataSource;
    }

    @Bean
    public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer() 
    {
        return bean -> bean.setDataSource(dataSource);
    }
}

使用调试器验证.SchedulerConfig 中自动连接的数据源是调度程序数据源,但之后它已被覆盖.在 QuartzAutoConfiguration.quartzDataSourceInitializer 中添加断点并检查数据源.这不是调度程序数据源.这已被主要数据源覆盖.Quartz 自动配置覆盖 SchedulerFactoryBeanCustomizer 定制.我已经打开 github.com/spring-projects/spring-boot/issues/12780 修复它.

Verified using debugger. Autowired data source in SchedulerConfig is scheduler data source, but it has been overridden after that. Added break point in QuartzAutoConfiguration.quartzDataSourceInitializer and checked the data source. This is not scheduler data source. This has been overridden by primary data source. Quartz auto configuration overrides SchedulerFactoryBeanCustomizer customization. I have opened github.com/spring-projects/spring-boot/issues/12780 to fix it.

这是 spring-boot 中的一个错误.作为一种解决方法,我删除了 spring.quartz.job-store-type 属性,然后在定制器中配置了 DataSource 和 PlatformTransactionManager.请参阅以下更新代码:

This is a bug in spring-boot. As a workaround, I removed spring.quartz.job-store-type property and then configured DataSource and PlatformTransactionManager in customizer. Refer below updated code:

@Configuration
public class SchedulerConfig 
{
    private DataSource dataSource;

    private PlatformTransactionManager transactionManager;

    @Autowired
    public SchedulerConfig(@Qualifier("schedulerDataSource") DataSource dataSource, @Qualifier("schedulerTransactionManager") PlatformTransactionManager transactionManager) 
    {
        this.dataSource = dataSource;
        this.transactionManager = transactionManager;
    }

    @Bean
    public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer() 
    {
        return bean -> 
        {
            bean.setDataSource(dataSource);
            bean.setTransactionManager(transactionManager);
        };
    }
}

推荐答案

这是 spring-boot 中的一个错误.作为一种解决方法,我删除了 spring.quartz.job-store-type 属性,然后在定制器中配置了 DataSource 和 PlatformTransactionManager.请参阅以下更新代码:

This is a bug in spring-boot. As a workaround, I removed spring.quartz.job-store-type property and then configured DataSource and PlatformTransactionManager in customizer. Refer below updated code:

@Configuration
public class SchedulerConfig 
{
    private DataSource dataSource;

    private PlatformTransactionManager transactionManager;

    @Autowired
    public SchedulerConfig(@Qualifier("schedulerDataSource") DataSource dataSource, @Qualifier("schedulerTransactionManager") PlatformTransactionManager transactionManager) 
    {
        this.dataSource = dataSource;
        this.transactionManager = transactionManager;
    }

    @Bean
    public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer() 
    {
        return bean -> 
        {
            bean.setDataSource(dataSource);
            bean.setTransactionManager(transactionManager);
        };
    }
}

这篇关于Spring Boot 2.0 Quartz - 使用非主数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆