refactoring to event manager services, new subproject for syslog with netty dependencies

main
Jörg Prante 6 months ago
parent 120a9e915d
commit 09a7a95b25

@ -6,5 +6,4 @@ dependencies {
implementation libs.time
implementation libs.datastructures.common
implementation libs.datastructures.json.tiny
implementation libs.netty.handler
}

@ -1,3 +1,5 @@
import org.xbib.event.common.EventManagerService;
module org.xbib.event.common {
exports org.xbib.event.bus;
exports org.xbib.event.clock;
@ -6,9 +8,9 @@ module org.xbib.event.common {
exports org.xbib.event.log;
exports org.xbib.event.path;
exports org.xbib.event.persistence;
exports org.xbib.event.syslog;
exports org.xbib.event.timer;
exports org.xbib.event.wal;
uses EventManagerService;
requires org.xbib.event.api;
requires org.xbib.settings.api;
requires org.xbib.settings.datastructures.json;
@ -16,10 +18,5 @@ module org.xbib.event.common {
requires org.xbib.time;
requires org.xbib.datastructures.common;
requires org.xbib.datastructures.json.tiny;
requires io.netty.buffer;
requires io.netty.common;
requires io.netty.transport;
requires io.netty.handler;
requires io.netty.codec;
requires java.logging;
}

@ -1,7 +1,7 @@
package org.xbib.event.clock;
import org.xbib.event.bus.AsyncEventBus;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings;
import org.xbib.time.schedule.CronExpression;
import org.xbib.time.schedule.CronSchedule;
@ -11,27 +11,25 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ClockEventManager implements Closeable {
public class ClockEventManagerService implements EventManagerService, Closeable {
private static final Logger logger = Logger.getLogger(ClockEventManager.class.getName());
private static final Logger logger = Logger.getLogger(ClockEventManagerService.class.getName());
private final CronSchedule<Integer> cronSchedule;
private CronSchedule<Integer> cronSchedule;
private final List<String> suspended;
private List<String> suspended;
public ClockEventManager(Settings settings) {
this(settings, new AsyncEventBus(Executors.newSingleThreadExecutor()), ClockEventManager.class.getClassLoader());
public ClockEventManagerService() {
}
public ClockEventManager(Settings settings,
EventBus eventBus,
ClassLoader classLoader) {
public ClockEventManagerService init(Settings settings, EventBus eventBus, ClassLoader classLoader, ExecutorService executorService) {
this.suspended = new ArrayList<>();
ThreadFactory threadFactory = new ThreadFactory() {
int n = 1;
@ -40,9 +38,9 @@ public class ClockEventManager implements Closeable {
return new Thread(r, "clock-event-manager-" + (n++));
}
};
ScheduledExecutorService executorService =
ScheduledExecutorService scheduledExecutorService =
Executors.newScheduledThreadPool(settings.getAsInt("pool.size", 2), threadFactory);
this.cronSchedule = new CronSchedule<>(executorService);
this.cronSchedule = new CronSchedule<>(scheduledExecutorService);
for (Map.Entry<String, Settings> clockEventService : settings.getGroups("event.clock").entrySet()) {
String name = clockEventService.getKey();
Settings entrySettings = clockEventService.getValue();
@ -69,6 +67,7 @@ public class ClockEventManager implements Closeable {
}
logger.log(Level.INFO, "entries = " + cronSchedule.getEntries());
cronSchedule.start();
return this;
}
public List<String> getSuspended() {

@ -10,7 +10,7 @@ public class ClockEventService implements Callable<Integer> {
private static final Logger logger = Logger.getLogger(ClockEventService.class.getName());
private final ClockEventManager manager;
private final ClockEventManagerService manager;
private final EventBus eventBus;
@ -18,7 +18,7 @@ public class ClockEventService implements Callable<Integer> {
private final Class<? extends ClockEvent> eventClass;
public ClockEventService(ClockEventManager manager,
public ClockEventService(ClockEventManagerService manager,
EventBus eventBus,
String name,
Class<? extends ClockEvent> eventClass) {

@ -1,22 +1,22 @@
package org.xbib.event.common;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import org.xbib.event.EventConsumer;
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.path.FileFollowEventManager;
import org.xbib.event.path.PathEventManager;
import org.xbib.event.syslog.SyslogEventManager;
import org.xbib.event.timer.TimerEventManager;
import org.xbib.event.clock.ClockEventManagerService;
import org.xbib.event.generic.GenericEventManagerService;
import org.xbib.event.path.FileFollowEventManagerService;
import org.xbib.event.path.PathEventManagerService;
import org.xbib.event.timer.TimerEventManagerService;
import org.xbib.settings.Settings;
import java.io.Closeable;
import java.io.IOException;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -31,54 +31,53 @@ public final class EventManager {
private final Builder builder;
private final GenericEventManager genericEventManager;
private final ClockEventManager clockEventManager;
private final TimerEventManager timerEventManager;
private final FileFollowEventManager fileFollowEventManager;
private final PathEventManager pathEventManager;
private final SyslogEventManager syslogEventManager;
private final Map<Class<? extends EventManagerService>, EventManagerService> services;
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);
this.pathEventManager = new PathEventManager(builder.settings, builder.eventBus, builder.executorService, builder.classLoader);
this.syslogEventManager = new SyslogEventManager(builder.settings, builder.eventBus);
this.services = new HashMap<>();
services.put(GenericEventManagerService.class, new GenericEventManagerService()
.init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService));
services.put(ClockEventManagerService.class, new ClockEventManagerService()
.init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService));
services.put(TimerEventManagerService.class, new TimerEventManagerService()
.init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService));
services.put(FileFollowEventManagerService.class, new FileFollowEventManagerService()
.init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService));
services.put(PathEventManagerService.class, new PathEventManagerService()
.init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService));
for (EventManagerService service : ServiceLoader.load(EventManagerService.class)) {
services.put(service.getClass(), service.init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService));
}
logger.log(Level.INFO, "installed event service managers = " + services.keySet());
}
public static Builder builder(Settings settings) {
return new Builder(settings);
}
public GenericEventManager getGenericEventManager() {
return genericEventManager;
public EventManagerService getEventManagerService(Class<? extends EventManagerService> cl) {
return services.get(cl);
}
public ClockEventManager getClockEventManager() {
return clockEventManager;
public GenericEventManagerService getGenericEventManagerService() {
return (GenericEventManagerService) services.get(GenericEventManagerService.class);
}
public TimerEventManager getTimerEventManager() {
return timerEventManager;
public ClockEventManagerService getClockEventManagerService() {
return (ClockEventManagerService) services.get(ClockEventManagerService.class);
}
public FileFollowEventManager getFileFollowEventManager() {
return fileFollowEventManager;
public TimerEventManagerService getTimerEventManagerService() {
return (TimerEventManagerService) services.get(TimerEventManagerService.class);
}
public PathEventManager getPathEventManager() {
return pathEventManager;
public FileFollowEventManagerService getFileFollowEventManagerService() {
return (FileFollowEventManagerService) services.get(FileFollowEventManagerService.class);
}
public SyslogEventManager getSyslogEventManager() {
return syslogEventManager;
public PathEventManagerService getPathEventManagerService() {
return (PathEventManagerService) services.get(PathEventManagerService.class);
}
public void close() throws IOException {
@ -87,11 +86,11 @@ public final class EventManager {
closeable.close();
}
}
clockEventManager.close();
timerEventManager.close();
fileFollowEventManager.close();
pathEventManager.close();
syslogEventManager.close();
for (EventManagerService service : services.values()) {
if (service instanceof Closeable closeable) {
closeable.close();
}
}
}
public static class Builder {

@ -0,0 +1,11 @@
package org.xbib.event.common;
import org.xbib.event.bus.EventBus;
import org.xbib.settings.Settings;
import java.util.concurrent.ExecutorService;
public interface EventManagerService {
EventManagerService init(Settings settings, EventBus eventBus, ClassLoader classLoader, ExecutorService executorService);
}

@ -2,17 +2,26 @@ package org.xbib.event.generic;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import org.xbib.event.bus.AsyncEventBus;
import org.xbib.event.bus.EventBus;
import org.xbib.event.bus.Subscriber;
import org.xbib.event.bus.SubscriberRegistry;
import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings;
public class GenericEventManager {
public class GenericEventManagerService implements EventManagerService {
private final AsyncEventBus eventBus;
private EventBus eventBus;
public GenericEventManager(AsyncEventBus eventBus) {
public GenericEventManagerService() {
}
@Override
public GenericEventManagerService init(Settings settings, EventBus eventBus, ClassLoader classLoader, ExecutorService executorService) {
this.eventBus = eventBus;
return this;
}
public void post(Object event) {

@ -1,6 +1,7 @@
package org.xbib.event.path;
import org.xbib.event.bus.AsyncEventBus;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings;
import java.io.Closeable;
@ -16,17 +17,21 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
public class FileFollowEventManager implements Closeable {
public class FileFollowEventManagerService implements EventManagerService, Closeable {
private static final Logger logger = Logger.getLogger(FileFollowEventManager.class.getName());
private static final Logger logger = Logger.getLogger(FileFollowEventManagerService.class.getName());
private final Map<Future<?>, FileFollowEventService> eventServiceMap;
private Map<Future<?>, FileFollowEventService> eventServiceMap;
public FileFollowEventManagerService() {
}
@SuppressWarnings("unchecked")
public FileFollowEventManager(Settings settings,
AsyncEventBus eventBus,
ExecutorService executorService,
ClassLoader classLoader) {
@Override
public FileFollowEventManagerService init(Settings settings,
EventBus eventBus,
ClassLoader classLoader,
ExecutorService executorService) {
this.eventServiceMap = new LinkedHashMap<>();
for (Map.Entry<String, Settings> entry : settings.getGroups("event.filefollow").entrySet()) {
Settings definition = entry.getValue();
@ -49,6 +54,7 @@ public class FileFollowEventManager implements Closeable {
}
}
}
return this;
}
@Override

@ -3,6 +3,7 @@ package org.xbib.event.path;
import org.xbib.datastructures.api.TimeValue;
import org.xbib.datastructures.json.tiny.Json;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings;
import java.io.Closeable;
@ -22,25 +23,29 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class PathEventManager implements Closeable {
public class PathEventManagerService implements EventManagerService, Closeable {
private static final Logger logger = Logger.getLogger(PathEventManager.class.getName());
private static final Logger logger = Logger.getLogger(PathEventManagerService.class.getName());
private final EventBus eventBus;
private EventBus eventBus;
private final ExecutorService executorService;
private ExecutorService executorService;
private final Path path;
private Path path;
private final Map<Future<?>, PathEventService> eventServiceMap;
private Map<Future<?>, PathEventService> eventServiceMap;
private final List<String> suspendedQueues;
private List<String> suspendedQueues;
public PathEventManagerService() {
}
@SuppressWarnings("unchecked")
public PathEventManager(Settings settings,
EventBus eventBus,
ExecutorService executorService,
ClassLoader classLoader) {
@Override
public PathEventManagerService init(Settings settings,
EventBus eventBus,
ClassLoader classLoader,
ExecutorService executorService) {
this.eventBus = eventBus;
this.executorService = executorService;
this.eventServiceMap = new LinkedHashMap<>();
@ -64,6 +69,7 @@ public class PathEventManager implements Closeable {
logger.log(Level.SEVERE, "unable to create event path service " + entry.getKey() + ", reason " + e.getMessage(), e);
}
}
return this;
}
public void createPathEventService(String name,

@ -32,7 +32,7 @@ public class PathEventService implements Callable<Integer>, Closeable {
public static final String FAIL = "fail";
private final PathEventManager pathEventManager;
private final PathEventManagerService pathEventManager;
private final EventBus eventBus;
@ -52,7 +52,7 @@ public class PathEventService implements Callable<Integer>, Closeable {
private volatile boolean keepWatching;
public PathEventService(PathEventManager pathEventManager,
public PathEventService(PathEventManagerService pathEventManager,
EventBus eventBus,
String name,
Path path,

@ -2,6 +2,7 @@ package org.xbib.event.timer;
import org.xbib.event.bus.AsyncEventBus;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManagerService;
import org.xbib.event.persistence.FilePersistenceStore;
import org.xbib.event.persistence.PersistenceStore;
import org.xbib.settings.Settings;
@ -16,26 +17,27 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TimerEventManager implements Closeable {
public class TimerEventManagerService implements EventManagerService, Closeable {
private static final Logger logger = Logger.getLogger(TimerEventManager.class.getName());
private static final Logger logger = Logger.getLogger(TimerEventManagerService.class.getName());
private final Map<String, TimerEventService> services;
private Map<String, TimerEventService> services;
public TimerEventManager(Settings settings) {
this(settings, new AsyncEventBus(Executors.newSingleThreadExecutor()),
TimerEventManager.class.getClassLoader(), ZoneId.systemDefault());
public TimerEventManagerService() {
}
@SuppressWarnings("unchecked")
public TimerEventManager(Settings settings,
EventBus eventBus,
ClassLoader classLoader,
ZoneId zoneId) {
@Override
public TimerEventManagerService init(Settings settings,
EventBus eventBus,
ClassLoader classLoader,
ExecutorService executorService) {
this.services = new LinkedHashMap<>();
for (Map.Entry<String, Settings> entry : settings.getGroups("event.timer").entrySet()) {
String name = entry.getKey();
@ -44,38 +46,43 @@ public class TimerEventManager implements Closeable {
try {
Class<? extends TimerEvent> eventClass = (Class<? extends TimerEvent>) classLoader.loadClass(className);
PersistenceStore<String, Object> persistenceStore = new FilePersistenceStore(timerSettings, name);
services.put(name, new TimerEventService(eventBus, name, eventClass, zoneId, persistenceStore));
services.put(name, new TimerEventService(eventBus, name, eventClass, ZoneId.systemDefault(), persistenceStore));
logger.log(Level.INFO, "timer " + name + " active for timer event class " + className);
} catch (Exception e) {
logger.log(Level.WARNING, "unable to activate timer " + name + ", reason: " + e.getMessage(), e);
}
}
return this;
}
public boolean put(String key, String timeSpec, Map<String,Object> map) throws ParseException, IOException {
if (services.containsKey(key)) {
public boolean put(String service,
String timeSpec,
Map<String, Object> map) throws ParseException, IOException {
if (services.containsKey(service)) {
Span span = Chronic.parse(timeSpec);
if (span != null) {
ZonedDateTime zonedDateTime = span.getBeginCalendar();
services.get(key).schedule(zonedDateTime.toInstant(), map);
services.get(service).schedule(zonedDateTime.toInstant(), map);
logger.log(Level.INFO, "scheduled to " + zonedDateTime);
} else {
logger.log(Level.INFO, "timer event key " + key + ": can not understand time spec " + timeSpec);
logger.log(Level.INFO, "timer event key " + service + ": can not understand time spec " + timeSpec);
return false;
}
return true;
} else {
logger.log(Level.SEVERE, "unknown timer event key: " + key);
logger.log(Level.SEVERE, "unknown timer event key: " + service);
}
return false;
}
public boolean put(String key, Instant instant, Map<String,Object> map) throws IOException {
if (services.containsKey(key)) {
services.get(key).schedule(instant, map);
public boolean put(String service,
Instant instant,
Map<String, Object> map) throws IOException {
if (services.containsKey(service)) {
services.get(service).schedule(instant, map);
return true;
} else {
logger.log(Level.SEVERE, "unknown timer event key: " + key);
logger.log(Level.SEVERE, "unknown timer event key: " + service);
}
return false;
}

@ -24,7 +24,7 @@ public class EventManagerTest {
EventManager eventManager = EventManager.builder(settings)
.register(consumer)
.build();
eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> {
eventManager.getGenericEventManagerService().post(new DefaultGenericEvent(e -> {
logger.log(Level.INFO, "received event " + e);
}));
}
@ -39,7 +39,7 @@ public class EventManagerTest {
.register(consumer)
.build();
CompletableFuture<GenericEvent> future = new CompletableFuture<>();
eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> {
eventManager.getGenericEventManagerService().post(new DefaultGenericEvent(e -> {
logger.log(Level.INFO, "received event " + e);
future.complete(e);
}));
@ -59,7 +59,7 @@ public class EventManagerTest {
.loadEventConsumers()
.build();
CompletableFuture<GenericEvent> future = new CompletableFuture<>();
eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> {
eventManager.getGenericEventManagerService().post(new DefaultGenericEvent(e -> {
logger.log(Level.INFO, "received event " + e);
}), future);
GenericEvent e = future.get();

@ -19,8 +19,7 @@ public class ClockEventManagerTest {
EventManager eventManager = EventManager.builder(settings)
.register(clockEventConsumer)
.build();
ClockEventManager clockEventManager = eventManager.getClockEventManager();
Thread.sleep(90000L);
clockEventManager.close();
eventManager.close();
}
}

@ -28,15 +28,14 @@ public class FileFollowEventManagerTest {
EventManager eventManager = EventManager.builder(settings)
.register(consumer)
.build();
FileFollowEventManager fileFolloeEventManager = eventManager.getFileFollowEventManager();
Thread.sleep(5000L);
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(path.resolve("test.txt"))) {
bufferedWriter.write("Hello");
logger.log(Level.INFO, "Hello written");
}
Thread.sleep(5000L);
fileFolloeEventManager.close();
Files.delete(path.resolve("test.txt"));
Files.delete(path);
eventManager.close();
}
}

@ -20,7 +20,7 @@ public class TimerEventManagerTest {
EventManager eventManager = EventManager.builder(settings)
.register(consumer)
.build();
TimerEventManager timerEventManager = eventManager.getTimerEventManager();
TimerEventManagerService timerEventManager = eventManager.getTimerEventManagerService();
timerEventManager.put("testtimerevent", Instant.now().plusSeconds(5L), Map.of("a", "b"));
Thread.sleep(10000L);
timerEventManager.close();

@ -0,0 +1,3 @@
dependencies {
}

@ -0,0 +1,5 @@
dependencies {
api project(':event-common')
implementation libs.datastructures.json.tiny
implementation libs.netty.handler
}

@ -0,0 +1,17 @@
import org.xbib.event.common.EventManagerService;
import org.xbib.event.syslog.SyslogEventManagerService;
module org.xbib.event.syslog {
exports org.xbib.event.syslog;
provides EventManagerService with SyslogEventManagerService;
requires org.xbib.event.common;
requires org.xbib.datastructures.json.tiny;
requires io.netty.buffer;
requires io.netty.common;
requires io.netty.transport;
requires io.netty.handler;
requires io.netty.codec;
requires java.logging;
requires org.xbib.settings.api;
requires org.xbib.event.api;
}

@ -1,6 +1,7 @@
package org.xbib.event.syslog;
import org.xbib.event.bus.AsyncEventBus;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings;
import java.io.Closeable;
@ -8,16 +9,22 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SyslogEventManager implements Closeable {
private static final Logger logger = Logger.getLogger(SyslogEventManager.class.getName());
public class SyslogEventManagerService implements EventManagerService, Closeable {
private static final Logger logger = Logger.getLogger(SyslogEventManagerService.class.getName());
private final List<SyslogService> syslogServices;
private List<SyslogService> syslogServices;
public SyslogEventManager(Settings settings,
AsyncEventBus eventBus) {
public SyslogEventManagerService() {}
@Override
public SyslogEventManagerService init(Settings settings,
EventBus eventBus,
ClassLoader classLoader,
ExecutorService executorService) {
this.syslogServices = new ArrayList<>();
for (Map.Entry<String, Settings> entry : settings.getGroups("event.syslog").entrySet()) {
Settings definition = entry.getValue();
@ -31,6 +38,7 @@ public class SyslogEventManager implements Closeable {
}
}
}
return this;
}
@Override

@ -1,6 +1,7 @@
package org.xbib.event.syslog;
package org.xbib.event.syslog.test;
import org.junit.jupiter.api.Test;
import org.xbib.event.syslog.SyslogService;
import org.xbib.settings.Settings;
import java.io.IOException;
@ -14,7 +15,7 @@ public class SyslogServiceTest {
.build();
SyslogService syslogService = new SyslogService(settings, null);
syslogService.startTcp();
Thread.sleep(60000L);
Thread.sleep(60000L); // send a syslog from console
syslogService.close();
}
}

@ -1,3 +1,3 @@
group = org.xbib
name = event
version = 0.1.0
version = 0.2.0

@ -45,3 +45,4 @@ include 'event-async'
include 'event-common'
include 'event-loop'
include 'event-net-http'
include 'event-syslog'

Loading…
Cancel
Save