From 6a67fe5db2394adace6a38d62c38170dd8a03409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Wed, 4 Oct 2023 17:15:38 +0200 Subject: [PATCH] add event manager --- .../java/org/xbib/event/EventManager.java | 52 +++++++++++++++++++ .../xbib/event/clock/ClockEventManager.java | 23 -------- 2 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 src/main/java/org/xbib/event/EventManager.java diff --git a/src/main/java/org/xbib/event/EventManager.java b/src/main/java/org/xbib/event/EventManager.java new file mode 100644 index 0000000..c2592c6 --- /dev/null +++ b/src/main/java/org/xbib/event/EventManager.java @@ -0,0 +1,52 @@ +package org.xbib.event; + +import org.xbib.event.bus.AsyncEventBus; +import org.xbib.event.bus.EventBus; +import org.xbib.event.clock.ClockEventManager; +import org.xbib.settings.Settings; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class EventManager { + + private static final Logger logger = Logger.getLogger(EventManager.class.getName()); + + public EventManager(Settings settings) { + this(settings, new AsyncEventBus(Executors.newSingleThreadExecutor()), ClockEventManager.class.getClassLoader()); + } + + public EventManager(Settings settings, + EventBus eventBus, + ClassLoader classLoader) { + // register consumers + List consumerList = new ArrayList<>(); + for (Map.Entry consumers : settings.getGroups("event.consumer").entrySet()) { + Settings entrySettings = consumers.getValue(); + if (entrySettings.getAsBoolean("enabled", true)) { + String className = entrySettings.get("class"); + try { + if (className != null) { + @SuppressWarnings("unchecked") + Class consumerClass = (Class) classLoader.loadClass(className); + eventBus.register(consumerClass.getDeclaredConstructor().newInstance()); + logger.log(Level.INFO, "consumer " + consumerClass + " registered"); + consumerList.add(consumerClass.getName()); + } + } catch (Exception e) { + logger.log(Level.WARNING, "unable to load consumer " + className + ", reason " + e.getMessage()); + } + } + } + logger.log(Level.INFO, "consumers = " + consumerList); + + } + + public void start() { + + } +} diff --git a/src/main/java/org/xbib/event/clock/ClockEventManager.java b/src/main/java/org/xbib/event/clock/ClockEventManager.java index 82994f6..890ca72 100644 --- a/src/main/java/org/xbib/event/clock/ClockEventManager.java +++ b/src/main/java/org/xbib/event/clock/ClockEventManager.java @@ -1,6 +1,5 @@ package org.xbib.event.clock; -import org.xbib.event.EventConsumer; import org.xbib.event.bus.AsyncEventBus; import org.xbib.event.bus.EventBus; import org.xbib.settings.Settings; @@ -9,8 +8,6 @@ import org.xbib.time.schedule.CronSchedule; import java.io.Closeable; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -41,26 +38,6 @@ public class ClockEventManager implements Closeable { ScheduledExecutorService executorService = Executors.newScheduledThreadPool(settings.getAsInt("pool.size", 2), threadFactory); this.cronSchedule = new CronSchedule<>(executorService); - // register consumers - List consumerList = new ArrayList<>(); - for (Map.Entry consumers : settings.getGroups("event.consumer").entrySet()) { - Settings entrySettings = consumers.getValue(); - if (entrySettings.getAsBoolean("enabled", true)) { - String className = entrySettings.get("class"); - try { - if (className != null) { - @SuppressWarnings("unchecked") - Class consumerClass = (Class) classLoader.loadClass(className); - eventBus.register(consumerClass.getDeclaredConstructor().newInstance()); - logger.log(Level.INFO, "consumer " + consumerClass + " registered"); - consumerList.add(consumerClass.getName()); - } - } catch (Exception e) { - logger.log(Level.WARNING, "unable to load consumer " + className + ", reason " + e.getMessage()); - } - } - } - logger.log(Level.INFO, "consumers = " + consumerList); for (Map.Entry cronjobs : settings.getGroups("event.clock").entrySet()) { Settings entrySettings = cronjobs.getValue(); if (entrySettings.getAsBoolean("enabled", true)) {