add generic event manager

This commit is contained in:
Jörg Prante 2023-12-05 22:15:22 +01:00
parent 6fa7b63753
commit 3145720f40
5 changed files with 53 additions and 3 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib
name = event
version = 0.0.6
version = 0.0.7
org.gradle.warning.mode = ALL

View file

@ -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;

View file

@ -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<EventConsumer> 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);

View file

@ -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() {
}
}

View file

@ -1,7 +1,6 @@
package org.xbib.event.timer;
import org.xbib.event.DefaultEvent;
import org.xbib.event.clock.ClockEvent;
import java.time.Instant;