How to customize a profile preset? #1016
-
I want to base my wrapper profile on @Profile("!local")
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(DataSourceAutoConfiguration::class)
@EnableConfigurationProperties(AwsHikariConfigurationProperties::class)
class AwsJdbcConfiguration(props: AwsHikariConfigurationProperties) {
init {
LOG.info("setting AWS Hikari configuration: {}", props)
val hikariPoolConfigurator = HikariPoolConfigurator { _, _ ->
HikariConfig().apply {
maximumPoolSize = props.maximumPoolSize
minimumIdle = props.minimumIdle
idleTimeout = props.idleTimeout.toMillis()
initializationFailTimeout = props.initializationFailTimeout.toMillis()
connectionTimeout = props.connectionTimeout.toMillis()
keepaliveTime = props.keepaliveTime.toMillis()
validationTimeout = props.validationTimeout.toMillis()
maxLifetime = props.maxLifetime.toMillis()
}
}
val connectionProvider = HikariPooledConnectionProvider(hikariPoolConfigurator, null)
ConfigurationProfileBuilder.get()
.from(ConfigurationProfilePresetCodes.D0)
.withName("custom")
.withConnectionProvider(connectionProvider)
.buildAndSet()
}
companion object {
private val LOG = KotlinLogging.logger {}
}
}
@Validated
@ConfigurationProperties("spring.datasource.aws.hikari")
data class AwsHikariConfigurationProperties(
@Positive
val maximumPoolSize: Int,
@Positive
val minimumIdle: Int = DEFAULT_MINIMUM_IDLE,
@DurationMin(millis = 0L)
val idleTimeout: Duration = DEFAULT_IDLE_TIMEOUT,
val initializationFailTimeout: Duration = DEFAULT_INITIALIZATION_FAIL_TIMEOUT,
@DurationMin(millis = 0L)
val connectionTimeout: Duration = DEFAULT_CONNECTION_TIMEOUT,
@DurationMin(millis = 0L)
val keepaliveTime: Duration = DEFAULT_KEEPALIVE_TIME,
@DurationMin(millis = 0L)
val validationTimeout: Duration = DEFAULT_VALIDATION_TIMEOUT,
@DurationMin(millis = 0L)
val maxLifetime: Duration = DEFAULT_MAX_LIFETIME,
) {
companion object {
// defaults match ConfigurationProfilePresetCodes.D0
private const val DEFAULT_MINIMUM_IDLE = 2
private val DEFAULT_IDLE_TIMEOUT = Duration.ofMinutes(15L)
private val DEFAULT_INITIALIZATION_FAIL_TIMEOUT = Duration.ofMillis(-1L)
private val DEFAULT_CONNECTION_TIMEOUT = Duration.ofSeconds(10L)
private val DEFAULT_KEEPALIVE_TIME = Duration.ofMinutes(3L)
private val DEFAULT_VALIDATION_TIMEOUT = Duration.ofSeconds(1L)
private val DEFAULT_MAX_LIFETIME = Duration.ofDays(1L)
}
} Then the JDBC URL would just have a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I do have this working now. It would be a great addition to the project to have a Spring Boot auto-configuration library so we don't have to write this stuff ourselves. I can't share the full code, but here's the @Configuration(proxyBeanMethods = false)
@ConditionalOnProperty("aws.jdbc.profile-preset")
@EnableConfigurationProperties(AwsJdbcDriverProperties::class)
class AwsJdbcDriverConfiguration {
@Bean
fun dataSource(dataSourceProps: DataSourceProperties, awsJdbcDriverProps: AwsJdbcDriverProperties): DataSource {
val connectionProfile = ConnectionProfileBuilder.buildConfigurationProfile(awsJdbcDriverProps)
DriverConfigurationProfiles.addOrReplaceProfile(connectionProfile.name, connectionProfile)
val properties = connectionProfile.properties ?: Properties()
properties.setProperty(PropertyDefinition.USER, dataSourceProps.username)
properties.setProperty(PropertyDefinition.PASSWORD, dataSourceProps.password)
return SimpleDriverDataSource(Driver(), dataSourceProps.url, properties)
}
}
@Validated
@ConfigurationProperties("aws.jdbc")
data class AwsJdbcDriverProperties(
val profilePreset: ProfilePreset,
@NotBlank
val profileName: String? = null,
val connectionPluginFactories: List<Class<ConnectionPluginFactory>> = emptyList(),
@DurationMin(millis = 1L)
val connectTimeout: Duration? = null,
@DurationMin(millis = 1L)
val socketTimeout: Duration? = null,
@DurationMin(millis = 1L)
val loginTimeout: Duration? = null,
@DurationMin(millis = 1L)
val tcpKeepAlive: Boolean? = null,
@NestedConfigurationProperty
val monitoring: MonitoringProperties? = null,
@NestedConfigurationProperty
val hikari: HikariProperties? = null,
) {
fun name() = profileName ?: "${profilePreset}_custom"
}
@Suppress("unused")
enum class ProfilePreset {
A0, A1, A2, B, C0, C1, D0, D1, E, F0, F1, G0, G1, H, I0, I1, SF_D0, SF_D1, SF_E, SF_F0, SF_F1
}
@Validated
data class MonitoringProperties(
@DurationMin(millis = 1L)
val failureDetectionTime: Duration? = null,
@Positive
val failureDetectionCount: Int? = null,
@DurationMin(millis = 1L)
val failureDetectionInterval: Duration? = null,
@DurationMin(millis = 1L)
val connectTimeout: Duration? = null,
@DurationMin(millis = 1L)
val socketTimeout: Duration? = null,
@DurationMin(millis = 1L)
val loginTimeout: Duration? = null,
)
@Validated
data class HikariProperties(
@Positive
val maximumPoolSize: Int? = null,
@Positive
val minimumIdle: Int? = null,
@DurationMin(millis = 1L)
val idleTimeout: Duration? = null,
val initializationFailTimeout: Duration? = null,
@DurationMin(millis = 250L)
val connectionTimeout: Duration? = null,
@DurationMin(millis = 1L)
val keepaliveTime: Duration? = null,
@DurationMin(millis = 250L)
val validationTimeout: Duration? = null,
@DurationMin(millis = 1L)
val maxLifetime: Duration? = null,
) Of course
|
Beta Was this translation helpful? Give feedback.
I do have this working now. It would be a great addition to the project to have a Spring Boot auto-configuration library so we don't have to write this stuff ourselves. I can't share the full code, but here's the
@Configuration
and all the@ConfigurationProperties
classes: