diff --git a/gradle.properties b/gradle.properties index 7cc3384..f4dfe41 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = event -version = 0.0.5 +version = 0.0.6 org.gradle.warning.mode = ALL diff --git a/src/main/java/org/xbib/event/EventManager.java b/src/main/java/org/xbib/event/EventManager.java index e298fa5..bb2ae93 100644 --- a/src/main/java/org/xbib/event/EventManager.java +++ b/src/main/java/org/xbib/event/EventManager.java @@ -25,17 +25,7 @@ public final class EventManager implements Closeable { private static final Logger logger = Logger.getLogger(EventManager.class.getName()); - private static ExecutorService executorService; - - private static ClassLoader classLoader; - - private static AsyncEventBus eventBus; - - private static List eventConsumers; - - static { - createInstances(); - } + private final Builder builder; private final ClockEventManager clockEventManager; @@ -47,26 +37,17 @@ public final class EventManager implements Closeable { private final SyslogEventManager syslogEventManager; - private EventManager(Settings settings) { - this.clockEventManager = new ClockEventManager(settings, eventBus, classLoader); - this.timerEventManager = new TimerEventManager(settings, eventBus, classLoader, ZoneId.systemDefault()); - this.fileFollowEventManager = new FileFollowEventManager(settings, eventBus, executorService, classLoader); - this.pathEventManager = new PathEventManager(settings, eventBus, executorService, classLoader); - this.syslogEventManager = new SyslogEventManager(settings, eventBus); + private EventManager(Builder builder) { + this.builder = builder; + 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); + this.pathEventManager = new PathEventManager(builder.settings, builder.eventBus, builder.executorService, builder.classLoader); + this.syslogEventManager = new SyslogEventManager(builder.settings, builder.eventBus); } - public static EventManager newEventManager(Settings settings) { - return new EventManager(settings); - } - - public static void register(EventConsumer eventConsumer) { - Objects.requireNonNull(eventConsumer, "event consumer must not be null"); - eventConsumers.add(eventConsumer); - eventBus.register(eventConsumer); - } - - public List getEventConsumers() { - return eventConsumers; + public static Builder builder(Settings settings) { + return new Builder(settings); } public ClockEventManager getClockEventManager() { @@ -96,7 +77,7 @@ public final class EventManager implements Closeable { @Override public void close() throws IOException { - for (EventConsumer eventConsumer : eventConsumers) { + for (EventConsumer eventConsumer : builder.eventConsumers) { eventConsumer.close(); } clockEventManager.close(); @@ -106,25 +87,65 @@ public final class EventManager implements Closeable { syslogEventManager.close(); } + public static class Builder { + + private final Settings settings; + + private ExecutorService executorService; + + private ClassLoader classLoader; + + private AsyncEventBus eventBus; + + private final List eventConsumers; + + private Builder(Settings settings) { + this.settings = settings; + this.eventConsumers = new ArrayList<>(); + } + + public Builder setExecutorService(ExecutorService executorService) { + this.executorService = executorService; + return this; + } + + public Builder setClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + return this; + } + + public Builder setEventBus(AsyncEventBus eventBus) { + this.eventBus = eventBus; + return this; + } + + public Builder register(EventConsumer eventConsumer) { + Objects.requireNonNull(eventConsumer, "event consumer must not be null"); + eventConsumers.add(eventConsumer); + return this; + } + + public EventManager build() { + if (executorService == null) { + executorService = Executors.newFixedThreadPool(settings.getAsInt("event.threadcount", 2)); + } + if (classLoader == null) { + classLoader = EventManager.class.getClassLoader(); + } + if (eventBus == null) { + eventBus = new AsyncEventBus(executorService, new EventManagerExceptionHandler()); + } + for (EventConsumer eventConsumer : eventConsumers) { + eventBus.register(eventConsumer); + } + return new EventManager(this); + } + } + private static class EventManagerExceptionHandler implements SubscriberExceptionHandler { @Override public void handleException(Throwable exception, SubscriberExceptionContext context) { logger.log(Level.SEVERE, exception.getMessage(), exception); } } - - private static void createInstances() { - if (executorService == null) { - executorService = Executors.newFixedThreadPool(2); - } - if (classLoader == null) { - classLoader = EventManager.class.getClassLoader(); - } - if (eventBus == null) { - eventBus = new AsyncEventBus(executorService, new EventManagerExceptionHandler()); - } - if (eventConsumers == null) { - eventConsumers = new ArrayList<>(); - } - } } diff --git a/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java b/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java index 6d80ad2..52d5e13 100644 --- a/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java +++ b/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java @@ -5,20 +5,20 @@ import org.xbib.event.EventManager; import org.xbib.settings.Settings; import java.io.IOException; -import java.util.logging.Logger; public class ClockEventManagerTest { @Test public void testEvents() throws IOException, InterruptedException { TestClockEventConsumer clockEventConsumer = new TestClockEventConsumer(); - EventManager.register(clockEventConsumer); Settings settings = Settings.settingsBuilder() .put("event.clock.testclockevent.enabled", "true") .put("event.clock.testclockevent.class", "org.xbib.event.clock.TestClockEvent") .put("event.clock.testclockevent.entry", "*/1 6-21 * * *") .build(); - EventManager eventManager = EventManager.newEventManager(settings); + EventManager eventManager = EventManager.builder(settings) + .register(clockEventConsumer) + .build(); ClockEventManager clockEventManager = eventManager.getClockEventManager(); Thread.sleep(90000L); clockEventManager.close(); diff --git a/src/test/java/org/xbib/event/io/file/FileFollowEventManagerTest.java b/src/test/java/org/xbib/event/io/file/FileFollowEventManagerTest.java index 13b86b2..2faedcc 100644 --- a/src/test/java/org/xbib/event/io/file/FileFollowEventManagerTest.java +++ b/src/test/java/org/xbib/event/io/file/FileFollowEventManagerTest.java @@ -2,16 +2,12 @@ package org.xbib.event.io.file; import org.junit.jupiter.api.Test; import org.xbib.event.EventManager; -import org.xbib.event.timer.TestTimerEventConsumer; -import org.xbib.event.timer.TimerEventManager; import org.xbib.settings.Settings; import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.time.Instant; -import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -23,14 +19,15 @@ public class FileFollowEventManagerTest { public void testFileFollowEvents() throws IOException, InterruptedException { Path path = Files.createTempDirectory("testfilefollow"); TestFileFollowEventConsumer consumer = new TestFileFollowEventConsumer(); - EventManager.register(consumer); Settings settings = Settings.settingsBuilder() .put("event.filefollow.testfilefollowevent.enabled", "true") .put("event.filefollow.testfilefollowevent.class", "org.xbib.event.io.file.TestFileFollowEvent") .put("event.filefollow.testfilefollowevent.base", path.toString()) .put("event.filefollow.testfilefollowevent.pattern", ".*") .build(); - EventManager eventManager = EventManager.newEventManager(settings); + EventManager eventManager = EventManager.builder(settings) + .register(consumer) + .build(); FileFollowEventManager fileFolloeEventManager = eventManager.getFileFollowEventManager(); Thread.sleep(5000L); try (BufferedWriter bufferedWriter = Files.newBufferedWriter(path.resolve("test.txt"))) { diff --git a/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java b/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java index e057b99..8a8b867 100644 --- a/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java +++ b/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java @@ -13,12 +13,13 @@ public class TimerEventManagerTest { @Test public void testTimerEvents() throws IOException, InterruptedException { TestTimerEventConsumer consumer = new TestTimerEventConsumer(); - EventManager.register(consumer); Settings settings = Settings.settingsBuilder() .put("event.timer.testtimerevent.enabled", "true") .put("event.timer.testtimerevent.class", "org.xbib.event.timer.TestTimerEvent") .build(); - EventManager eventManager = EventManager.newEventManager(settings); + EventManager eventManager = EventManager.builder(settings) + .register(consumer) + .build(); TimerEventManager timerEventManager = eventManager.getTimerEventManager(); timerEventManager.put("testtimerevent", Instant.now().plusSeconds(5L), Map.of("a", "b")); Thread.sleep(10000L);