From 3145720f409360f9f3b94733d98c0183f45ea86e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Tue, 5 Dec 2023 22:15:22 +0100 Subject: [PATCH] add generic event manager --- gradle.properties | 2 +- src/main/java/module-info.java | 2 ++ .../java/org/xbib/event/EventManager.java | 30 ++++++++++++++++++- .../event/generic/GenericEventManager.java | 21 +++++++++++++ .../xbib/event/timer/DefaultTimerEvent.java | 1 - 5 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/xbib/event/generic/GenericEventManager.java diff --git a/gradle.properties b/gradle.properties index f4dfe41..109b9f9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = event -version = 0.0.6 +version = 0.0.7 org.gradle.warning.mode = ALL diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index e296def..1a2f5ff 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,6 +1,8 @@ module org.xbib.event { + uses org.xbib.event.EventConsumer; exports org.xbib.event.bus; exports org.xbib.event.clock; + exports org.xbib.event.generic; exports org.xbib.event.io; exports org.xbib.event.io.file; exports org.xbib.event.io.path; diff --git a/src/main/java/org/xbib/event/EventManager.java b/src/main/java/org/xbib/event/EventManager.java index bb2ae93..8352cf5 100644 --- a/src/main/java/org/xbib/event/EventManager.java +++ b/src/main/java/org/xbib/event/EventManager.java @@ -1,9 +1,11 @@ package org.xbib.event; +import java.util.ServiceLoader; import org.xbib.event.bus.AsyncEventBus; import org.xbib.event.bus.SubscriberExceptionContext; import org.xbib.event.bus.SubscriberExceptionHandler; import org.xbib.event.clock.ClockEventManager; +import org.xbib.event.generic.GenericEventManager; import org.xbib.event.io.file.FileFollowEventManager; import org.xbib.event.io.path.PathEventManager; import org.xbib.event.syslog.SyslogEventManager; @@ -27,6 +29,8 @@ public final class EventManager implements Closeable { private final Builder builder; + private final GenericEventManager genericEventManager; + private final ClockEventManager clockEventManager; private final TimerEventManager timerEventManager; @@ -39,6 +43,7 @@ public final class EventManager implements Closeable { private EventManager(Builder builder) { this.builder = builder; + this.genericEventManager = new GenericEventManager(builder.eventBus); this.clockEventManager = new ClockEventManager(builder.settings, builder.eventBus, builder.classLoader); this.timerEventManager = new TimerEventManager(builder.settings, builder.eventBus, builder.classLoader, ZoneId.systemDefault()); this.fileFollowEventManager = new FileFollowEventManager(builder.settings, builder.eventBus, builder.executorService, builder.classLoader); @@ -50,6 +55,10 @@ public final class EventManager implements Closeable { return new Builder(settings); } + public GenericEventManager getGenericEventManager() { + return genericEventManager; + } + public ClockEventManager getClockEventManager() { return clockEventManager; } @@ -80,6 +89,7 @@ public final class EventManager implements Closeable { for (EventConsumer eventConsumer : builder.eventConsumers) { eventConsumer.close(); } + genericEventManager.close(); clockEventManager.close(); timerEventManager.close(); fileFollowEventManager.close(); @@ -95,6 +105,8 @@ public final class EventManager implements Closeable { private ClassLoader classLoader; + private SubscriberExceptionHandler subscriberExceptionHandler; + private AsyncEventBus eventBus; private final List eventConsumers; @@ -102,6 +114,7 @@ public final class EventManager implements Closeable { private Builder(Settings settings) { this.settings = settings; this.eventConsumers = new ArrayList<>(); + this.subscriberExceptionHandler = new EventManagerExceptionHandler(); } public Builder setExecutorService(ExecutorService executorService) { @@ -125,6 +138,21 @@ public final class EventManager implements Closeable { return this; } + public Builder setSubscriberExceptionHandler(SubscriberExceptionHandler subscriberExceptionHandler) { + this.subscriberExceptionHandler = subscriberExceptionHandler; + return this; + } + + public Builder loadEventConsumers() { + if (classLoader == null) { + classLoader = EventManager.class.getClassLoader(); + } + for (EventConsumer eventConsumer : ServiceLoader.load(EventConsumer.class, classLoader)) { + eventConsumers.add(eventConsumer); + } + return this; + } + public EventManager build() { if (executorService == null) { executorService = Executors.newFixedThreadPool(settings.getAsInt("event.threadcount", 2)); @@ -133,7 +161,7 @@ public final class EventManager implements Closeable { classLoader = EventManager.class.getClassLoader(); } if (eventBus == null) { - eventBus = new AsyncEventBus(executorService, new EventManagerExceptionHandler()); + eventBus = new AsyncEventBus(executorService, subscriberExceptionHandler); } for (EventConsumer eventConsumer : eventConsumers) { eventBus.register(eventConsumer); diff --git a/src/main/java/org/xbib/event/generic/GenericEventManager.java b/src/main/java/org/xbib/event/generic/GenericEventManager.java new file mode 100644 index 0000000..b541de8 --- /dev/null +++ b/src/main/java/org/xbib/event/generic/GenericEventManager.java @@ -0,0 +1,21 @@ +package org.xbib.event.generic; + +import java.io.Closeable; +import org.xbib.event.bus.AsyncEventBus; + +public class GenericEventManager implements Closeable { + + private final AsyncEventBus eventBus; + + public GenericEventManager(AsyncEventBus eventBus) { + this.eventBus = eventBus; + } + + public void post(Object event) { + eventBus.post(event); + } + + @Override + public void close() { + } +} diff --git a/src/main/java/org/xbib/event/timer/DefaultTimerEvent.java b/src/main/java/org/xbib/event/timer/DefaultTimerEvent.java index f432d89..debb59e 100644 --- a/src/main/java/org/xbib/event/timer/DefaultTimerEvent.java +++ b/src/main/java/org/xbib/event/timer/DefaultTimerEvent.java @@ -1,7 +1,6 @@ package org.xbib.event.timer; import org.xbib.event.DefaultEvent; -import org.xbib.event.clock.ClockEvent; import java.time.Instant;