Skip to content

Commit

Permalink
[Issue-2771] [Enhancement] Tries to inject Vertx instance from CDI us…
Browse files Browse the repository at this point in the history
…ing a qualifier
  • Loading branch information
gaol committed Sep 27, 2024
1 parent 4d869e4 commit 43ed734
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.event.Reception;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.literal.NamedLiteral;
import jakarta.inject.Inject;

import org.eclipse.microprofile.config.Config;

import io.vertx.mutiny.core.Vertx;

/**
Expand All @@ -19,6 +22,9 @@
@ApplicationScoped
public class ExecutionHolder {

private static final String REACTIVE_MESSAGING_VERTX_CDI_QUALIFIER = "mp.messaging.connector.vertx.cdi.qualifier";
private static final String REACTIVE_MESSAGING_VERTX_CDI_QUALIFIER_REQUIRED = "mp.messaging.connector.vertx.cdi.qualifier.required";

private boolean internalVertxInstance = false;
final Vertx vertx;

Expand All @@ -40,13 +46,26 @@ public ExecutionHolder(Vertx vertx) {
}

@Inject
public ExecutionHolder(Instance<Vertx> instanceOfVertx) {
if (instanceOfVertx == null || instanceOfVertx.isUnsatisfied()) {
public ExecutionHolder(Instance<Vertx> instanceOfVertx, Instance<Config> config) {
String cdiQualifier = null;
boolean qualifierRequired = false;
if (config != null && !config.isUnsatisfied()) {
final Config theConfig = config.get();
cdiQualifier = theConfig.getConfigValue(REACTIVE_MESSAGING_VERTX_CDI_QUALIFIER).getValue();
qualifierRequired = theConfig.getOptionalValue(REACTIVE_MESSAGING_VERTX_CDI_QUALIFIER_REQUIRED, Boolean.class)
.orElse(Boolean.FALSE);
}
Instance<Vertx> vertxInstance = instanceOfVertx;
if (cdiQualifier != null && !cdiQualifier.isEmpty()) {
log.vertxFromCDIQualifier(cdiQualifier);
vertxInstance = instanceOfVertx.select(NamedLiteral.of(cdiQualifier));
}
if (vertxInstance == null || vertxInstance.isUnsatisfied() || (qualifierRequired && cdiQualifier == null)) {
internalVertxInstance = true;
this.vertx = Vertx.vertx();
log.vertXInstanceCreated();
} else {
this.vertx = instanceOfVertx.get();
this.vertx = vertxInstance.get();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ public interface ProviderLogging extends BasicLogger {
@LogMessage(level = Logger.Level.WARN)
@Message(id = 243, value = "Processing method '%s' annotated with @Acknowledgement(POST_PROCESSING), but may not be compatible with post-processing acknowledgement management. You may experience duplicate (negative-)acknowledgement of messages.")
void postProcessingNotFullySupported(String methodAsString);

@LogMessage(level = Logger.Level.INFO)
@Message(id = 244, value = "Trying to get Vertx instance using named qualifier: %s.")
void vertxFromCDIQualifier(String cdiQualifier);
}

0 comments on commit 43ed734

Please sign in to comment.