使用 Oracle AQ 和通用连接池的 Spring 启动

问题描述 投票:0回答:1

我正在将 Oracle AQ 与 Spring Boot 一起使用,如下所示:

Gradle 依赖:

implementation "com.oracle.database.spring:oracle-spring-boot-starter-aqjms:23.4.0"

Jms配置:

    @Configuration
    @EnableJms
    public class JmsConfiguration {
    
        @Bean
        public ConnectionFactory connectionFactory(DataSource dataSource) {
            return AQjmsFactory.getQueueConnectionFactory(dataSource);
        }

现在我尝试根据此

oracle docs
通过application.yaml为Oracle通用连接池设置一些属性,但是属性未应用

spring:
  datasource:
    url: jdbc:oracle:thin:@localhost:1521/ORCLPDB1
    username: AQ_USER
    password: your_password
    oracleucp:
      initial-pool-size: 5
      min-pool-size: 10
      max-pool-size: 30
      connection-wait-timeout: 2
      connection-factory-class-name: oracle.jdbc.pool.OracleDataSource
      connection-pool-name: some_pool_name
    type: oracle.ucp.jdbc.PoolDataSource

如果我通过代码设置它们,那么它就可以工作:

import jakarta.jms.ConnectionFactory;
import jakarta.jms.JMSException;
import oracle.jakarta.jms.AQjmsFactory;
import oracle.ucp.jdbc.PoolDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;

import java.sql.SQLException;

@Configuration
@EnableJms
public class JmsConfiguration {

    @Bean
    public ConnectionFactory connectionFactory(PoolDataSource poolDataSource) throws JMSException, SQLException {
        poolDataSource.setInitialPoolSize(5);
        poolDataSource.setMinPoolSize(10);
        poolDataSource.setMaxPoolSize(30);
        poolDataSource.setConnectionWaitTimeout(3000);

        return AQjmsFactory.getQueueConnectionFactory(poolDataSource);
    }

要查看设置是否已应用,我使用以下命令:

import oracle.ucp.jdbc.PoolDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.event.EventListener;

import java.sql.SQLException;
import java.util.List;

@SpringBootApplication
@ConfigurationPropertiesScan
public class RequestBufferingServiceApplication {

    @Autowired
    PoolDataSource poolDataSource;

    public static void main(String[] args) {
        SpringApplication.run(RequestBufferingServiceApplication.class, args);
    }

    @EventListener(ApplicationReadyEvent.class)
    public void foo() throws SQLException {
        System.out.println("------");
        System.out.println("Init pool size: " + poolDataSource.getInitialPoolSize());
        System.out.println("Min pool size: " + poolDataSource.getMinPoolSize());
        System.out.println("Max pool size: " + poolDataSource.getMaxPoolSize());
        System.out.println("ConnectionWaitTimeout: " + poolDataSource.getConnectionWaitTimeout());
        System.out.println("------");
    }
}

如何通过

application.yaml
正确应用Oracle UCP属性?

编辑:

不受

application.yaml
影响的输出:

------
Init pool size: 0
Min pool size: 0
Max pool size: 2147483647
ConnectionWaitTimeout: 3
------

具有 java 配置影响的输出(所需):

------
Init pool size: 5
Min pool size: 10
Max pool size: 30
ConnectionWaitTimeout: 2
------

构建.gradle:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.3'
    id 'io.spring.dependency-management' version '1.1.6'
    id 'io.freefair.lombok' version '8.11'
    id "io.qameta.allure-report" version "2.11.2"
    id 'org.sonarqube' version '5.0.0.4638'
    id 'jacoco'
}

group = 'de.telekom.mff'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

bootJar {
    archiveFileName = "request-buffering-service.${archiveExtension.get()}"
}

jar {
    enabled = true
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

ext {
    versions = [
            oracleSpringBootStarterAqjms: "23.4.0",
            allureCucumber7Jvm          : "2.27.0",
            testcontainers              : "1.20.1",
            springCloud                 : "2023.0.3",
            awaitility                  : "4.2.2",
            xmlUnit                     : "2.10.0",
            jsonUnitAssertj             : "3.2.2",
            wiremock                    : "4.1.4"
    ]
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${versions.springCloud}"
    }
}

dependencies {
    implementation "org.springframework.boot:spring-boot-starter-web"
    implementation "org.springframework.boot:spring-boot-starter-webflux"
    implementation "org.springframework.boot:spring-boot-starter-log4j2"
    implementation "org.springframework.boot:spring-boot-starter-actuator"
    implementation "org.springframework:spring-aspects"
    implementation "org.springframework.cloud:spring-cloud-starter-kubernetes-fabric8-config"
    implementation "org.springframework.cloud:spring-cloud-kubernetes-fabric8-leader"
    implementation "com.oracle.database.spring:oracle-spring-boot-starter-aqjms:${versions.oracleSpringBootStarterAqjms}"
    testImplementation "org.springframework.boot:spring-boot-starter-test"
    testImplementation "io.fabric8:kubernetes-server-mock"
    testImplementation "org.springframework.cloud:spring-cloud-contract-wiremock:${versions.wiremock}"
    testImplementation "org.testcontainers:oracle-free:${versions.testcontainers}"
    testImplementation "org.testcontainers:junit-jupiter:${versions.testcontainers}"
    testImplementation "org.awaitility:awaitility:${versions.awaitility}"
    testImplementation "org.xmlunit:xmlunit-core:${versions.xmlUnit}"
    testImplementation "io.qameta.allure:allure-cucumber7-jvm:${versions.allureCucumber7Jvm}"
    testImplementation "net.javacrumbs.json-unit:json-unit-assertj:${versions.jsonUnitAssertj}"
    testRuntimeOnly "org.junit.platform:junit-platform-launcher"
}

Spring启动调试日志(使用23.4.0 aqjms starter,其中db的url、用户、密码通过application.yaml自动配置):

负匹配:

数据源自动配置: 不匹配: - @ConditionalOnClass 没有找到所需的类 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType' (课堂条件)

数据源初始化配置: 不匹配: - @ConditionalOnClass 没有找到所需的类 'org.springframework.jdbc.datasource.init.DatabasePopulator' (课堂条件)

数据源事务管理器自动配置: 不匹配: - @ConditionalOnClass 没有找到所需的类 'org.springframework.jdbc.core.JdbcTemplate' (OnClassCondition)

java spring oracle-database spring-boot oracle-aq
1个回答
0
投票

oracle-spring-boot-starter-aqjms
(版本 23.4.0)包含
DataSource
ConnectionFactory
的自动配置。这是包含此启动器时启动的配置。此配置不是
oracle-spring-boot-starter-ucp
的一部分,它将采用常规 Spring Boot 配置。

这解释了它为何有效。

因此,如果您想使用完整的 Spring Boot 数据源配置,您需要排除

com.oracle.spring.aqjms.AqJmsAutoConfiguration
的应用。

@SpringBootApplication(excludeName = {"com.oracle.spring.aqjms.AqJmsAutoConfiguration"})

或者您可以将排除添加到您的

application.yml

spring:
  autoconfigure:
    exclude: com.oracle.spring.aqjms.AqJmsAutoConfiguration

现在对于“oracle-spring-boot-starter-aqjms

version 24.4.0 things have changed and the auto configuration for the datasource moved to the
oracle-spring-boot-starter-ucp
instead. So for that to work you would need to exclude a different auto-config the
com.oracle.spring.ucp.UCPAutoConfiguration”,您可以通过上述任一方式排除它上面。

排除到位后,运行应用程序时,您现在将拥有正确配置的 Oracle 池数据源。或者至少是您期望在那里的那个。

© www.soinside.com 2019 - 2024. All rights reserved.