From ef9528c1ad0eaa2cf913409766388921e12c0550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Tue, 30 Jan 2024 17:44:12 +0100 Subject: [PATCH] working on event manager --- .../org/xbib/event/common/EventManager.java | 23 ++++++++++-------- .../generic/GenericEventManagerTest.java | 24 ++++++++++--------- .../event/net/http/test/HttpEventTest.java | 1 - 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/event-common/src/main/java/org/xbib/event/common/EventManager.java b/event-common/src/main/java/org/xbib/event/common/EventManager.java index cb86178..30e7a79 100644 --- a/event-common/src/main/java/org/xbib/event/common/EventManager.java +++ b/event-common/src/main/java/org/xbib/event/common/EventManager.java @@ -155,9 +155,12 @@ public final class EventManager { private final List eventConsumers; + private boolean loadEventConsumers; + private EventManagerBuilder() { this.eventConsumers = new ArrayList<>(); this.subscriberExceptionHandler = new EventManagerExceptionHandler(); + this.loadEventConsumers = true; } public EventManagerBuilder setSettings(Settings settings) { @@ -201,17 +204,20 @@ public final class EventManager { return this; } - public EventManagerBuilder loadEventConsumers() { - if (classLoader == null) { - classLoader = EventManager.class.getClassLoader(); - } - for (EventConsumer eventConsumer : ServiceLoader.load(EventConsumer.class, classLoader)) { - eventConsumers.add(eventConsumer); - } + public EventManagerBuilder loadEventConsumers(boolean enabled) { + this.loadEventConsumers = enabled; return this; } public EventManager build() { + if (classLoader == null) { + classLoader = EventManager.class.getClassLoader(); + } + if (loadEventConsumers) { + for (EventConsumer eventConsumer : ServiceLoader.load(EventConsumer.class, classLoader)) { + eventConsumers.add(eventConsumer); + } + } if (settings == null) { settings = Settings.emptySettings(); } @@ -222,9 +228,6 @@ public final class EventManager { executorService = Executors.newFixedThreadPool(threadcount); } } - if (classLoader == null) { - classLoader = EventManager.class.getClassLoader(); - } if (eventBus == null) { eventBus = new AsyncEventBus(executorService, subscriberExceptionHandler); } diff --git a/event-common/src/test/java/org/xbib/event/generic/GenericEventManagerTest.java b/event-common/src/test/java/org/xbib/event/generic/GenericEventManagerTest.java index 872138e..e9d5e9b 100644 --- a/event-common/src/test/java/org/xbib/event/generic/GenericEventManagerTest.java +++ b/event-common/src/test/java/org/xbib/event/generic/GenericEventManagerTest.java @@ -15,17 +15,16 @@ import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class GenericEventManagerTest { private static final Logger logger = Logger.getLogger(GenericEventManagerTest.class.getName()); @Test - void testGenericEvents() { - Settings settings = Settings.settingsBuilder() - .build(); + void testSimpleGenericEvent() { TestEventConsumer consumer = new TestEventConsumer(); EventManager eventManager = EventManager.builder() - .setSettings(settings) .register(consumer) .build(); Event event = EventManager.eventBuilder() @@ -33,16 +32,14 @@ public class GenericEventManagerTest { .setListener(e -> logger.log(Level.INFO, "received event " + e)) .build(); eventManager.getGenericEventManagerService().post(event); + assertEquals(1, consumer.getCount()); } @Test - void testGenericEventWithWaitForSubscriber() throws InterruptedException, ExecutionException { - Settings settings = Settings.settingsBuilder() - .build(); + void testGenericEventWithWaitForSubscriber() throws InterruptedException, ExecutionException, TimeoutException { TestEventConsumer consumer = new TestEventConsumer(); EventManager eventManager = EventManager.builder() - .setSettings(settings) .register(consumer) .build(); CompletableFuture future = new CompletableFuture<>(); @@ -54,8 +51,7 @@ public class GenericEventManagerTest { }) .build(); eventManager.getGenericEventManagerService().post(event); - Event e = future.get(); - logger.log(Level.INFO, "the event was received with result " + e + ", continuing"); + Event e = future.get(1000L, TimeUnit.MILLISECONDS); } @Test @@ -68,7 +64,6 @@ public class GenericEventManagerTest { .setSettings(settings) .register(consumer1) .register(consumer2) - .loadEventConsumers() .build(); CompletableFuture future = new CompletableFuture<>(); Event event = EventManager.eventBuilder() @@ -87,12 +82,19 @@ public class GenericEventManagerTest { private static class TestEventConsumer implements EventConsumer { + private int count; + TestEventConsumer() { } @Subscribe public void onEvent(GenericEventImpl event) { event.received(); + count++; + } + + public int getCount() { + return count; } } } diff --git a/event-net-http/src/test/java/org/xbib/event/net/http/test/HttpEventTest.java b/event-net-http/src/test/java/org/xbib/event/net/http/test/HttpEventTest.java index e210c79..648b553 100644 --- a/event-net-http/src/test/java/org/xbib/event/net/http/test/HttpEventTest.java +++ b/event-net-http/src/test/java/org/xbib/event/net/http/test/HttpEventTest.java @@ -48,7 +48,6 @@ public class HttpEventTest { nettyHttpServerConfig.setDebug(true); EventManager eventManager = EventManager.builder() - .loadEventConsumers() .build(); HttpEventManagerService httpEventManagerService = eventManager.getEventManagerService(HttpEventManagerService.class);