add http events

This commit is contained in:
Jörg Prante 2024-01-19 13:54:14 +01:00
parent 5c31d6e055
commit 99cc9e2c90
15 changed files with 133 additions and 67 deletions

View file

@ -1,9 +1,9 @@
dependencies {
api project(':event-api')
api libs.settings.api
implementation libs.settings.datastructures.json
implementation libs.net
implementation libs.time
implementation libs.settings.datastructures.json
implementation libs.datastructures.common
implementation libs.datastructures.json.tiny
}

View file

@ -11,6 +11,7 @@ module org.xbib.event.common {
exports org.xbib.event.timer;
exports org.xbib.event.wal;
uses EventManagerService;
uses org.xbib.event.EventConsumer;
requires org.xbib.event.api;
requires org.xbib.settings.api;
requires org.xbib.settings.datastructures.json;

View file

@ -1,24 +1,23 @@
package org.xbib.event.clock;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManager;
import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings;
import org.xbib.time.schedule.CronExpression;
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.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 ClockEventManagerService implements EventManagerService, Closeable {
public class ClockEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(ClockEventManagerService.class.getName());
@ -29,7 +28,10 @@ public class ClockEventManagerService implements EventManagerService, Closeable
public ClockEventManagerService() {
}
public ClockEventManagerService init(Settings settings, EventBus eventBus, ClassLoader classLoader, ExecutorService executorService) {
public ClockEventManagerService init(EventManager eventManager) {
Settings settings = eventManager.getSettings();
EventBus eventBus = eventManager.getEventBus();
ClassLoader classLoader = eventManager.getClassLoader();
this.suspended = new ArrayList<>();
ThreadFactory threadFactory = new ThreadFactory() {
int n = 1;
@ -83,7 +85,7 @@ public class ClockEventManagerService implements EventManagerService, Closeable
}
@Override
public void close() throws IOException {
public void shutdown() throws IOException {
cronSchedule.close();
}
}

View file

@ -7,6 +7,7 @@ import java.util.ServiceLoader;
import org.xbib.event.Event;
import org.xbib.event.EventConsumer;
import org.xbib.event.bus.AsyncEventBus;
import org.xbib.event.bus.EventBus;
import org.xbib.event.bus.SubscriberExceptionContext;
import org.xbib.event.bus.SubscriberExceptionHandler;
import org.xbib.event.clock.ClockEventManagerService;
@ -37,22 +38,33 @@ public final class EventManager {
private EventManager(Builder builder) {
this.builder = builder;
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));
services.put(GenericEventManagerService.class, new GenericEventManagerService().init(this));
services.put(ClockEventManagerService.class, new ClockEventManagerService().init(this));
services.put(TimerEventManagerService.class, new TimerEventManagerService().init(this));
services.put(FileFollowEventManagerService.class, new FileFollowEventManagerService().init(this));
services.put(PathEventManagerService.class, new PathEventManagerService().init(this));
for (EventManagerService service : ServiceLoader.load(EventManagerService.class)) {
services.put(service.getClass(), service.init(builder.settings, builder.eventBus, builder.classLoader, builder.executorService));
services.put(service.getClass(), service.init(this));
}
logger.log(Level.INFO, "installed event service managers = " + services.keySet());
}
public Settings getSettings() {
return builder.settings;
}
public EventBus getEventBus() {
return builder.eventBus;
}
public ClassLoader getClassLoader() {
return builder.classLoader;
}
public ExecutorService getExecutorService() {
return builder.executorService;
}
public static Builder builder(Settings settings) {
return new Builder(settings);
}
@ -92,8 +104,10 @@ public final class EventManager {
}
}
for (EventManagerService service : services.values()) {
if (service instanceof Closeable closeable) {
closeable.close();
try {
service.shutdown();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
}

View file

@ -1,11 +1,10 @@
package org.xbib.event.common;
import org.xbib.event.bus.EventBus;
import org.xbib.settings.Settings;
import java.util.concurrent.ExecutorService;
import java.io.IOException;
public interface EventManagerService {
EventManagerService init(Settings settings, EventBus eventBus, ClassLoader classLoader, ExecutorService executorService);
EventManagerService init(EventManager eventManager);
void shutdown() throws IOException;
}

View file

@ -1,14 +1,14 @@
package org.xbib.event.generic;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import org.xbib.event.bus.EventBus;
import org.xbib.event.bus.Subscriber;
import org.xbib.event.bus.SubscriberRegistry;
import org.xbib.event.common.EventManager;
import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings;
public class GenericEventManagerService implements EventManagerService {
@ -18,11 +18,16 @@ public class GenericEventManagerService implements EventManagerService {
}
@Override
public GenericEventManagerService init(Settings settings, EventBus eventBus, ClassLoader classLoader, ExecutorService executorService) {
this.eventBus = eventBus;
public GenericEventManagerService init(EventManager eventManager) {
this.eventBus = eventManager.getEventBus();
return this;
}
@Override
public void shutdown() throws IOException {
}
public void post(Object event) {
eventBus.post(event);
}

View file

@ -1,6 +1,7 @@
package org.xbib.event.path;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManager;
import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings;
@ -17,7 +18,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
public class FileFollowEventManagerService implements EventManagerService, Closeable {
public class FileFollowEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(FileFollowEventManagerService.class.getName());
@ -28,10 +29,11 @@ public class FileFollowEventManagerService implements EventManagerService, Close
@SuppressWarnings("unchecked")
@Override
public FileFollowEventManagerService init(Settings settings,
EventBus eventBus,
ClassLoader classLoader,
ExecutorService executorService) {
public FileFollowEventManagerService init(EventManager eventManager) {
Settings settings = eventManager.getSettings();
EventBus eventBus = eventManager.getEventBus();
ClassLoader classLoader = eventManager.getClassLoader();
ExecutorService executorService = eventManager.getExecutorService();
this.eventServiceMap = new LinkedHashMap<>();
for (Map.Entry<String, Settings> entry : settings.getGroups("event.filefollow").entrySet()) {
Settings definition = entry.getValue();
@ -58,10 +60,14 @@ public class FileFollowEventManagerService implements EventManagerService, Close
}
@Override
public void close() throws IOException {
public void shutdown() throws IOException {
for (Map.Entry<Future<?>, FileFollowEventService> entry : eventServiceMap.entrySet()) {
try {
entry.getValue().setKeepWatching(false);
entry.getKey().cancel(true);
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
}
}

View file

@ -3,10 +3,10 @@ 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.EventManager;
import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings;
import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
@ -23,7 +23,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class PathEventManagerService implements EventManagerService, Closeable {
public class PathEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(PathEventManagerService.class.getName());
@ -42,12 +42,11 @@ public class PathEventManagerService implements EventManagerService, Closeable {
@SuppressWarnings("unchecked")
@Override
public PathEventManagerService init(Settings settings,
EventBus eventBus,
ClassLoader classLoader,
ExecutorService executorService) {
this.eventBus = eventBus;
this.executorService = executorService;
public PathEventManagerService init(EventManager eventManager) {
Settings settings = eventManager.getSettings();
ClassLoader classLoader = eventManager.getClassLoader();
this.eventBus = eventManager.getEventBus();
this.executorService = eventManager.getExecutorService();
this.eventServiceMap = new LinkedHashMap<>();
this.suspendedQueues = new ArrayList<>();
this.path = Paths.get(settings.get("event.pathevent.base", "/var/tmp/pathevent"));
@ -90,8 +89,8 @@ public class PathEventManagerService implements EventManagerService, Closeable {
}
@Override
public void close() throws IOException {
logger.log(Level.INFO, "closing all path event services");
public void shutdown() throws IOException {
logger.log(Level.INFO, "shut down all path event services");
eventServiceMap.forEach((k, v) -> {
k.cancel(true);
try {

View file

@ -1,7 +1,7 @@
package org.xbib.event.timer;
import org.xbib.event.bus.AsyncEventBus;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManager;
import org.xbib.event.common.EventManagerService;
import org.xbib.event.persistence.FilePersistenceStore;
import org.xbib.event.persistence.PersistenceStore;
@ -9,7 +9,6 @@ import org.xbib.settings.Settings;
import org.xbib.time.chronic.Chronic;
import org.xbib.time.chronic.Span;
import java.io.Closeable;
import java.io.IOException;
import java.text.ParseException;
import java.time.Instant;
@ -17,12 +16,10 @@ 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 TimerEventManagerService implements EventManagerService, Closeable {
public class TimerEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(TimerEventManagerService.class.getName());
@ -31,13 +28,12 @@ public class TimerEventManagerService implements EventManagerService, Closeable
public TimerEventManagerService() {
}
@SuppressWarnings("unchecked")
@Override
public TimerEventManagerService init(Settings settings,
EventBus eventBus,
ClassLoader classLoader,
ExecutorService executorService) {
public TimerEventManagerService init(EventManager eventManager) {
Settings settings = eventManager.getSettings();
EventBus eventBus = eventManager.getEventBus();
ClassLoader classLoader = eventManager.getClassLoader();
this.services = new LinkedHashMap<>();
for (Map.Entry<String, Settings> entry : settings.getGroups("event.timer").entrySet()) {
String name = entry.getKey();
@ -95,7 +91,7 @@ public class TimerEventManagerService implements EventManagerService, Closeable
}
@Override
public void close() throws IOException {
public void shutdown() throws IOException {
for (Map.Entry<String, TimerEventService> entry : services.entrySet()) {
logger.log(Level.INFO, "closing timer " + entry.getKey());
entry.getValue().close();

View file

@ -2,5 +2,6 @@ dependencies {
api project(':event-common')
api libs.net.http.server.netty
api libs.net.http.client.netty
implementation libs.settings.datastructures.json
implementation libs.datastructures.json.tiny
}

View file

@ -1,5 +1,9 @@
import org.xbib.event.common.EventManagerService;
import org.xbib.event.net.http.HttpEventManagerService;
module org.xbib.event.net.http {
exports org.xbib.event.net.http;
provides EventManagerService with HttpEventManagerService;
requires org.xbib.event.api;
requires org.xbib.event.common;
requires org.xbib.net.http;
@ -9,5 +13,6 @@ module org.xbib.event.net.http {
requires org.xbib.net.http.server;
requires org.xbib.net.http.server.netty;
requires org.xbib.net.http.server.netty.secure;
requires org.xbib.settings.api;
requires java.logging;
}

View file

@ -0,0 +1,21 @@
package org.xbib.event.net.http;
import java.io.IOException;
import org.xbib.event.common.EventManager;
import org.xbib.event.common.EventManagerService;
public class HttpEventManagerService implements EventManagerService {
public HttpEventManagerService() {
}
@Override
public EventManagerService init(EventManager eventManager) {
return null;
}
@Override
public void shutdown() throws IOException {
}
}

View file

@ -13,11 +13,11 @@ import java.nio.charset.StandardCharsets;
import static org.xbib.net.http.HttpResponseStatus.NOT_FOUND;
import static org.xbib.net.http.HttpResponseStatus.OK;
public class EventReceiverService {
public class HttpEventReceiverService {
private final EventManager eventManager;
public EventReceiverService(EventManager eventManager) {
public HttpEventReceiverService(EventManager eventManager) {
this.eventManager = eventManager;
}

View file

@ -0,0 +1,18 @@
package org.xbib.event.net.http;
import org.xbib.event.common.EventManager;
public class HttpEventSubmitterService {
private final EventManager eventManager;
public HttpEventSubmitterService(EventManager eventManager) {
this.eventManager = eventManager;
}
public void submit(Object event) {
}
}

View file

@ -1,19 +1,19 @@
package org.xbib.event.syslog;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManager;
import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings;
import java.io.Closeable;
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 SyslogEventManagerService implements EventManagerService, Closeable {
public class SyslogEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(SyslogEventManagerService.class.getName());
private List<SyslogService> syslogServices;
@ -21,10 +21,9 @@ public class SyslogEventManagerService implements EventManagerService, Closeable
public SyslogEventManagerService() {}
@Override
public SyslogEventManagerService init(Settings settings,
EventBus eventBus,
ClassLoader classLoader,
ExecutorService executorService) {
public SyslogEventManagerService init(EventManager eventManager) {
Settings settings = eventManager.getSettings();
EventBus eventBus = eventManager.getEventBus();
this.syslogServices = new ArrayList<>();
for (Map.Entry<String, Settings> entry : settings.getGroups("event.syslog").entrySet()) {
Settings definition = entry.getValue();
@ -42,7 +41,7 @@ public class SyslogEventManagerService implements EventManagerService, Closeable
}
@Override
public void close() throws IOException {
public void shutdown() throws IOException {
for (SyslogService syslogService : syslogServices) {
syslogService.close();
}