Skip to content

Commit

Permalink
Merge pull request #9485 from mkouba/issue-9456
Browse files Browse the repository at this point in the history
VertxProducer - simplify code, get rid of synchronization
  • Loading branch information
mkouba authored May 21, 2020
2 parents dc0d6a5 + b841d47 commit 7f6ef81
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package io.quarkus.vertx.verticles;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.EventBus;

@ApplicationScoped
public class MyUndeployedVerticle extends AbstractVerticle {

// Injecting @Singleton bean
@Inject
EventBus eventBus;

@Override
public Uni<Void> asyncStart() {
return vertx.eventBus().consumer("bravo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
* The original Vert.x instance is coming from the core artifact.
*
* IMPORTANT: The Axle and RxJava 2 API are now deprecated. It is recommended to switch to the Mutiny API.
*
* IMPL NOTE: There is no need to cache the mutiny/axle/rx wrappers locally because the bean instances are stored in the
* singleton context, i.e. the producer method is only called once.
*/
@ApplicationScoped
public class VertxProducer {
Expand All @@ -34,14 +37,6 @@ public class VertxProducer {
@Inject
Vertx vertx;

@Deprecated
private io.vertx.axle.core.Vertx axleVertx;

@Deprecated
private io.vertx.reactivex.core.Vertx rxVertx;

private io.vertx.mutiny.core.Vertx mutinyVertx;

@Singleton
@Produces
public EventBus eventbus() {
Expand All @@ -50,63 +45,54 @@ public EventBus eventbus() {

@Singleton
@Produces
public synchronized io.vertx.mutiny.core.Vertx mutiny() {
if (mutinyVertx == null) {
mutinyVertx = io.vertx.mutiny.core.Vertx.newInstance(vertx);
}
return mutinyVertx;
public io.vertx.mutiny.core.Vertx mutiny() {
return io.vertx.mutiny.core.Vertx.newInstance(vertx);
}

@Singleton
@Produces
@Deprecated
public synchronized io.vertx.axle.core.Vertx axle() {
if (axleVertx == null) {
LOGGER.warn(
"`io.vertx.axle.core.Vertx` is deprecated and will be removed in a future version - it is "
+ "recommended to switch to `io.vertx.mutiny.core.Vertx`");
axleVertx = io.vertx.axle.core.Vertx.newInstance(vertx);
}
return axleVertx;
public io.vertx.axle.core.Vertx axle() {
LOGGER.warn(
"`io.vertx.axle.core.Vertx` is deprecated and will be removed in a future version - it is "
+ "recommended to switch to `io.vertx.mutiny.core.Vertx`");
return io.vertx.axle.core.Vertx.newInstance(vertx);
}

@Singleton
@Produces
@Deprecated
public io.vertx.reactivex.core.Vertx rx() {
if (rxVertx == null) {
LOGGER.warn(
"`io.vertx.reactivex.core.Vertx` is deprecated and will be removed in a future version - it is "
+ "recommended to switch to `io.vertx.mutiny.core.Vertx`");
rxVertx = io.vertx.reactivex.core.Vertx.newInstance(vertx);
}
return rxVertx;
LOGGER.warn(
"`io.vertx.reactivex.core.Vertx` is deprecated and will be removed in a future version - it is "
+ "recommended to switch to `io.vertx.mutiny.core.Vertx`");
return io.vertx.reactivex.core.Vertx.newInstance(vertx);
}

@Singleton
@Produces
@Deprecated
public io.vertx.axle.core.eventbus.EventBus axleEventBus() {
public io.vertx.axle.core.eventbus.EventBus axleEventBus(io.vertx.axle.core.Vertx axle) {
LOGGER.warn(
"`io.vertx.axle.core.eventbus.EventBus` is deprecated and will be removed in a future version - it is "
+ "recommended to switch to `io.vertx.mutiny.core.eventbus.EventBus`");
return axle().eventBus();
return axle.eventBus();
}

@Singleton
@Produces
@Deprecated
public synchronized io.vertx.reactivex.core.eventbus.EventBus rxEventBus() {
public io.vertx.reactivex.core.eventbus.EventBus rxEventBus(io.vertx.reactivex.core.Vertx rx) {
LOGGER.warn(
"`io.vertx.reactivex.core.eventbus.EventBus` is deprecated and will be removed in a future version - it "
+ "is recommended to switch to `io.vertx.mutiny.core.eventbus.EventBus`");
return rx().eventBus();
return rx.eventBus();
}

@Singleton
@Produces
public synchronized io.vertx.mutiny.core.eventbus.EventBus mutinyEventBus() {
return mutiny().eventBus();
public io.vertx.mutiny.core.eventbus.EventBus mutinyEventBus(io.vertx.mutiny.core.Vertx mutiny) {
return mutiny.eventBus();
}

/**
Expand All @@ -117,7 +103,7 @@ public synchronized io.vertx.mutiny.core.eventbus.EventBus mutinyEventBus() {
* @param beanManager
*/
void undeployVerticles(@Observes @BeforeDestroyed(ApplicationScoped.class) Object event,
BeanManager beanManager) {
BeanManager beanManager, io.vertx.mutiny.core.Vertx mutiny) {
// Only beans with the AbstractVerticle in the set of bean types are considered - we need a deployment id
Set<Bean<?>> beans = beanManager.getBeans(AbstractVerticle.class, Any.Literal.INSTANCE);
Context applicationContext = beanManager.getContext(ApplicationScoped.class);
Expand All @@ -129,8 +115,8 @@ void undeployVerticles(@Observes @BeforeDestroyed(ApplicationScoped.class) Objec
// Only existing instances are considered
try {
AbstractVerticle verticle = (AbstractVerticle) instance;
mutinyVertx.undeploy(verticle.deploymentID()).await().indefinitely();
LOGGER.debugf("Undeployed verticle %s:", instance.getClass());
mutiny.undeploy(verticle.deploymentID()).await().indefinitely();
LOGGER.debugf("Undeployed verticle: %s", instance.getClass());
} catch (Exception e) {
// In theory, a user can undeploy the verticle manually
LOGGER.debugf("Unable to undeploy verticle %s: %s", instance.getClass(), e.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ private void verifyProducer() {

assertThat(producer.axle()).isNotNull();
assertFalse(producer.axle().isClustered());
assertThat(producer.axleEventBus()).isNotNull();
assertThat(producer.axleEventBus(producer.axle())).isNotNull();

assertThat(producer.rx()).isNotNull();
assertFalse(producer.rx().isClustered());
assertThat(producer.rxEventBus()).isNotNull();
assertThat(producer.rxEventBus(producer.rx())).isNotNull();

assertThat(producer.mutiny()).isNotNull();
assertFalse(producer.mutiny().isClustered());
assertThat(producer.mutinyEventBus()).isNotNull();
assertThat(producer.mutinyEventBus(producer.mutiny())).isNotNull();

}
}

0 comments on commit 7f6ef81

Please sign in to comment.