45 lines
1.5 KiB
Java
45 lines
1.5 KiB
Java
package com.cygnus.cloud.database;
|
|
|
|
import io.vertx.core.Vertx;
|
|
import io.vertx.pgclient.PgConnectOptions;
|
|
import io.vertx.pgclient.PgBuilder;
|
|
import io.vertx.sqlclient.Pool;
|
|
import io.vertx.sqlclient.PoolOptions;
|
|
import java.util.concurrent.TimeUnit;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
@Configuration
|
|
public class VertxDatabaseConfiguration {
|
|
|
|
@Bean(destroyMethod = "close")
|
|
Vertx vertx() {
|
|
return Vertx.vertx();
|
|
}
|
|
|
|
@Bean(destroyMethod = "close")
|
|
Pool postgresPool(Vertx vertx, DatabaseProperties properties) {
|
|
PgConnectOptions connection = new PgConnectOptions()
|
|
.setHost(properties.host())
|
|
.setPort(properties.port())
|
|
.setDatabase(properties.database())
|
|
.setUser(properties.username())
|
|
.setPassword(properties.password())
|
|
.setSslMode(properties.ssl()
|
|
? io.vertx.pgclient.SslMode.REQUIRE
|
|
: io.vertx.pgclient.SslMode.DISABLE);
|
|
|
|
PoolOptions pool = new PoolOptions()
|
|
.setMaxSize(properties.poolSize())
|
|
.setMaxWaitQueueSize(properties.poolWaitQueueSize())
|
|
.setConnectionTimeout(Math.toIntExact(properties.connectTimeout().toMillis()))
|
|
.setConnectionTimeoutUnit(TimeUnit.MILLISECONDS);
|
|
|
|
return PgBuilder.pool()
|
|
.using(vertx)
|
|
.connectingTo(connection)
|
|
.with(pool)
|
|
.build();
|
|
}
|
|
}
|