Add Mutiny support to the reactive MySQL extension

* Expose the Mutiny MySQLPool
* Deprecate the RX and Axle API
* Use lazy instantiation
* Change the synchronization protocol for the PgPool

Use volatile instead of being guarded by a lock for the main pool.
This is because the "set" is called at start time, and then there is no concurrent writes.
The other pools are guarded because we don't do atomic actions on them (check and set).
This commit is contained in:
Clement Escoffier
2020-02-16 10:29:08 +01:00
parent 715fbf4192
commit 7710570b7a
3 changed files with 84 additions and 7 deletions

View File

@@ -20,6 +20,7 @@ public class MySQLPoolProducerTest {
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(BeanUsingBareMySQLClient.class)
.addClasses(BeanUsingMutinyMySQLClient.class)
.addClasses(BeanUsingAxleMySQLClient.class)
.addClasses(BeanUsingRXMySQLClient.class));
@@ -32,11 +33,15 @@ public class MySQLPoolProducerTest {
@Inject
BeanUsingRXMySQLClient beanUsingRx;
@Inject
BeanUsingMutinyMySQLClient beanUsingMutiny;
@Test
public void testVertxInjection() throws Exception {
public void testVertxInjection() {
beanUsingBare.verify()
.thenCompose(v -> beanUsingAxle.verify())
.thenCompose(v -> beanUsingRx.verify())
.thenCompose(v -> beanUsingMutiny.verify())
.toCompletableFuture()
.join();
}
@@ -59,6 +64,20 @@ public class MySQLPoolProducerTest {
@ApplicationScoped
static class BeanUsingAxleMySQLClient {
@Inject
io.vertx.mutiny.mysqlclient.MySQLPool mysqlClient;
public CompletionStage<Void> verify() {
return mysqlClient.query("SELECT 1")
.onItem().ignore().andContinueWithNull()
.onFailure().recoverWithItem((Void) null)
.subscribeAsCompletionStage();
}
}
@ApplicationScoped
static class BeanUsingMutinyMySQLClient {
@Inject
io.vertx.axle.mysqlclient.MySQLPool mysqlClient;

View File

@@ -44,6 +44,10 @@
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-axle-mysql-client</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-mysql-client</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -4,36 +4,90 @@ import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Singleton;
import org.jboss.logging.Logger;
import io.vertx.mysqlclient.MySQLPool;
@ApplicationScoped
public class MySQLPoolProducer {
private static final Logger LOGGER = Logger.getLogger(MySQLPoolProducer.class);
private volatile MySQLPool mysqlPool;
private volatile io.vertx.axle.mysqlclient.MySQLPool axleMySQLPool;
private volatile io.vertx.reactivex.mysqlclient.MySQLPool rxMySQLPool;
private io.vertx.mutiny.mysqlclient.MySQLPool mutinyMySQLPool;
/**
* @deprecated The Axle API is deprecated and will be removed in the future, use {@link #mutinyMySQLPool} instead.
*/
@Deprecated
private io.vertx.axle.mysqlclient.MySQLPool axleMySQLPool;
/**
* @deprecated The RX API is deprecated and will be removed in the future, use {@link #mutinyMySQLPool} instead.
*/
@Deprecated
private io.vertx.reactivex.mysqlclient.MySQLPool rxMySQLPool;
void initialize(MySQLPool mysqlPool) {
this.mysqlPool = mysqlPool;
this.axleMySQLPool = io.vertx.axle.mysqlclient.MySQLPool.newInstance(mysqlPool);
this.rxMySQLPool = io.vertx.reactivex.mysqlclient.MySQLPool.newInstance(mysqlPool);
}
/**
* @return the <em>bare</em> MySQL Pool instance.
*/
@Singleton
@Produces
public MySQLPool mysqlPool() {
return mysqlPool;
}
/**
* @return the <em>mutiny</em> MySQL Pool instance. The instance is created lazily.
*/
@Singleton
@Produces
public io.vertx.axle.mysqlclient.MySQLPool axleMySQLPool() {
public synchronized io.vertx.mutiny.mysqlclient.MySQLPool mutinyMySQLPool() {
if (mutinyMySQLPool == null) {
mutinyMySQLPool = io.vertx.mutiny.mysqlclient.MySQLPool.newInstance(mysqlPool);
}
return mutinyMySQLPool;
}
/**
* Produces the Axle MySQL Pool instance. The instance is created lazily.
*
* @return the Axle MySQL pool instance
* @deprecated The Axle API is deprecated and will be removed in the future, use {@link #mutinyMySQLPool()} instead.
*/
@Singleton
@Produces
@Deprecated
public synchronized io.vertx.axle.mysqlclient.MySQLPool axleMySQLPool() {
if (axleMySQLPool == null) {
LOGGER.warn(
"`io.vertx.axle.mysqlclient.MySQLPool` is deprecated and will be removed in a future version - it is "
+ "recommended to switch to `io.vertx.mutiny.mysqlclient.MySQLPool`");
axleMySQLPool = io.vertx.axle.mysqlclient.MySQLPool.newInstance(mysqlPool);
}
return axleMySQLPool;
}
/**
* Produces the RX MySQL Pool instance. The instance is created lazily.
*
* @return the RX MySQL pool instance
* @deprecated The RX API is deprecated and will be removed in the future, use {@link #mutinyMySQLPool()} instead.
*/
@Singleton
@Produces
public io.vertx.reactivex.mysqlclient.MySQLPool rxMySQLPool() {
@Deprecated
public synchronized io.vertx.reactivex.mysqlclient.MySQLPool rxMySQLPool() {
if (rxMySQLPool == null) {
LOGGER.warn(
"`io.vertx.reactivex.mysqlclient.MySQLPool` is deprecated and will be removed in a future version - it is "
+ "recommended to switch to `io.vertx.mutiny.mysqlclient.MySQLPool`");
rxMySQLPool = io.vertx.reactivex.mysqlclient.MySQLPool.newInstance(mysqlPool);
}
return rxMySQLPool;
}
}