json format of event

This commit is contained in:
Jörg Prante 2024-02-05 17:53:40 +01:00
parent 788ce1b7d7
commit 757e62040b
18 changed files with 239 additions and 249 deletions

View file

@ -4,7 +4,6 @@ module org.xbib.event.common {
exports org.xbib.event.bus; exports org.xbib.event.bus;
exports org.xbib.event.clock; exports org.xbib.event.clock;
exports org.xbib.event.common; exports org.xbib.event.common;
exports org.xbib.event.generic;
exports org.xbib.event.log; exports org.xbib.event.log;
exports org.xbib.event.path; exports org.xbib.event.path;
exports org.xbib.event.persistence; exports org.xbib.event.persistence;

View file

@ -1,6 +1,7 @@
package org.xbib.event.clock; package org.xbib.event.clock;
import org.xbib.event.bus.EventBus; import org.xbib.event.Event;
import org.xbib.event.common.AbstractEventManagerService;
import org.xbib.event.common.EventManager; import org.xbib.event.common.EventManager;
import org.xbib.event.common.EventManagerService; import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings; import org.xbib.settings.Settings;
@ -8,8 +9,6 @@ import org.xbib.time.schedule.CronExpression;
import org.xbib.time.schedule.CronSchedule; import org.xbib.time.schedule.CronSchedule;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -17,21 +16,21 @@ import java.util.concurrent.ThreadFactory;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public class ClockEventManagerService implements EventManagerService { public class ClockEventManagerService extends AbstractEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(ClockEventManagerService.class.getName()); private static final Logger logger = Logger.getLogger(ClockEventManagerService.class.getName());
private CronSchedule<Integer> cronSchedule; private EventManager eventManager;
private List<String> suspended; private CronSchedule<Boolean> cronSchedule;
public ClockEventManagerService() { public ClockEventManagerService() {
super();
} }
public ClockEventManagerService init(EventManager eventManager) { public ClockEventManagerService init(EventManager eventManager) {
this.eventManager = eventManager;
Settings settings = eventManager.getSettings(); Settings settings = eventManager.getSettings();
EventBus eventBus = eventManager.getEventBus();
this.suspended = new ArrayList<>();
ThreadFactory threadFactory = new ThreadFactory() { ThreadFactory threadFactory = new ThreadFactory() {
int n = 1; int n = 1;
@Override @Override
@ -49,7 +48,7 @@ public class ClockEventManagerService implements EventManagerService {
String entry = entrySettings.get("entry"); String entry = entrySettings.get("entry");
if (entry != null) { if (entry != null) {
try { try {
ClockEventService clockEventService = new ClockEventService(this, eventBus, name); ClockEventService clockEventService = new ClockEventService(this, name);
cronSchedule.add(name, CronExpression.parse(entry), clockEventService); cronSchedule.add(name, CronExpression.parse(entry), clockEventService);
logger.log(Level.INFO, "cron job " + name + " scheduled on " + entry); logger.log(Level.INFO, "cron job " + name + " scheduled on " + entry);
} catch (Exception e) { } catch (Exception e) {
@ -65,20 +64,12 @@ public class ClockEventManagerService implements EventManagerService {
return this; return this;
} }
public List<String> getSuspended() {
return suspended;
}
public void suspend(String name) {
suspended.add(name);
}
public void resume(String name) {
suspended.remove(name);
}
@Override @Override
public void shutdown() throws IOException { public void shutdown() throws IOException {
cronSchedule.close(); cronSchedule.close();
} }
public void publish(Event event) {
eventManager.publish(event);
}
} }

View file

@ -5,43 +5,38 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.xbib.event.Event; import org.xbib.event.Event;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManager; import org.xbib.event.common.EventManager;
public class ClockEventService implements Callable<Integer> { public class ClockEventService implements Callable<Boolean> {
private static final Logger logger = Logger.getLogger(ClockEventService.class.getName()); private static final Logger logger = Logger.getLogger(ClockEventService.class.getName());
private final ClockEventManagerService manager; private final ClockEventManagerService clockEventManagerService;
private final EventBus eventBus;
private final String name; private final String name;
public ClockEventService(ClockEventManagerService manager, public ClockEventService(ClockEventManagerService clockEventManagerService,
EventBus eventBus,
String name) { String name) {
this.manager = manager; this.clockEventManagerService = clockEventManagerService;
this.eventBus = eventBus;
this.name = name; this.name = name;
} }
@Override @Override
public Integer call() { public Boolean call() {
try { try {
if (manager.getSuspended().contains(name)) { if (clockEventManagerService.getSuspended().contains(name)) {
logger.log(Level.FINE, "clock event " + name + " suspended"); logger.log(Level.FINE, "clock event " + name + " suspended");
return 1; return false;
} else { } else {
Event clockEvent = EventManager.eventBuilder() Event clockEvent = EventManager.eventBuilder()
.setType("clock") .setType("clock")
.build(); .build();
eventBus.post(clockEvent); clockEventManagerService.publish(clockEvent);
return 0; return true;
} }
} catch (Throwable t) { } catch (Throwable t) {
logger.log(Level.WARNING, t.getMessage(), t); logger.log(Level.WARNING, t.getMessage(), t);
return 1; return false;
} }
} }
} }

View file

@ -0,0 +1,25 @@
package org.xbib.event.common;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public abstract class AbstractEventManagerService {
private List<String> suspended;
public AbstractEventManagerService() {
this.suspended = new CopyOnWriteArrayList<>();
}
public void suspend(String name) {
suspended.add(name);
}
public void resume(String name) {
suspended.remove(name);
}
public List<String> getSuspended() {
return suspended;
}
}

View file

@ -11,16 +11,18 @@ import java.util.Map;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import org.xbib.datastructures.json.tiny.Json; import org.xbib.datastructures.json.tiny.Json;
import org.xbib.datastructures.json.tiny.JsonBuilder;
import org.xbib.event.Event; import org.xbib.event.Event;
import org.xbib.event.EventConsumer; import org.xbib.event.EventConsumer;
import org.xbib.event.Listener; import org.xbib.event.Listener;
import org.xbib.event.Payload; import org.xbib.event.Payload;
import org.xbib.event.bus.AsyncEventBus; import org.xbib.event.bus.AsyncEventBus;
import org.xbib.event.bus.EventBus; import org.xbib.event.bus.EventBus;
import org.xbib.event.bus.Subscriber;
import org.xbib.event.bus.SubscriberExceptionContext; import org.xbib.event.bus.SubscriberExceptionContext;
import org.xbib.event.bus.SubscriberExceptionHandler; import org.xbib.event.bus.SubscriberExceptionHandler;
import org.xbib.event.bus.SubscriberRegistry;
import org.xbib.event.clock.ClockEventManagerService; import org.xbib.event.clock.ClockEventManagerService;
import org.xbib.event.generic.GenericEventManagerService;
import org.xbib.event.path.FileFollowEventManagerService; import org.xbib.event.path.FileFollowEventManagerService;
import org.xbib.event.path.PathEventManagerService; import org.xbib.event.path.PathEventManagerService;
import org.xbib.event.timer.TimerEventManagerService; import org.xbib.event.timer.TimerEventManagerService;
@ -31,12 +33,14 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public final class EventManager { public final class EventManager extends AbstractEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(EventManager.class.getName()); private static final Logger logger = Logger.getLogger(EventManager.class.getName());
@ -47,6 +51,7 @@ public final class EventManager {
private final Map<Class<? extends EventManagerService>, EventManagerService> eventManagerServices; private final Map<Class<? extends EventManagerService>, EventManagerService> eventManagerServices;
private EventManager(EventManagerBuilder builder) { private EventManager(EventManagerBuilder builder) {
super();
this.builder = builder; this.builder = builder;
eventTypes.put("null", NullEvent.class); eventTypes.put("null", NullEvent.class);
eventTypes.put("generic", GenericEventImpl.class); eventTypes.put("generic", GenericEventImpl.class);
@ -59,7 +64,7 @@ public final class EventManager {
} }
logger.log(Level.INFO, "installed events = " + eventTypes.keySet()); logger.log(Level.INFO, "installed events = " + eventTypes.keySet());
this.eventManagerServices = new HashMap<>(); this.eventManagerServices = new HashMap<>();
eventManagerServices.put(GenericEventManagerService.class, new GenericEventManagerService().init(this)); eventManagerServices.put(this.getClass(), this);
eventManagerServices.put(ClockEventManagerService.class, new ClockEventManagerService().init(this)); eventManagerServices.put(ClockEventManagerService.class, new ClockEventManagerService().init(this));
eventManagerServices.put(TimerEventManagerService.class, new TimerEventManagerService().init(this)); eventManagerServices.put(TimerEventManagerService.class, new TimerEventManagerService().init(this));
eventManagerServices.put(FileFollowEventManagerService.class, new FileFollowEventManagerService().init(this)); eventManagerServices.put(FileFollowEventManagerService.class, new FileFollowEventManagerService().init(this));
@ -70,6 +75,10 @@ public final class EventManager {
logger.log(Level.INFO, "installed event service managers = " + eventManagerServices.keySet()); logger.log(Level.INFO, "installed event service managers = " + eventManagerServices.keySet());
} }
public EventManagerService init(EventManager eventManager) {
return this;
}
public static EventManagerBuilder builder() { public static EventManagerBuilder builder() {
return new EventManagerBuilder(); return new EventManagerBuilder();
} }
@ -94,19 +103,11 @@ public final class EventManager {
return builder.executorService; return builder.executorService;
} }
public void dispatch(Event event) {
getGenericEventManagerService().post(event);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends EventManagerService> T getEventManagerService(Class<T> cl) { public <T extends EventManagerService> T getEventManagerService(Class<T> cl) {
return (T) eventManagerServices.get(cl); return (T) eventManagerServices.get(cl);
} }
public GenericEventManagerService getGenericEventManagerService() {
return (GenericEventManagerService) eventManagerServices.get(GenericEventManagerService.class);
}
public ClockEventManagerService getClockEventManagerService() { public ClockEventManagerService getClockEventManagerService() {
return (ClockEventManagerService) eventManagerServices.get(ClockEventManagerService.class); return (ClockEventManagerService) eventManagerServices.get(ClockEventManagerService.class);
} }
@ -123,7 +124,19 @@ public final class EventManager {
return (PathEventManagerService) eventManagerServices.get(PathEventManagerService.class); return (PathEventManagerService) eventManagerServices.get(PathEventManagerService.class);
} }
public void close() throws IOException { public void publish(Event event) {
getEventBus().post(event);
}
public void publish(GenericEventImpl event,
CompletableFuture<Event> future) {
SubscriberRegistry subscriberRegistry = getEventBus().getSubscribers();
Set<Subscriber> set = subscriberRegistry.getSubscribersForTesting(event.getClass());
event.setListener(new WrappedListener(event.getListener(), set.size(), future));
publish(event);
}
public void shutdown() throws IOException {
for (EventConsumer eventConsumer : builder.eventConsumers) { for (EventConsumer eventConsumer : builder.eventConsumers) {
if (eventConsumer instanceof Closeable closeable) { if (eventConsumer instanceof Closeable closeable) {
closeable.close(); closeable.close();
@ -131,7 +144,9 @@ public final class EventManager {
} }
for (EventManagerService service : eventManagerServices.values()) { for (EventManagerService service : eventManagerServices.values()) {
try { try {
service.shutdown(); if (service != this) {
service.shutdown();
}
} catch (IOException e) { } catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e); logger.log(Level.SEVERE, e.getMessage(), e);
} }
@ -244,6 +259,33 @@ public final class EventManager {
} }
} }
private static class WrappedListener implements Listener {
private final Listener listener;
private int size;
private final CompletableFuture<Event> future;
public WrappedListener(Listener listener, int size, CompletableFuture<Event> future) {
this.listener = listener;
this.size = size;
this.future = future;
}
@Override
public void listen(Event event) {
if (listener != null) {
listener.listen(event);
} else {
logger.log(Level.WARNING, "listener not set");
}
if (--size == 0) {
future.complete(event);
}
}
}
public static Event eventOf(String eventType, public static Event eventOf(String eventType,
String code, String code,
String message, String message,
@ -338,8 +380,22 @@ public final class EventManager {
return builder.fileSize; return builder.fileSize;
} }
@Override
public String toJson() throws IOException { public String toJson() throws IOException {
return Json.toString(builder.payload); JsonBuilder builder = JsonBuilder.builder();
builder.beginMap();
builder.fieldIfNotNull("type", getType());
builder.fieldIfNotNull("code", getCode());
builder.fieldIfNotNull("message", getMessage());
builder.buildKey("payload").buildMap(getPayload());
builder.fieldIfNotNull("created", getCreated() != null ? getCreated().toString() : null);
builder.fieldIfNotNull("scheduled", getScheduledFor() != null ? getScheduledFor().toString() : null);
builder.fieldIfNotNull("path", getPath() != null ? getPath().toAbsolutePath().toString() : null);
builder.fieldIfNotNull("base", getBase());
builder.fieldIfNotNull("suffix", getSuffix());
builder.fieldIfNotNull("filesize", getFileSize());
builder.endMap();
return builder.build();
} }
@Override @Override
@ -507,5 +563,4 @@ public final class EventManager {
return pos >= 0 ? name.substring(pos + 1) : null; return pos >= 0 ? name.substring(pos + 1) : null;
} }
} }
} }

View file

@ -1,77 +0,0 @@
package org.xbib.event.generic;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xbib.event.Event;
import org.xbib.event.Listener;
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.event.common.GenericEventImpl;
public class GenericEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(GenericEventManagerService.class.getName());
private EventBus eventBus;
public GenericEventManagerService() {
}
@Override
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);
}
public void post(GenericEventImpl event,
CompletableFuture<Event> future) {
SubscriberRegistry subscriberRegistry = eventBus.getSubscribers();
Set<Subscriber> set = subscriberRegistry.getSubscribersForTesting(event.getClass());
logger.log(Level.INFO, "set = " + set);
event.setListener(new WrappedListener(event.getListener(), set.size(), future));
post(event);
}
static class WrappedListener implements Listener {
private final Listener listener;
private int size;
private final CompletableFuture<Event> future;
public WrappedListener(Listener listener, int size, CompletableFuture<Event> future) {
this.listener = listener;
this.size = size;
this.future = future;
}
@Override
public void listen(Event event) {
if (listener != null) {
listener.listen(event);
} else {
logger.log(Level.WARNING, "listener not set");
}
if (--size == 0) {
future.complete(event);
}
}
}
}

View file

@ -1,6 +1,8 @@
package org.xbib.event.path; package org.xbib.event.path;
import org.xbib.event.Event;
import org.xbib.event.bus.EventBus; import org.xbib.event.bus.EventBus;
import org.xbib.event.common.AbstractEventManagerService;
import org.xbib.event.common.EventManager; import org.xbib.event.common.EventManager;
import org.xbib.event.common.EventManagerService; import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings; import org.xbib.settings.Settings;
@ -17,22 +19,26 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class FileFollowEventManagerService implements EventManagerService { public class FileFollowEventManagerService extends AbstractEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(FileFollowEventManagerService.class.getName()); private static final Logger logger = Logger.getLogger(FileFollowEventManagerService.class.getName());
private EventManager eventManager;
private Map<Future<?>, FileFollowEventService> eventServiceMap; private Map<Future<?>, FileFollowEventService> eventServiceMap;
public FileFollowEventManagerService() { public FileFollowEventManagerService() {
super();
} }
@Override @Override
public FileFollowEventManagerService init(EventManager eventManager) { public FileFollowEventManagerService init(EventManager eventManager) {
this.eventManager = eventManager;
Settings settings = eventManager.getSettings(); Settings settings = eventManager.getSettings();
EventBus eventBus = eventManager.getEventBus();
ExecutorService executorService = eventManager.getExecutorService(); ExecutorService executorService = eventManager.getExecutorService();
this.eventServiceMap = new LinkedHashMap<>(); this.eventServiceMap = new LinkedHashMap<>();
for (Map.Entry<String, Settings> entry : settings.getGroups("event.filefollow").entrySet()) { for (Map.Entry<String, Settings> entry : settings.getGroups("event.filefollow").entrySet()) {
String name = entry.getKey();
Settings definition = entry.getValue(); Settings definition = entry.getValue();
if (definition.getAsBoolean("enabled", true)) { if (definition.getAsBoolean("enabled", true)) {
String baseStr = definition.get("base"); String baseStr = definition.get("base");
@ -42,12 +48,12 @@ public class FileFollowEventManagerService implements EventManagerService {
try { try {
Path base = Paths.get(baseStr); Path base = Paths.get(baseStr);
Pattern pattern = Pattern.compile(patternStr); Pattern pattern = Pattern.compile(patternStr);
FileFollowEventService fileFollowEventService = new FileFollowEventService(definition, eventBus, base, pattern); FileFollowEventService fileFollowEventService = new FileFollowEventService(this, name, base, pattern);
Future<?> future = executorService.submit(fileFollowEventService); Future<?> future = executorService.submit(fileFollowEventService);
eventServiceMap.put(future, fileFollowEventService); eventServiceMap.put(future, fileFollowEventService);
logger.log(Level.INFO, "file follow service " + entry.getKey() + " with base " + base + " and pattern " + pattern + " added"); logger.log(Level.INFO, "file follow service " + name + " with base " + base + " and pattern " + pattern + " added");
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "unable to create file follow service " + entry.getKey() + ", reason " + e.getMessage(), e); logger.log(Level.SEVERE, "unable to create file follow service " +name + ", reason " + e.getMessage(), e);
} }
} }
} }
@ -65,4 +71,8 @@ public class FileFollowEventManagerService implements EventManagerService {
} }
} }
} }
public void publish(Event event) {
eventManager.publish(event);
}
} }

