use builder pattern for EventManager
This commit is contained in:
parent
1d03b1be86
commit
6fa7b63753
5 changed files with 76 additions and 57 deletions
|
@ -1,5 +1,5 @@
|
||||||
group = org.xbib
|
group = org.xbib
|
||||||
name = event
|
name = event
|
||||||
version = 0.0.5
|
version = 0.0.6
|
||||||
|
|
||||||
org.gradle.warning.mode = ALL
|
org.gradle.warning.mode = ALL
|
||||||
|
|
|
@ -25,17 +25,7 @@ public final class EventManager implements Closeable {
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(EventManager.class.getName());
|
private static final Logger logger = Logger.getLogger(EventManager.class.getName());
|
||||||
|
|
||||||
private static ExecutorService executorService;
|
private final Builder builder;
|
||||||
|
|
||||||
private static ClassLoader classLoader;
|
|
||||||
|
|
||||||
private static AsyncEventBus eventBus;
|
|
||||||
|
|
||||||
private static List<EventConsumer> eventConsumers;
|
|
||||||
|
|
||||||
static {
|
|
||||||
createInstances();
|
|
||||||
}
|
|
||||||
|
|
||||||
private final ClockEventManager clockEventManager;
|
private final ClockEventManager clockEventManager;
|
||||||
|
|
||||||
|
@ -47,26 +37,17 @@ public final class EventManager implements Closeable {
|
||||||
|
|
||||||
private final SyslogEventManager syslogEventManager;
|
private final SyslogEventManager syslogEventManager;
|
||||||
|
|
||||||
private EventManager(Settings settings) {
|
private EventManager(Builder builder) {
|
||||||
this.clockEventManager = new ClockEventManager(settings, eventBus, classLoader);
|
this.builder = builder;
|
||||||
this.timerEventManager = new TimerEventManager(settings, eventBus, classLoader, ZoneId.systemDefault());
|
this.clockEventManager = new ClockEventManager(builder.settings, builder.eventBus, builder.classLoader);
|
||||||
this.fileFollowEventManager = new FileFollowEventManager(settings, eventBus, executorService, classLoader);
|
this.timerEventManager = new TimerEventManager(builder.settings, builder.eventBus, builder.classLoader, ZoneId.systemDefault());
|
||||||
this.pathEventManager = new PathEventManager(settings, eventBus, executorService, classLoader);
|
this.fileFollowEventManager = new FileFollowEventManager(builder.settings, builder.eventBus, builder.executorService, builder.classLoader);
|
||||||
this.syslogEventManager = new SyslogEventManager(settings, eventBus);
|
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) {
|
public static Builder builder(Settings settings) {
|
||||||
return new EventManager(settings);
|
return new Builder(settings);
|
||||||
}
|
|
||||||
|
|
||||||
public static void register(EventConsumer eventConsumer) {
|
|
||||||
Objects.requireNonNull(eventConsumer, "event consumer must not be null");
|
|
||||||
eventConsumers.add(eventConsumer);
|
|
||||||
eventBus.register(eventConsumer);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<EventConsumer> getEventConsumers() {
|
|
||||||
return eventConsumers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ClockEventManager getClockEventManager() {
|
public ClockEventManager getClockEventManager() {
|
||||||
|
@ -96,7 +77,7 @@ public final class EventManager implements Closeable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
for (EventConsumer eventConsumer : eventConsumers) {
|
for (EventConsumer eventConsumer : builder.eventConsumers) {
|
||||||
eventConsumer.close();
|
eventConsumer.close();
|
||||||
}
|
}
|
||||||
clockEventManager.close();
|
clockEventManager.close();
|
||||||
|
@ -106,25 +87,65 @@ public final class EventManager implements Closeable {
|
||||||
syslogEventManager.close();
|
syslogEventManager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
|
||||||
|
private final Settings settings;
|
||||||
|
|
||||||
|
private ExecutorService executorService;
|
||||||
|
|
||||||
|
private ClassLoader classLoader;
|
||||||
|
|
||||||
|
private AsyncEventBus eventBus;
|
||||||
|
|
||||||
|
private final List<EventConsumer> 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 {
|
private static class EventManagerExceptionHandler implements SubscriberExceptionHandler {
|
||||||
@Override
|
@Override
|
||||||
public void handleException(Throwable exception, SubscriberExceptionContext context) {
|
public void handleException(Throwable exception, SubscriberExceptionContext context) {
|
||||||
logger.log(Level.SEVERE, exception.getMessage(), exception);
|
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<>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,20 +5,20 @@ import org.xbib.event.EventManager;
|
||||||
import org.xbib.settings.Settings;
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
public class ClockEventManagerTest {
|
public class ClockEventManagerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEvents() throws IOException, InterruptedException {
|
public void testEvents() throws IOException, InterruptedException {
|
||||||
TestClockEventConsumer clockEventConsumer = new TestClockEventConsumer();
|
TestClockEventConsumer clockEventConsumer = new TestClockEventConsumer();
|
||||||
EventManager.register(clockEventConsumer);
|
|
||||||
Settings settings = Settings.settingsBuilder()
|
Settings settings = Settings.settingsBuilder()
|
||||||
.put("event.clock.testclockevent.enabled", "true")
|
.put("event.clock.testclockevent.enabled", "true")
|
||||||
.put("event.clock.testclockevent.class", "org.xbib.event.clock.TestClockEvent")
|
.put("event.clock.testclockevent.class", "org.xbib.event.clock.TestClockEvent")
|
||||||
.put("event.clock.testclockevent.entry", "*/1 6-21 * * *")
|
.put("event.clock.testclockevent.entry", "*/1 6-21 * * *")
|
||||||
.build();
|
.build();
|
||||||
EventManager eventManager = EventManager.newEventManager(settings);
|
EventManager eventManager = EventManager.builder(settings)
|
||||||
|
.register(clockEventConsumer)
|
||||||
|
.build();
|
||||||
ClockEventManager clockEventManager = eventManager.getClockEventManager();
|
ClockEventManager clockEventManager = eventManager.getClockEventManager();
|
||||||
Thread.sleep(90000L);
|
Thread.sleep(90000L);
|
||||||
clockEventManager.close();
|
clockEventManager.close();
|
||||||
|
|
|
@ -2,16 +2,12 @@ package org.xbib.event.io.file;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.xbib.event.EventManager;
|
import org.xbib.event.EventManager;
|
||||||
import org.xbib.event.timer.TestTimerEventConsumer;
|
|
||||||
import org.xbib.event.timer.TimerEventManager;
|
|
||||||
import org.xbib.settings.Settings;
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@ -23,14 +19,15 @@ public class FileFollowEventManagerTest {
|
||||||
public void testFileFollowEvents() throws IOException, InterruptedException {
|
public void testFileFollowEvents() throws IOException, InterruptedException {
|
||||||
Path path = Files.createTempDirectory("testfilefollow");
|
Path path = Files.createTempDirectory("testfilefollow");
|
||||||
TestFileFollowEventConsumer consumer = new TestFileFollowEventConsumer();
|
TestFileFollowEventConsumer consumer = new TestFileFollowEventConsumer();
|
||||||
EventManager.register(consumer);
|
|
||||||
Settings settings = Settings.settingsBuilder()
|
Settings settings = Settings.settingsBuilder()
|
||||||
.put("event.filefollow.testfilefollowevent.enabled", "true")
|
.put("event.filefollow.testfilefollowevent.enabled", "true")
|
||||||
.put("event.filefollow.testfilefollowevent.class", "org.xbib.event.io.file.TestFileFollowEvent")
|
.put("event.filefollow.testfilefollowevent.class", "org.xbib.event.io.file.TestFileFollowEvent")
|
||||||
.put("event.filefollow.testfilefollowevent.base", path.toString())
|
.put("event.filefollow.testfilefollowevent.base", path.toString())
|
||||||
.put("event.filefollow.testfilefollowevent.pattern", ".*")
|
.put("event.filefollow.testfilefollowevent.pattern", ".*")
|
||||||
.build();
|
.build();
|
||||||
EventManager eventManager = EventManager.newEventManager(settings);
|
EventManager eventManager = EventManager.builder(settings)
|
||||||
|
.register(consumer)
|
||||||
|
.build();
|
||||||
FileFollowEventManager fileFolloeEventManager = eventManager.getFileFollowEventManager();
|
FileFollowEventManager fileFolloeEventManager = eventManager.getFileFollowEventManager();
|
||||||
Thread.sleep(5000L);
|
Thread.sleep(5000L);
|
||||||
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(path.resolve("test.txt"))) {
|
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(path.resolve("test.txt"))) {
|
||||||
|
|
|
@ -13,12 +13,13 @@ public class TimerEventManagerTest {
|
||||||
@Test
|
@Test
|
||||||
public void testTimerEvents() throws IOException, InterruptedException {
|
public void testTimerEvents() throws IOException, InterruptedException {
|
||||||
TestTimerEventConsumer consumer = new TestTimerEventConsumer();
|
TestTimerEventConsumer consumer = new TestTimerEventConsumer();
|
||||||
EventManager.register(consumer);
|
|
||||||
Settings settings = Settings.settingsBuilder()
|
Settings settings = Settings.settingsBuilder()
|
||||||
.put("event.timer.testtimerevent.enabled", "true")
|
.put("event.timer.testtimerevent.enabled", "true")
|
||||||
.put("event.timer.testtimerevent.class", "org.xbib.event.timer.TestTimerEvent")
|
.put("event.timer.testtimerevent.class", "org.xbib.event.timer.TestTimerEvent")
|
||||||
.build();
|
.build();
|
||||||
EventManager eventManager = EventManager.newEventManager(settings);
|
EventManager eventManager = EventManager.builder(settings)
|
||||||
|
.register(consumer)
|
||||||
|
.build();
|
||||||
TimerEventManager timerEventManager = eventManager.getTimerEventManager();
|
TimerEventManager timerEventManager = eventManager.getTimerEventManager();
|
||||||
timerEventManager.put("testtimerevent", Instant.now().plusSeconds(5L), Map.of("a", "b"));
|
timerEventManager.put("testtimerevent", Instant.now().plusSeconds(5L), Map.of("a", "b"));
|
||||||
Thread.sleep(10000L);
|
Thread.sleep(10000L);
|
||||||
|
|
Loading…
Reference in a new issue