Skip to content

Commit

Permalink
Make mutiny version of pool use the already configured vertx pool
Browse files Browse the repository at this point in the history
Before this, the mutiny version would recreate the delegate instead
of reusing the existing bean

Fixes: quarkusio#39047
geoand committed Mar 7, 2024
1 parent e91bab4 commit d894149
Showing 14 changed files with 249 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -65,12 +65,16 @@

class ReactiveDB2ClientProcessor {

private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class),
private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create(
DotName.createSimple(Instance.class),
new Type[] { ClassType.create(DotName.createSimple(DB2PoolCreator.class.getName())) }, null);
private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0];

private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class);

private static final DotName VERTX_DB2_POOL = DotName.createSimple(DB2Pool.class);
private static final Type VERTX_DB2_POOL_TYPE = Type.create(VERTX_DB2_POOL, Type.Kind.CLASS);

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
ServiceStartBuildItem build(BuildProducer<FeatureBuildItem> feature,
@@ -208,7 +212,7 @@ private void createPoolIfDefined(DB2PoolRecorder recorder,
.defaultBean()
.addType(Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(poolFunction)
.unremovable()
@@ -223,9 +227,9 @@ private void createPoolIfDefined(DB2PoolRecorder recorder,
.defaultBean()
.addType(io.vertx.mutiny.sqlclient.Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(VERTX_DB2_POOL_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(recorder.mutinyDB2Pool(poolFunction))
.createWith(recorder.mutinyDB2Pool(dataSourceName))
.unremovable()
.setRuntimeInit();

Original file line number Diff line number Diff line change
@@ -10,11 +10,13 @@
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions;
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.util.TypeLiteral;

@@ -47,7 +49,7 @@
public class DB2PoolRecorder {

private static final Logger log = Logger.getLogger(DB2PoolRecorder.class);
private static final TypeLiteral<Instance<DB2PoolCreator>> TYPE_LITERAL = new TypeLiteral<>() {
private static final TypeLiteral<Instance<DB2PoolCreator>> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() {
};

public Function<SyntheticCreationalContext<DB2Pool>, DB2Pool> configureDB2Pool(RuntimeValue<Vertx> vertx,
@@ -75,16 +77,30 @@ public DB2Pool apply(SyntheticCreationalContext<DB2Pool> context) {
}

public Function<SyntheticCreationalContext<io.vertx.mutiny.db2client.DB2Pool>, io.vertx.mutiny.db2client.DB2Pool> mutinyDB2Pool(
Function<SyntheticCreationalContext<DB2Pool>, DB2Pool> function) {
String dataSourceName) {
return new Function<>() {
@SuppressWarnings("unchecked")
@Override
public io.vertx.mutiny.db2client.DB2Pool apply(SyntheticCreationalContext context) {
return io.vertx.mutiny.db2client.DB2Pool.newInstance(function.apply(context));
DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class);
if (datasourceSupport.getInactiveNames().contains(dataSourceName)) {
throw DataSourceUtil.dataSourceInactive(dataSourceName);
}
DB2Pool db2Pool = (DB2Pool) context.getInjectedReference(DB2Pool.class,
getReactiveDataSourceQualifier(dataSourceName));
return io.vertx.mutiny.db2client.DB2Pool.newInstance(db2Pool);
}
};
}

private static Annotation getReactiveDataSourceQualifier(String dataSourceName) {
if (DataSourceUtil.isDefault(dataSourceName)) {
return Default.Literal.INSTANCE;
}

return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName);
}

private DB2Pool initialize(VertxInternal vertx,
Integer eventLoopCount,
String dataSourceName,
@@ -240,9 +256,9 @@ private DB2Pool createPool(Vertx vertx, PoolOptions poolOptions, DB2ConnectOptio
SyntheticCreationalContext<DB2Pool> context) {
Instance<DB2PoolCreator> instance;
if (DataSourceUtil.isDefault(dataSourceName)) {
instance = context.getInjectedReference(TYPE_LITERAL);
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL);
} else {
instance = context.getInjectedReference(TYPE_LITERAL,
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL,
new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName));
}
if (instance.isResolvable()) {
Original file line number Diff line number Diff line change
@@ -65,10 +65,13 @@

class ReactiveMSSQLClientProcessor {

private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class),
private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create(
DotName.createSimple(Instance.class),
new Type[] { ClassType.create(DotName.createSimple(MSSQLPoolCreator.class.getName())) }, null);
private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0];
private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class);
private static final DotName VERTX_MSSQL_POOL = DotName.createSimple(MSSQLPool.class);
private static final Type VERTX_MSSQL_POOL_TYPE = Type.create(VERTX_MSSQL_POOL, Type.Kind.CLASS);

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
@@ -207,7 +210,7 @@ private void createPoolIfDefined(MSSQLPoolRecorder recorder,
.defaultBean()
.addType(Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(poolFunction)
.unremovable()
@@ -222,9 +225,9 @@ private void createPoolIfDefined(MSSQLPoolRecorder recorder,
.defaultBean()
.addType(io.vertx.mutiny.sqlclient.Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(VERTX_MSSQL_POOL_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(recorder.mutinyMSSQLPool(poolFunction))
.createWith(recorder.mutinyMSSQLPool(dataSourceName))
.unremovable()
.setRuntimeInit();

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.reactive.mssql.client;

import jakarta.inject.Inject;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.ClientProxy;
import io.quarkus.test.QuarkusUnitTest;

public class SamePoolInstanceTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.overrideConfigKey("quarkus.devservices.enabled", "false");

@Inject
io.vertx.mutiny.mssqlclient.MSSQLPool mutinyPool;
@Inject
io.vertx.mssqlclient.MSSQLPool pool;

@Test
public void test() {
Assertions.assertThat(ClientProxy.unwrap(pool)).isSameAs(ClientProxy.unwrap(mutinyPool.getDelegate()));
}
}
Original file line number Diff line number Diff line change
@@ -10,11 +10,13 @@
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions;
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.util.TypeLiteral;

@@ -46,7 +48,7 @@
@Recorder
public class MSSQLPoolRecorder {

private static final TypeLiteral<Instance<MSSQLPoolCreator>> TYPE_LITERAL = new TypeLiteral<>() {
private static final TypeLiteral<Instance<MSSQLPoolCreator>> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() {
};

private static final Logger log = Logger.getLogger(MSSQLPoolRecorder.class);
@@ -80,16 +82,31 @@ public MSSQLPool apply(SyntheticCreationalContext<MSSQLPool> context) {
}

public Function<SyntheticCreationalContext<io.vertx.mutiny.mssqlclient.MSSQLPool>, io.vertx.mutiny.mssqlclient.MSSQLPool> mutinyMSSQLPool(
Function<SyntheticCreationalContext<MSSQLPool>, MSSQLPool> function) {
String dataSourceName) {
return new Function<SyntheticCreationalContext<io.vertx.mutiny.mssqlclient.MSSQLPool>, io.vertx.mutiny.mssqlclient.MSSQLPool>() {
@Override
@SuppressWarnings("unchecked")
public io.vertx.mutiny.mssqlclient.MSSQLPool apply(SyntheticCreationalContext context) {
return io.vertx.mutiny.mssqlclient.MSSQLPool.newInstance(function.apply(context));
DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class);
if (datasourceSupport.getInactiveNames().contains(dataSourceName)) {
throw DataSourceUtil.dataSourceInactive(dataSourceName);
}

return io.vertx.mutiny.mssqlclient.MSSQLPool.newInstance(
(MSSQLPool) context.getInjectedReference(MSSQLPool.class,
getReactiveDataSourceQualifier(dataSourceName)));
}
};
}

private static Annotation getReactiveDataSourceQualifier(String dataSourceName) {
if (DataSourceUtil.isDefault(dataSourceName)) {
return Default.Literal.INSTANCE;
}

return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName);
}

private MSSQLPool initialize(VertxInternal vertx,
Integer eventLoopCount,
String dataSourceName, DataSourceRuntimeConfig dataSourceRuntimeConfig,
@@ -243,9 +260,9 @@ private MSSQLPool createPool(Vertx vertx, PoolOptions poolOptions, MSSQLConnectO
SyntheticCreationalContext<MSSQLPool> context) {
Instance<MSSQLPoolCreator> instance;
if (DataSourceUtil.isDefault(dataSourceName)) {
instance = context.getInjectedReference(TYPE_LITERAL);
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL);
} else {
instance = context.getInjectedReference(TYPE_LITERAL,
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL,
new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName));
}
if (instance.isResolvable()) {
Original file line number Diff line number Diff line change
@@ -65,10 +65,13 @@

class ReactiveMySQLClientProcessor {

private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class),
private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create(
DotName.createSimple(Instance.class),
new Type[] { ClassType.create(DotName.createSimple(MySQLPoolCreator.class.getName())) }, null);
private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0];
private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class);
private static final DotName VERTX_MYSQL_POOL = DotName.createSimple(MySQLPool.class);
private static final Type VERTX_MYSQL_POOL_TYPE = Type.create(VERTX_MYSQL_POOL, Type.Kind.CLASS);

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
@@ -208,7 +211,7 @@ private void createPoolIfDefined(MySQLPoolRecorder recorder,
.defaultBean()
.addType(Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(poolFunction)
.unremovable()
@@ -223,9 +226,9 @@ private void createPoolIfDefined(MySQLPoolRecorder recorder,
.defaultBean()
.addType(io.vertx.mutiny.sqlclient.Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(VERTX_MYSQL_POOL_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(recorder.mutinyMySQLPool(poolFunction))
.createWith(recorder.mutinyMySQLPool(dataSourceName))
.unremovable()
.setRuntimeInit();

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.reactive.mysql.client;

import jakarta.inject.Inject;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.ClientProxy;
import io.quarkus.test.QuarkusUnitTest;

public class SamePoolInstanceTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest();

@Inject
io.vertx.mutiny.mysqlclient.MySQLPool mutinyPool;
@Inject
io.vertx.mysqlclient.MySQLPool pool;

@Test
public void test() {
Assertions.assertThat(ClientProxy.unwrap(pool)).isSameAs(ClientProxy.unwrap(mutinyPool.getDelegate()));
}
}
Original file line number Diff line number Diff line change
@@ -10,13 +10,15 @@
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions;
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.util.TypeLiteral;

@@ -47,7 +49,7 @@
@Recorder
public class MySQLPoolRecorder {

private static final TypeLiteral<Instance<MySQLPoolCreator>> TYPE_LITERAL = new TypeLiteral<>() {
private static final TypeLiteral<Instance<MySQLPoolCreator>> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() {
};

public Function<SyntheticCreationalContext<MySQLPool>, MySQLPool> configureMySQLPool(RuntimeValue<Vertx> vertx,
@@ -75,16 +77,30 @@ public MySQLPool apply(SyntheticCreationalContext<MySQLPool> context) {
}

public Function<SyntheticCreationalContext<io.vertx.mutiny.mysqlclient.MySQLPool>, io.vertx.mutiny.mysqlclient.MySQLPool> mutinyMySQLPool(
Function<SyntheticCreationalContext<MySQLPool>, MySQLPool> function) {
String dataSourceName) {
return new Function<>() {
@Override
@SuppressWarnings("unchecked")
public io.vertx.mutiny.mysqlclient.MySQLPool apply(SyntheticCreationalContext context) {
return io.vertx.mutiny.mysqlclient.MySQLPool.newInstance(function.apply(context));
DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class);
if (datasourceSupport.getInactiveNames().contains(dataSourceName)) {
throw DataSourceUtil.dataSourceInactive(dataSourceName);
}
return io.vertx.mutiny.mysqlclient.MySQLPool.newInstance(
(MySQLPool) context.getInjectedReference(MySQLPool.class,
getReactiveDataSourceQualifier(dataSourceName)));
}
};
}

private static Annotation getReactiveDataSourceQualifier(String dataSourceName) {
if (DataSourceUtil.isDefault(dataSourceName)) {
return Default.Literal.INSTANCE;
}

return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName);
}

private MySQLPool initialize(VertxInternal vertx,
Integer eventLoopCount,
String dataSourceName,
@@ -262,9 +278,9 @@ private MySQLPool createPool(Vertx vertx, PoolOptions poolOptions, List<MySQLCon
SyntheticCreationalContext<MySQLPool> context) {
Instance<MySQLPoolCreator> instance;
if (DataSourceUtil.isDefault(dataSourceName)) {
instance = context.getInjectedReference(TYPE_LITERAL);
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL);
} else {
instance = context.getInjectedReference(TYPE_LITERAL,
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL,
new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName));
}
if (instance.isResolvable()) {
Original file line number Diff line number Diff line change
@@ -65,10 +65,13 @@

class ReactiveOracleClientProcessor {

private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class),
private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create(
DotName.createSimple(Instance.class),
new Type[] { ClassType.create(DotName.createSimple(OraclePoolCreator.class.getName())) }, null);
private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0];
private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class);
private static final DotName VERTX_ORACLE_POOL = DotName.createSimple(OraclePool.class);
private static final Type VERTX_ORACLE_POOL_TYPE = Type.create(VERTX_ORACLE_POOL, Type.Kind.CLASS);

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
@@ -209,7 +212,7 @@ private void createPoolIfDefined(OraclePoolRecorder recorder,
.defaultBean()
.addType(Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(poolFunction)
.unremovable()
@@ -224,9 +227,9 @@ private void createPoolIfDefined(OraclePoolRecorder recorder,
.defaultBean()
.addType(io.vertx.mutiny.sqlclient.Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(VERTX_ORACLE_POOL_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(recorder.mutinyOraclePool(poolFunction))
.createWith(recorder.mutinyOraclePool(dataSourceName))
.unremovable()
.setRuntimeInit();

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.reactive.oracle.client;

import jakarta.inject.Inject;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.ClientProxy;
import io.quarkus.test.QuarkusUnitTest;

public class SamePoolInstanceTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest();

@Inject
io.vertx.mutiny.oracleclient.OraclePool mutinyPool;
@Inject
io.vertx.oracleclient.OraclePool pool;

@Test
public void test() {
Assertions.assertThat(ClientProxy.unwrap(pool)).isSameAs(ClientProxy.unwrap(mutinyPool.getDelegate()));
}
}
Original file line number Diff line number Diff line change
@@ -4,11 +4,13 @@
import static io.quarkus.credentials.CredentialsProvider.USER_PROPERTY_NAME;
import static io.quarkus.reactive.datasource.runtime.UnitisedTime.unitised;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.util.TypeLiteral;

@@ -41,7 +43,7 @@
@Recorder
public class OraclePoolRecorder {

private static final TypeLiteral<Instance<OraclePoolCreator>> TYPE_LITERAL = new TypeLiteral<>() {
private static final TypeLiteral<Instance<OraclePoolCreator>> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() {
};

private static final Logger log = Logger.getLogger(OraclePoolRecorder.class);
@@ -71,16 +73,30 @@ public OraclePool apply(SyntheticCreationalContext<OraclePool> context) {
}

public Function<SyntheticCreationalContext<io.vertx.mutiny.oracleclient.OraclePool>, io.vertx.mutiny.oracleclient.OraclePool> mutinyOraclePool(
Function<SyntheticCreationalContext<OraclePool>, OraclePool> function) {
String dataSourceName) {
return new Function<>() {
@SuppressWarnings("unchecked")
@Override
public io.vertx.mutiny.oracleclient.OraclePool apply(SyntheticCreationalContext context) {
return io.vertx.mutiny.oracleclient.OraclePool.newInstance(function.apply(context));
DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class);
if (datasourceSupport.getInactiveNames().contains(dataSourceName)) {
throw DataSourceUtil.dataSourceInactive(dataSourceName);
}
return io.vertx.mutiny.oracleclient.OraclePool.newInstance(
(OraclePool) context.getInjectedReference(OraclePool.class,
getReactiveDataSourceQualifier(dataSourceName)));
}
};
}

private static Annotation getReactiveDataSourceQualifier(String dataSourceName) {
if (DataSourceUtil.isDefault(dataSourceName)) {
return Default.Literal.INSTANCE;
}

return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName);
}

private OraclePool initialize(VertxInternal vertx,
Integer eventLoopCount,
String dataSourceName,
@@ -211,9 +227,9 @@ private OraclePool createPool(Vertx vertx, PoolOptions poolOptions, OracleConnec
SyntheticCreationalContext<OraclePool> context) {
Instance<OraclePoolCreator> instance;
if (DataSourceUtil.isDefault(dataSourceName)) {
instance = context.getInjectedReference(TYPE_LITERAL);
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL);
} else {
instance = context.getInjectedReference(TYPE_LITERAL,
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL,
new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName));
}
if (instance.isResolvable()) {
Original file line number Diff line number Diff line change
@@ -66,10 +66,13 @@

class ReactivePgClientProcessor {

private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class),
private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create(
DotName.createSimple(Instance.class),
new Type[] { ClassType.create(DotName.createSimple(PgPoolCreator.class.getName())) }, null);
private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0];
private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class);
private static final DotName VERTX_PG_POOL = DotName.createSimple(PgPool.class);
private static final Type VERTX_PG_POOL_TYPE = Type.create(VERTX_PG_POOL, Type.Kind.CLASS);

@BuildStep
NativeImageConfigBuildItem config() {
@@ -213,7 +216,7 @@ private void createPoolIfDefined(PgPoolRecorder recorder,
.defaultBean()
.addType(Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(poolFunction)
.unremovable()
@@ -223,14 +226,15 @@ private void createPoolIfDefined(PgPoolRecorder recorder,

syntheticBeans.produce(pgPoolBeanConfigurator.done());

// the Mutiny pool is created by using the Vertx pool
ExtendedBeanConfigurator mutinyPgPoolConfigurator = SyntheticBeanBuildItem
.configure(io.vertx.mutiny.pgclient.PgPool.class)
.defaultBean()
.addType(io.vertx.mutiny.sqlclient.Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(VERTX_PG_POOL_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(recorder.mutinyPgPool(poolFunction))
.createWith(recorder.mutinyPgPool(dataSourceName))
.unremovable()
.setRuntimeInit();

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.reactive.pg.client;

import static org.assertj.core.api.Assertions.*;

import jakarta.inject.Inject;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.ClientProxy;
import io.quarkus.test.QuarkusUnitTest;

public class SamePoolInstanceTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest();

@Inject
io.vertx.mutiny.pgclient.PgPool mutinyPool;
@Inject
io.vertx.pgclient.PgPool pool;

@Test
public void test() {
assertThat(ClientProxy.unwrap(pool)).isSameAs(ClientProxy.unwrap(mutinyPool.getDelegate()));
}
}
Original file line number Diff line number Diff line change
@@ -10,12 +10,14 @@
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions;
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.util.TypeLiteral;

@@ -46,7 +48,7 @@
@Recorder
public class PgPoolRecorder {

private static final TypeLiteral<Instance<PgPoolCreator>> TYPE_LITERAL = new TypeLiteral<>() {
private static final TypeLiteral<Instance<PgPoolCreator>> PG_POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() {
};

public Function<SyntheticCreationalContext<PgPool>, PgPool> configurePgPool(RuntimeValue<Vertx> vertx,
@@ -74,16 +76,30 @@ public PgPool apply(SyntheticCreationalContext<PgPool> context) {
}

public Function<SyntheticCreationalContext<io.vertx.mutiny.pgclient.PgPool>, io.vertx.mutiny.pgclient.PgPool> mutinyPgPool(
Function<SyntheticCreationalContext<PgPool>, PgPool> function) {
String dataSourceName) {
return new Function<>() {
@SuppressWarnings("unchecked")
@Override
public io.vertx.mutiny.pgclient.PgPool apply(SyntheticCreationalContext context) {
return io.vertx.mutiny.pgclient.PgPool.newInstance(function.apply(context));
DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class);
if (datasourceSupport.getInactiveNames().contains(dataSourceName)) {
throw DataSourceUtil.dataSourceInactive(dataSourceName);
}

return io.vertx.mutiny.pgclient.PgPool.newInstance(
(PgPool) context.getInjectedReference(PgPool.class, getReactiveDataSourceQualifier(dataSourceName)));
}
};
}

private static Annotation getReactiveDataSourceQualifier(String dataSourceName) {
if (DataSourceUtil.isDefault(dataSourceName)) {
return Default.Literal.INSTANCE;
}

return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName);
}

private PgPool initialize(VertxInternal vertx,
Integer eventLoopCount,
String dataSourceName,
@@ -251,9 +267,9 @@ private PgPool createPool(Vertx vertx, PoolOptions poolOptions, List<PgConnectOp
SyntheticCreationalContext<PgPool> context) {
Instance<PgPoolCreator> instance;
if (DataSourceUtil.isDefault(dataSourceName)) {
instance = context.getInjectedReference(TYPE_LITERAL);
instance = context.getInjectedReference(PG_POOL_CREATOR_TYPE_LITERAL);
} else {
instance = context.getInjectedReference(TYPE_LITERAL,
instance = context.getInjectedReference(PG_POOL_CREATOR_TYPE_LITERAL,
new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName));
}
if (instance.isResolvable()) {

0 comments on commit d894149

Please sign in to comment.