From 7721ed7a71578bc0299385801d96861d7f9ced93 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 18 Jul 2023 18:20:00 +0100 Subject: [PATCH 1/2] Eagerly start and initialize EventsService before EventSourcedRegistryStorage --- .../events/EventSourcedRegistryStorage.java | 17 ++++++++++++++--- .../apicurio/registry/events/EventsService.java | 2 ++ .../registry/events/EventsServiceImpl.java | 9 +++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/apicurio/registry/events/EventSourcedRegistryStorage.java b/app/src/main/java/io/apicurio/registry/events/EventSourcedRegistryStorage.java index bc6349a6e6..ab4a2513c7 100644 --- a/app/src/main/java/io/apicurio/registry/events/EventSourcedRegistryStorage.java +++ b/app/src/main/java/io/apicurio/registry/events/EventSourcedRegistryStorage.java @@ -44,6 +44,7 @@ import io.apicurio.registry.storage.dto.StoredArtifactDto; import io.apicurio.registry.types.ArtifactState; import io.apicurio.registry.types.RuleType; +import org.slf4j.Logger; import java.util.HashMap; import java.util.List; @@ -51,7 +52,6 @@ import java.util.Optional; import java.util.Set; import javax.enterprise.context.ApplicationScoped; -import javax.inject.Inject; /** * @author Fabian Martinez @@ -59,8 +59,15 @@ @ApplicationScoped public class EventSourcedRegistryStorage extends RegistryStorageDecorator { - @Inject - EventsService eventsService; + final Logger log; + + final EventsService eventsService; + + // Need to have an eager evaluation of the EventsService implementation + EventSourcedRegistryStorage(EventsService eventService, Logger log) { + this.log = log; + this.eventsService = eventService; + } private void fireEvent(RegistryEventType type, String artifactId, Object data, Throwable error) { if (error == null && data != null) { @@ -73,6 +80,10 @@ private void fireEvent(RegistryEventType type, String artifactId, Object data, T */ @Override public boolean isEnabled() { + if (!eventsService.isReady()) { + throw new RuntimeException("Events Service not configured, please report this as a bug."); + } + log.info("Events service is configured: " + eventsService.isConfigured()); return eventsService.isConfigured(); } diff --git a/app/src/main/java/io/apicurio/registry/events/EventsService.java b/app/src/main/java/io/apicurio/registry/events/EventsService.java index ecc4d04446..5ddde10b87 100644 --- a/app/src/main/java/io/apicurio/registry/events/EventsService.java +++ b/app/src/main/java/io/apicurio/registry/events/EventsService.java @@ -24,6 +24,8 @@ */ public interface EventsService { + boolean isReady(); + boolean isConfigured(); void triggerEvent(RegistryEventType type, Optional artifactId, Object data); diff --git a/app/src/main/java/io/apicurio/registry/events/EventsServiceImpl.java b/app/src/main/java/io/apicurio/registry/events/EventsServiceImpl.java index 1811f1b1e0..f0936b4766 100644 --- a/app/src/main/java/io/apicurio/registry/events/EventsServiceImpl.java +++ b/app/src/main/java/io/apicurio/registry/events/EventsServiceImpl.java @@ -25,6 +25,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import io.quarkus.runtime.Startup; import org.slf4j.Logger; import io.apicurio.registry.events.dto.RegistryEventType; import io.quarkus.runtime.StartupEvent; @@ -36,12 +37,14 @@ /** * @author Fabian Martinez */ +@Startup @ApplicationScoped public class EventsServiceImpl implements EventsService { private static final String INTERNAL_EVENTS_ADDRESS = "registry-events"; private ObjectMapper mapper; + private boolean initDone = false; private boolean configuredSinks = false; @Inject @@ -64,6 +67,12 @@ public void init(@Observes StartupEvent ev) { configuredSinks = true; } } + initDone = true; + } + + @Override + public boolean isReady() { + return initDone; } @Override From c0f0a726f2a6c696a869c229bceb6231c311cacc Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Wed, 19 Jul 2023 12:05:04 +0100 Subject: [PATCH 2/2] review --- .../io/apicurio/registry/events/EventsServiceImpl.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/apicurio/registry/events/EventsServiceImpl.java b/app/src/main/java/io/apicurio/registry/events/EventsServiceImpl.java index f0936b4766..f1fb26f8fa 100644 --- a/app/src/main/java/io/apicurio/registry/events/EventsServiceImpl.java +++ b/app/src/main/java/io/apicurio/registry/events/EventsServiceImpl.java @@ -17,18 +17,16 @@ import java.util.Optional; +import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.event.Observes; import javax.enterprise.inject.Instance; import javax.inject.Inject; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import io.quarkus.runtime.Startup; import org.slf4j.Logger; import io.apicurio.registry.events.dto.RegistryEventType; -import io.quarkus.runtime.StartupEvent; import io.vertx.core.Vertx; import io.vertx.core.buffer.Buffer; import io.vertx.core.eventbus.DeliveryOptions; @@ -37,7 +35,6 @@ /** * @author Fabian Martinez */ -@Startup @ApplicationScoped public class EventsServiceImpl implements EventsService { @@ -59,7 +56,8 @@ public class EventsServiceImpl implements EventsService { @Inject Instance sinks; - public void init(@Observes StartupEvent ev) { + @PostConstruct + public void init() { for (EventSink sink : sinks) { if (sink.isConfigured()) { log.info("Subscribing sink " + sink.name());