View file

@ -1,9 +1,7 @@
package org.xbib.event.path; package org.xbib.event.path;
import org.xbib.event.Event; import org.xbib.event.Event;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManager; import org.xbib.event.common.EventManager;
import org.xbib.settings.Settings;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
@ -33,7 +31,9 @@ public class FileFollowEventService implements Callable<Integer>, Closeable {
private static final Logger logger = Logger.getLogger(FileFollowEventService.class.getName()); private static final Logger logger = Logger.getLogger(FileFollowEventService.class.getName());
private final EventBus eventBus; private final FileFollowEventManagerService fileFollowEventManagerService;
private final String name;
private final Path base; private final Path base;
@ -45,11 +45,12 @@ public class FileFollowEventService implements Callable<Integer>, Closeable {
private volatile boolean keepWatching; private volatile boolean keepWatching;
public FileFollowEventService(Settings settings, public FileFollowEventService(FileFollowEventManagerService fileFollowEventManagerService,
EventBus eventBus, String name,
Path base, Path base,
Pattern pattern) throws IOException { Pattern pattern) throws IOException {
this.eventBus = eventBus; this.fileFollowEventManagerService = fileFollowEventManagerService;
this.name = name;
this.base = base; this.base = base;
this.pattern = pattern; this.pattern = pattern;
FileSystem fileSystem = base.getFileSystem(); FileSystem fileSystem = base.getFileSystem();
@ -89,13 +90,17 @@ public class FileFollowEventService implements Callable<Integer>, Closeable {
String content = readRange(channel, lastSize, currentSize); String content = readRange(channel, lastSize, currentSize);
// split content by line, this allows pattern matching without preprocessing in worker // split content by line, this allows pattern matching without preprocessing in worker
for (String line : content.split("\n")) { for (String line : content.split("\n")) {
Event event = EventManager.eventBuilder() if (fileFollowEventManagerService.getSuspended().contains(name)) {
.setType("filefollow") logger.log(Level.WARNING, name + " is suspended");
.setCode(base.toString()) } else {
.setPath(path) Event event = EventManager.eventBuilder()
.setMessage(line) .setType("filefollow")
.build(); .setCode(base.toString())
eventBus.post(event); .setPath(path)
.setMessage(line)
.build();
fileFollowEventManagerService.publish(event);
}
} }
} }
} }

View file

@ -3,7 +3,7 @@ package org.xbib.event.path;
import org.xbib.datastructures.api.TimeValue; import org.xbib.datastructures.api.TimeValue;
import org.xbib.datastructures.json.tiny.Json; import org.xbib.datastructures.json.tiny.Json;
import org.xbib.event.Event; import org.xbib.event.Event;
import org.xbib.event.bus.EventBus; import org.xbib.event.common.AbstractEventManagerService;
import org.xbib.event.common.EventManager; import org.xbib.event.common.EventManager;
import org.xbib.event.common.EventManagerService; import org.xbib.event.common.EventManagerService;
import org.xbib.settings.Settings; import org.xbib.settings.Settings;
@ -14,7 +14,6 @@ import java.io.Writer;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -24,28 +23,26 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Stream; import java.util.stream.Stream;
public class PathEventManagerService implements EventManagerService { public class PathEventManagerService extends AbstractEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(PathEventManagerService.class.getName()); private static final Logger logger = Logger.getLogger(PathEventManagerService.class.getName());
private EventBus eventBus; private EventManager eventManager;
private ExecutorService executorService; private ExecutorService executorService;
private Map<Future<?>, PathEventService> eventServiceMap; private Map<Future<?>, PathEventService> eventServiceMap;
private List<String> suspendedQueues;
public PathEventManagerService() { public PathEventManagerService() {
super();
} }
@Override @Override
public PathEventManagerService init(EventManager eventManager) { public PathEventManagerService init(EventManager eventManager) {
this.eventManager = eventManager;
Settings settings = eventManager.getSettings(); Settings settings = eventManager.getSettings();
this.eventBus = eventManager.getEventBus();
this.executorService = eventManager.getExecutorService(); this.executorService = eventManager.getExecutorService();
this.eventServiceMap = new LinkedHashMap<>(); this.eventServiceMap = new LinkedHashMap<>();
this.suspendedQueues = new ArrayList<>();
for (Map.Entry<String, Settings> entry : settings.getGroups("event.path").entrySet()) { for (Map.Entry<String, Settings> entry : settings.getGroups("event.path").entrySet()) {
try { try {
String name = entry.getKey(); String name = entry.getKey();
@ -54,9 +51,12 @@ public class PathEventManagerService implements EventManagerService {
Path path = Paths.get(definition.get("path", "/var/tmp/" + name)); Path path = Paths.get(definition.get("path", "/var/tmp/" + name));
TimeValue lifetime = definition.getAsTime("lifetime", TimeValue.timeValueHours(72)); TimeValue lifetime = definition.getAsTime("lifetime", TimeValue.timeValueHours(72));
String eventType = definition.get("type", "path"); String eventType = definition.get("type", "path");
createPathEventService(name, path, eventType, lifetime); createQueue(name, path);
PathEventService pathEventService = new PathEventService(this, name, path, eventType, lifetime);
add(pathEventService);
pathEventService.drainIncoming();
} else { } else {
logger.log(Level.WARNING, "path servive definition not enabled in configuration"); logger.log(Level.WARNING, "path service definition not enabled in configuration");
} }
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "unable to create event path service " + entry.getKey() + ", reason " + e.getMessage(), e); logger.log(Level.SEVERE, "unable to create event path service " + entry.getKey() + ", reason " + e.getMessage(), e);
@ -65,21 +65,29 @@ public class PathEventManagerService implements EventManagerService {
return this; return this;
} }
public void createPathEventService(String name, Path path, String eventType, TimeValue lifetime) throws IOException {
createQueue(name, path);
PathEventService pathEventService = new PathEventService(this, eventBus, name, path, eventType, lifetime);
add(pathEventService);
}
public void add(PathEventService pathEventService) { public void add(PathEventService pathEventService) {
Future<?> future = executorService.submit(pathEventService); Future<?> future = executorService.submit(pathEventService);
eventServiceMap.put(future, pathEventService); eventServiceMap.put(future, pathEventService);
logger.log(Level.INFO, "path event service " + pathEventService + " added"); logger.log(Level.INFO, "path event service " + pathEventService + " added");
} }
public void publish(String eventType, Path path) {
Event event = EventManager.eventOf(eventType, null, null, path);
eventManager.publish(event);
}
public void failEvent(String eventType, Path path) {
try {
Event event = EventManager.eventOf(eventType, null, null, path);
event.fail();
} catch (IOException e) {
logger.log(Level.SEVERE, "unable to fail event because of " + e.getMessage(), e);
}
}
@Override @Override
public void shutdown() throws IOException { public void shutdown() throws IOException {
logger.log(Level.INFO, "shut down all path event services"); logger.log(Level.INFO, "shutting down all path event services");
eventServiceMap.forEach((k, v) -> { eventServiceMap.forEach((k, v) -> {
k.cancel(true); k.cancel(true);
try { try {
@ -103,30 +111,18 @@ public class PathEventManagerService implements EventManagerService {
}); });
} }
public List<String> getSuspendedQueues() {
return suspendedQueues;
}
public void suspend(String queue) {
suspendedQueues.add(queue);
}
public void resume(String queue) {
suspendedQueues.remove(queue);
}
public boolean put(Path path, String key, Map<String,Object> map) throws IOException {
return put(path, key, ".json", Json.toString(map));
}
public boolean putIfNotExists(Path path, String key, Map<String,Object> map) throws IOException { public boolean putIfNotExists(Path path, String key, Map<String,Object> map) throws IOException {
if (!exists(path, key, ".json")) { if (!exists(path, key, ".json")) {
return put(path, key, ".json", Json.toString(map)); return put(path, key, map);
} else { } else {
return false; return false;
} }
} }
public boolean put(Path path, String key, Map<String,Object> map) throws IOException {
return put(path, key, ".json", Json.toString(map));
}
public boolean put(Path path, String key, String suffix, String string) throws IOException { public boolean put(Path path, String key, String suffix, String string) throws IOException {
String keyFileName = key + suffix; String keyFileName = key + suffix;
if (Files.exists(path.resolve(Event.INCOMING).resolve(keyFileName)) || if (Files.exists(path.resolve(Event.INCOMING).resolve(keyFileName)) ||

View file

@ -4,8 +4,6 @@ import java.nio.file.FileVisitResult;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import org.xbib.datastructures.api.TimeValue; import org.xbib.datastructures.api.TimeValue;
import org.xbib.event.Event; import org.xbib.event.Event;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManager;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
@ -25,14 +23,12 @@ import java.util.concurrent.Callable;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public class PathEventService implements Callable<Integer>, Closeable { public class PathEventService implements Callable<Boolean>, Closeable {
private static final Logger logger = Logger.getLogger(PathEventService.class.getName()); private static final Logger logger = Logger.getLogger(PathEventService.class.getName());
private final PathEventManagerService pathEventManager; private final PathEventManagerService pathEventManager;
private final EventBus eventBus;
private final Path path; private final Path path;
private final String name; private final String name;
@ -43,18 +39,14 @@ public class PathEventService implements Callable<Integer>, Closeable {
private final WatchService watchService; private final WatchService watchService;
private int eventCount;
private volatile boolean keepWatching; private volatile boolean keepWatching;
public PathEventService(PathEventManagerService pathEventManager, public PathEventService(PathEventManagerService pathEventManager,
EventBus eventBus,
String name, String name,
Path path, Path path,
String eventType, String eventType,
TimeValue lifetime) throws IOException { TimeValue lifetime) throws IOException {
this.pathEventManager = pathEventManager; this.pathEventManager = pathEventManager;
this.eventBus = eventBus;
this.name = name; this.name = name;
this.path = path; this.path = path;
this.eventType = eventType; this.eventType = eventType;
@ -69,7 +61,7 @@ public class PathEventService implements Callable<Integer>, Closeable {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public Integer call() { public Boolean call() {
try { try {
logger.log(Level.INFO, "watch service running on " + path.resolve(Event.INCOMING)); logger.log(Level.INFO, "watch service running on " + path.resolve(Event.INCOMING));
while (keepWatching && watchService != null) { while (keepWatching && watchService != null) {
@ -86,10 +78,10 @@ public class PathEventService implements Callable<Integer>, Closeable {
String watchEventContext = pathWatchEvent.context().toString(); String watchEventContext = pathWatchEvent.context().toString();
Path p = path.resolve(Event.INCOMING).resolve(watchEventContext); Path p = path.resolve(Event.INCOMING).resolve(watchEventContext);
logger.log(Level.FINE, "watch event " + pathWatchEvent + " context = " + watchEventContext + " path = " + p); logger.log(Level.FINE, "watch event " + pathWatchEvent + " context = " + watchEventContext + " path = " + p);
if (pathEventManager.getSuspendedQueues().contains(name)) { if (pathEventManager.getSuspended().contains(name)) {
failEvent(p); pathEventManager.failEvent(eventType, p);
} else { } else {
postEvent(p); pathEventManager.publish(eventType, p);
} }
} }
watchKey.reset(); watchKey.reset();
@ -102,7 +94,7 @@ public class PathEventService implements Callable<Integer>, Closeable {
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e); logger.log(Level.SEVERE, e.getMessage(), e);
} }
return eventCount; return true;
} }
@Override @Override
@ -120,7 +112,7 @@ public class PathEventService implements Callable<Integer>, Closeable {
public void drainIncoming() throws IOException { public void drainIncoming() throws IOException {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path.resolve(Event.INCOMING))) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(path.resolve(Event.INCOMING))) {
stream.forEach(this::postEvent); stream.forEach(p -> pathEventManager.publish(eventType, p));
} }
} }
@ -149,21 +141,6 @@ public class PathEventService implements Callable<Integer>, Closeable {
} }
} }
private void postEvent(Path path) {
Event event = EventManager.eventOf(eventType, null, null, path);
eventBus.post(event);
eventCount++;
}
private void failEvent(Path file) {
try {
Event event = EventManager.eventFromFile(file);
event.fail();
} catch (IOException e) {
logger.log(Level.SEVERE, "unable to fail event because of " + e.getMessage(), e);
}
}
private static void delete(Path path) { private static void delete(Path path) {
if (path == null) { if (path == null) {
return; return;

View file

@ -1,7 +1,8 @@
package org.xbib.event.timer; package org.xbib.event.timer;
import org.xbib.event.Event;
import org.xbib.event.Payload; import org.xbib.event.Payload;
import org.xbib.event.bus.EventBus; import org.xbib.event.common.AbstractEventManagerService;
import org.xbib.event.common.EventManager; import org.xbib.event.common.EventManager;
import org.xbib.event.common.EventManagerService; import org.xbib.event.common.EventManagerService;
import org.xbib.event.persistence.FilePersistenceStore; import org.xbib.event.persistence.FilePersistenceStore;
@ -20,26 +21,29 @@ import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public class TimerEventManagerService implements EventManagerService { public class TimerEventManagerService extends AbstractEventManagerService implements EventManagerService {
private static final Logger logger = Logger.getLogger(TimerEventManagerService.class.getName()); private static final Logger logger = Logger.getLogger(TimerEventManagerService.class.getName());
private EventManager eventManager;
private Map<String, TimerEventService> services; private Map<String, TimerEventService> services;
public TimerEventManagerService() { public TimerEventManagerService() {
super();
} }
@Override @Override
public TimerEventManagerService init(EventManager eventManager) { public TimerEventManagerService init(EventManager eventManager) {
this.eventManager = eventManager;
Settings settings = eventManager.getSettings(); Settings settings = eventManager.getSettings();
EventBus eventBus = eventManager.getEventBus();
this.services = new LinkedHashMap<>(); this.services = new LinkedHashMap<>();
for (Map.Entry<String, Settings> entry : settings.getGroups("event.timer").entrySet()) { for (Map.Entry<String, Settings> entry : settings.getGroups("event.timer").entrySet()) {
String name = entry.getKey(); String name = entry.getKey();
Settings timerSettings = entry.getValue(); Settings timerSettings = entry.getValue();
try { try {
PersistenceStore<String, Object> persistenceStore = new FilePersistenceStore(timerSettings, name); PersistenceStore<String, Object> persistenceStore = new FilePersistenceStore(timerSettings, name);
TimerEventService timerEventService = new TimerEventService(eventBus, name, ZoneId.systemDefault(), persistenceStore); TimerEventService timerEventService = new TimerEventService(this, name, ZoneId.systemDefault(), persistenceStore);
services.put(name, timerEventService); services.put(name, timerEventService);
logger.log(Level.INFO, "timer " + name + " active: " + timerEventService); logger.log(Level.INFO, "timer " + name + " active: " + timerEventService);
} catch (Exception e) { } catch (Exception e) {
@ -49,9 +53,21 @@ public class TimerEventManagerService implements EventManagerService {
return this; return this;
} }
public boolean publish(String name, @Override
String timeSpec, public void shutdown() throws IOException {
Payload payload) throws ParseException, IOException { for (Map.Entry<String, TimerEventService> entry : services.entrySet()) {
logger.log(Level.INFO, "closing timer " + entry.getKey());
entry.getValue().close();
}
}
public void publish(Event event) {
eventManager.publish(event);
}
public boolean schedule(String name,
String timeSpec,
Payload payload) throws ParseException, IOException {
if (services.containsKey(name)) { if (services.containsKey(name)) {
Span span = Chronic.parse(timeSpec); Span span = Chronic.parse(timeSpec);
if (span != null) { if (span != null) {
@ -69,9 +85,9 @@ public class TimerEventManagerService implements EventManagerService {
return false; return false;
} }
public boolean publish(String service, public boolean schedule(String service,
Instant instant, Instant instant,
Payload payload) throws IOException { Payload payload) throws IOException {
if (services.containsKey(service)) { if (services.containsKey(service)) {
services.get(service).schedule(instant, payload); services.get(service).schedule(instant, payload);
return true; return true;
@ -87,12 +103,4 @@ public class TimerEventManagerService implements EventManagerService {
entry.getValue().purge(); entry.getValue().purge();
} }
} }
@Override
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,7 +2,6 @@ package org.xbib.event.timer;
import org.xbib.event.Event; import org.xbib.event.Event;
import org.xbib.event.Payload; import org.xbib.event.Payload;
import org.xbib.event.bus.EventBus;
import org.xbib.event.common.EventManager; import org.xbib.event.common.EventManager;
import org.xbib.event.persistence.PersistenceStore; import org.xbib.event.persistence.PersistenceStore;
@ -25,8 +24,10 @@ import java.util.logging.Logger;
class TimerEventService implements Closeable { class TimerEventService implements Closeable {
private static final Logger logger = Logger.getLogger(TimerEventService.class.getName()); private static final Logger logger = Logger.getLogger(TimerEventService.class.getName());
private final EventBus eventBus; private final TimerEventManagerService timerEventManagerService;
private final String name;
private final ZoneId zoneId; private final ZoneId zoneId;
@ -34,11 +35,12 @@ class TimerEventService implements Closeable {
private final Timer timer; private final Timer timer;
public TimerEventService(EventBus eventBus, public TimerEventService(TimerEventManagerService timerEventManagerService,
String name, String name,
ZoneId zoneId, ZoneId zoneId,
PersistenceStore<String, Object> persistenceStore) throws IOException { PersistenceStore<String, Object> persistenceStore) throws IOException {
this.eventBus = eventBus; this.timerEventManagerService = timerEventManagerService;
this.name = name;
this.zoneId = zoneId; this.zoneId = zoneId;
this.persistenceStore = persistenceStore; this.persistenceStore = persistenceStore;
this.timer = new Timer(); this.timer = new Timer();
@ -97,12 +99,16 @@ class TimerEventService implements Closeable {
@Override @Override
public void run() { public void run() {
try { try {
if (timerEventManagerService.getSuspended().contains(name)) {
logger.log(Level.FINE, "timer event " + name + " suspended");
return;
}
Event timerEvent = EventManager.eventBuilder() Event timerEvent = EventManager.eventBuilder()
.setType("timer") .setType("timer")
.setCode(name)
.setPayload(payload) .setPayload(payload)
.build(); .build();
logger.log(Level.FINE, "posting timer event " + timerEvent.getClass().getName() + " with payload = " + payload); timerEventManagerService.publish(timerEvent);
eventBus.post(timerEvent);
logger.log(Level.FINE, "persistence before remove: " + persistenceStore.get("tasks")); logger.log(Level.FINE, "persistence before remove: " + persistenceStore.get("tasks"));
if (persistenceStore.remove("tasks", this.payload)) { if (persistenceStore.remove("tasks", this.payload)) {
logger.log(Level.FINE, "removal done"); logger.log(Level.FINE, "removal done");

View file

@ -20,6 +20,6 @@ public class ClockEventManagerTest {
.register(clockEventConsumer) .register(clockEventConsumer)
.build(); .build();
Thread.sleep(90000L); Thread.sleep(90000L);
eventManager.close(); eventManager.shutdown();
} }
} }

View file

@ -1,11 +1,9 @@
package org.xbib.event.generic; package org.xbib.event.common;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xbib.event.Event; import org.xbib.event.Event;
import org.xbib.event.EventConsumer; import org.xbib.event.EventConsumer;
import org.xbib.event.bus.Subscribe; import org.xbib.event.bus.Subscribe;
import org.xbib.event.common.EventManager;
import org.xbib.event.common.GenericEventImpl;
import org.xbib.settings.Settings; import org.xbib.settings.Settings;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -32,7 +30,7 @@ public class GenericEventManagerTest {
.setType("generic") .setType("generic")
.setListener(e -> logger.log(Level.INFO, "received event " + e)) .setListener(e -> logger.log(Level.INFO, "received event " + e))
.build(); .build();
eventManager.getGenericEventManagerService().post(event); eventManager.publish(event);
// we must wait for a certain time because we do not use a future // we must wait for a certain time because we do not use a future
Thread.sleep(500L); Thread.sleep(500L);
assertEquals(1, consumer.getCount()); assertEquals(1, consumer.getCount());
@ -52,7 +50,7 @@ public class GenericEventManagerTest {
future.complete(e); future.complete(e);
}) })
.build(); .build();
eventManager.getGenericEventManagerService().post(event); eventManager.publish(event);
Event e = future.get(1000L, TimeUnit.MILLISECONDS); Event e = future.get(1000L, TimeUnit.MILLISECONDS);
assertNotNull(e); assertNotNull(e);
} }
@ -76,7 +74,7 @@ public class GenericEventManagerTest {
future.complete(e); future.complete(e);
}) })
.build(); .build();
eventManager.getGenericEventManagerService().post((GenericEventImpl) event, future); eventManager.publish((GenericEventImpl) event, future);
Event e = future.get(5L, TimeUnit.SECONDS); Event e = future.get(5L, TimeUnit.SECONDS);
if (e != null) { if (e != null) {
logger.log(Level.INFO, "the event " + e + " was received by all consumers"); logger.log(Level.INFO, "the event " + e + " was received by all consumers");

View file

@ -37,6 +37,6 @@ public class FileFollowEventManagerTest {
Thread.sleep(1000L); Thread.sleep(1000L);
Files.delete(testTxt); Files.delete(testTxt);
Files.delete(path); Files.delete(path);
eventManager.close(); eventManager.shutdown();
} }
} }

View file

@ -34,7 +34,8 @@ public class PathEventManagerTest {
bufferedWriter.write("Hello"); bufferedWriter.write("Hello");
} }
Thread.sleep(2000L); Thread.sleep(2000L);
eventManager.close(); eventManager.shutdown();
// extra destroy to clean up test
eventManager.getPathEventManagerService().destroy(); eventManager.getPathEventManagerService().destroy();
} }
@ -57,7 +58,8 @@ public class PathEventManagerTest {
bufferedWriter.write("Hello"); bufferedWriter.write("Hello");
} }
Thread.sleep(2000L); Thread.sleep(2000L);
eventManager.close(); eventManager.shutdown();
// extra destroy to clean up test
eventManager.getPathEventManagerService().destroy(); eventManager.getPathEventManagerService().destroy();
} }

View file

@ -23,7 +23,7 @@ public class TimerEventManagerTest {
.build(); .build();
TimerEventManagerService timerEventManager = eventManager.getTimerEventManagerService(); TimerEventManagerService timerEventManager = eventManager.getTimerEventManagerService();
Payload payload = new Payload(Map.of("a", "b")); Payload payload = new Payload(Map.of("a", "b"));
timerEventManager.publish("testtimerevent", Instant.now().plusSeconds(5L), payload); timerEventManager.schedule("testtimerevent", Instant.now().plusSeconds(5L), payload);
Thread.sleep(10000L); Thread.sleep(10000L);
timerEventManager.shutdown(); timerEventManager.shutdown();
} }

View file

@ -24,14 +24,14 @@ public class HttpEventReceiverService {
public HttpService createService(String prefix) { public HttpService createService(String prefix) {
return BaseHttpService.builder() return BaseHttpService.builder()
.setPrefix(prefix) .setPrefix(prefix)
.setPath("/event/{type}") .setPath("/event")
.setMethod(HttpMethod.POST) .setMethod(HttpMethod.POST)
.setHandler(ctx -> { .setHandler(ctx -> {
Event event = EventManager.eventFromJson(ctx.getRequest().asJson()); Event event = EventManager.eventFromJson(ctx.getRequest().asJson());
if (event.isNullEvent()) { if (event.isNullEvent()) {
ctx.status(NOT_FOUND).done(); ctx.status(NOT_FOUND).done();
} else { } else {
eventManager.dispatch(event); eventManager.publish(event);
ctx.status(OK) ctx.status(OK)
.header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON) .header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON)
.charset(StandardCharsets.UTF_8) .charset(StandardCharsets.UTF_8)