io.quarkus.vertx.runtime.VertxEventBusConsumerRecorder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-vertx Show documentation
Show all versions of quarkus-vertx Show documentation
Write reactive applications with the Vert.x API
package io.quarkus.vertx.runtime;
import static io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle.setContextSafe;
import static io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle.setCurrentContextSafe;
import static io.smallrye.common.expression.Expression.Flag.LENIENT_SYNTAX;
import static io.smallrye.common.expression.Expression.Flag.NO_TRIM;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.jboss.logging.Logger;
import io.quarkus.arc.CurrentContextFactory;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.ShutdownContext;
import io.quarkus.runtime.annotations.Recorder;
import io.quarkus.runtime.configuration.ProfileManager;
import io.quarkus.vertx.ConsumeEvent;
import io.quarkus.vertx.LocalEventBusCodec;
import io.quarkus.virtual.threads.VirtualThreadsRecorder;
import io.smallrye.common.expression.Expression;
import io.smallrye.common.expression.ResolveContext;
import io.smallrye.common.vertx.VertxContext;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.Message;
import io.vertx.core.eventbus.MessageCodec;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.VertxInternal;
@Recorder
public class VertxEventBusConsumerRecorder {
private static final Logger LOGGER = Logger.getLogger(VertxEventBusConsumerRecorder.class.getName());
static volatile Vertx vertx;
static volatile List> messageConsumers;
public void configureVertx(Supplier vertx, Map messageConsumerConfigurations,
LaunchMode launchMode, ShutdownContext shutdown, Map, Class>> codecByClass,
List> selectorTypes) {
VertxEventBusConsumerRecorder.vertx = vertx.get();
VertxEventBusConsumerRecorder.messageConsumers = new CopyOnWriteArrayList<>();
registerMessageConsumers(messageConsumerConfigurations);
registerCodecs(codecByClass, selectorTypes);
if (launchMode == LaunchMode.DEVELOPMENT) {
shutdown.addShutdownTask(new Runnable() {
@Override
public void run() {
unregisterMessageConsumers();
}
});
} else {
shutdown.addShutdownTask(new Runnable() {
@Override
public void run() {
destroy();
}
});
}
}
public RuntimeValue currentContextFactory() {
return new RuntimeValue<>(new VertxCurrentContextFactory());
}
public static Vertx getVertx() {
return vertx;
}
void destroy() {
messageConsumers = null;
vertx = null;
}
void registerMessageConsumers(Map messageConsumerConfigurations) {
if (!messageConsumerConfigurations.isEmpty()) {
EventBus eventBus = vertx.eventBus();
VertxInternal vi = (VertxInternal) VertxEventBusConsumerRecorder.vertx;
CountDownLatch latch = new CountDownLatch(messageConsumerConfigurations.size());
final List registrationFailures = new ArrayList<>();
for (Entry entry : messageConsumerConfigurations.entrySet()) {
EventConsumerInvoker invoker = createInvoker(entry.getKey());
String address = lookUpPropertyValue(entry.getValue().value());
// Create a context attached to each consumer
// If we don't all consumers will use the same event loop and so published messages (dispatched to all
// consumers) delivery is serialized.
ContextInternal context = vi.createEventLoopContext();
context.runOnContext(new Handler() {
@Override
public void handle(Void x) {
MessageConsumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy