From b96466515c77a168a2d3ee1f8f7e7f9136d0c86c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Wed, 24 Jan 2024 17:03:17 +0100 Subject: [PATCH] register event types --- event-common/src/main/java/module-info.java | 1 + .../xbib/event/clock/ClockEventService.java | 4 +- .../org/xbib/event/common/ClockEventImpl.java | 4 +- .../org/xbib/event/common/EventBuilder.java | 111 ------- .../java/org/xbib/event/common/EventImpl.java | 125 -------- .../org/xbib/event/common/EventManager.java | 287 ++++++++++++++++-- .../event/common/FileFollowEventImpl.java | 4 +- .../xbib/event/common/GenericEventImpl.java | 8 +- .../java/org/xbib/event/common/NullEvent.java | 9 + .../org/xbib/event/common/PathEventImpl.java | 4 +- .../org/xbib/event/common/TimerEventImpl.java | 4 +- .../event/path/FileFollowEventService.java | 4 +- .../org/xbib/event/path/PathEventService.java | 6 +- .../event/timer/TimerEventManagerService.java | 13 +- .../xbib/event/timer/TimerEventService.java | 4 +- .../org/xbib/event/clock/TestClockEvent.java | 10 - .../event/clock/TestClockEventConsumer.java | 6 +- .../generic/GenericEventManagerTest.java | 7 +- .../xbib/event/path/TestFileFollowEvent.java | 10 - .../path/TestFileFollowEventConsumer.java | 5 +- .../org/xbib/event/timer/TestTimerEvent.java | 11 - .../event/timer/TestTimerEventConsumer.java | 5 +- .../event/timer/TimerEventManagerTest.java | 2 +- .../net/http/HttpEventReceiverService.java | 5 +- 24 files changed, 322 insertions(+), 327 deletions(-) delete mode 100644 event-common/src/main/java/org/xbib/event/common/EventBuilder.java delete mode 100644 event-common/src/main/java/org/xbib/event/common/EventImpl.java create mode 100644 event-common/src/main/java/org/xbib/event/common/NullEvent.java delete mode 100644 event-common/src/test/java/org/xbib/event/clock/TestClockEvent.java delete mode 100644 event-common/src/test/java/org/xbib/event/path/TestFileFollowEvent.java delete mode 100644 event-common/src/test/java/org/xbib/event/timer/TestTimerEvent.java diff --git a/event-common/src/main/java/module-info.java b/event-common/src/main/java/module-info.java index 35f6182..6eb55a4 100644 --- a/event-common/src/main/java/module-info.java +++ b/event-common/src/main/java/module-info.java @@ -12,6 +12,7 @@ module org.xbib.event.common { exports org.xbib.event.wal; uses EventManagerService; uses org.xbib.event.EventConsumer; + uses org.xbib.event.Event; requires org.xbib.event.api; requires org.xbib.settings.api; requires org.xbib.settings.datastructures.json; diff --git a/event-common/src/main/java/org/xbib/event/clock/ClockEventService.java b/event-common/src/main/java/org/xbib/event/clock/ClockEventService.java index d845fbf..836d214 100644 --- a/event-common/src/main/java/org/xbib/event/clock/ClockEventService.java +++ b/event-common/src/main/java/org/xbib/event/clock/ClockEventService.java @@ -6,7 +6,7 @@ import java.util.logging.Logger; import org.xbib.event.Event; import org.xbib.event.bus.EventBus; -import org.xbib.event.common.EventImpl; +import org.xbib.event.common.EventManager; public class ClockEventService implements Callable { @@ -33,7 +33,7 @@ public class ClockEventService implements Callable { logger.log(Level.FINE, "clock event " + name + " suspended"); return 1; } else { - Event clockEvent = EventImpl.builder() + Event clockEvent = EventManager.eventBuilder() .setType("clock") .build(); eventBus.post(clockEvent); diff --git a/event-common/src/main/java/org/xbib/event/common/ClockEventImpl.java b/event-common/src/main/java/org/xbib/event/common/ClockEventImpl.java index 18bf24f..eba9836 100644 --- a/event-common/src/main/java/org/xbib/event/common/ClockEventImpl.java +++ b/event-common/src/main/java/org/xbib/event/common/ClockEventImpl.java @@ -2,9 +2,9 @@ package org.xbib.event.common; import org.xbib.event.ClockEvent; -public class ClockEventImpl extends EventImpl implements ClockEvent { +public class ClockEventImpl extends EventManager.EventImpl implements ClockEvent { - public ClockEventImpl(EventBuilder builder) { + public ClockEventImpl(EventManager.EventBuilder builder) { super(builder); } } diff --git a/event-common/src/main/java/org/xbib/event/common/EventBuilder.java b/event-common/src/main/java/org/xbib/event/common/EventBuilder.java deleted file mode 100644 index 0727459..0000000 --- a/event-common/src/main/java/org/xbib/event/common/EventBuilder.java +++ /dev/null @@ -1,111 +0,0 @@ -package org.xbib.event.common; - -import org.xbib.event.Event; -import org.xbib.event.Listener; -import org.xbib.event.Payload; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.time.Instant; - -public class EventBuilder { - - Listener listener; - - String type; - - String code; - - String message; - - Instant scheduled; - - Instant created; - - Payload payload; - - Path path; - - String base; - - String suffix; - - long fileSize; - - long maxFileSize; - - EventBuilder() { - this.maxFileSize = -1L; - } - - public EventBuilder setListener(Listener listener) { - this.listener = listener; - return this; - } - - public EventBuilder setType(String type) { - this.type = type; - return this; - } - - public EventBuilder setCode(String code) { - this.code = code; - return this; - } - - public EventBuilder setMessage(String message) { - this.message = message; - return this; - } - - public EventBuilder setScheduledFor(Instant scheduled) { - this.scheduled = scheduled; - return this; - } - - public EventBuilder setPayload(Payload payload) { - this.payload = payload; - return this; - } - - public EventBuilder setMaxFileSize(long maxFileSize) { - this.maxFileSize = maxFileSize; - return this; - } - - public EventBuilder setPath(Path path) throws IOException { - this.path = path; - base = getBase(path); - suffix = getSuffix(path); - fileSize = Files.size(path); - if (maxFileSize != -1L && fileSize > maxFileSize) { - throw new IOException("file size too large"); - } - return this; - } - - public Event build() { - this.created = Instant.now(); - return switch (type) { - case "clock" -> new ClockEventImpl(this); - case "timer" -> new TimerEventImpl(this); - case "path" -> new PathEventImpl(this); - case "filefollow" -> new FileFollowEventImpl(this); - case "generic" -> new GenericEventImpl(this, listener); - default -> new EventImpl(this); - }; - } - - private static String getBase(Path path) { - String name = path.getFileName().toString(); - int pos = name.lastIndexOf('.'); - return pos >= 0 ? name.substring(0, pos) : name; - } - - private static String getSuffix(Path path) { - String name = path.getFileName().toString(); - int pos = name.lastIndexOf('.'); - return pos >= 0 ? name.substring(pos + 1) : null; - } -} diff --git a/event-common/src/main/java/org/xbib/event/common/EventImpl.java b/event-common/src/main/java/org/xbib/event/common/EventImpl.java deleted file mode 100644 index 96895ea..0000000 --- a/event-common/src/main/java/org/xbib/event/common/EventImpl.java +++ /dev/null @@ -1,125 +0,0 @@ -package org.xbib.event.common; - -import org.xbib.datastructures.json.tiny.Json; -import org.xbib.event.Event; -import org.xbib.event.Listener; -import org.xbib.event.Payload; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.nio.file.attribute.FileTime; -import java.time.Instant; -import java.util.Map; - -public class EventImpl implements Event { - - private final EventBuilder builder; - - EventImpl(EventBuilder builder) { - this.builder = builder; - } - - public static EventBuilder builder() { - return new EventBuilder(); - } - - public Listener getListener() { - return builder.listener; - } - - @Override - public String getType() { - return builder.type; - } - - @Override - public String getCode() { - return builder.code; - } - - @Override - public String getMessage() { - return builder.message; - } - - @Override - public Payload getPayload() { - return builder.payload; - } - - @Override - public Instant getCreated() { - return builder.created; - } - - @Override - public Instant getScheduledFor() { - return builder.scheduled; - } - - @Override - public Path getPath() { - return builder.path; - } - - @Override - public String getBase() { - return builder.base; - } - - @Override - public String getSuffix() { - return builder.suffix; - } - - @Override - public long getFileSize() { - return builder.fileSize; - } - - - public static Event fromFile(Path file) throws IOException { - return fromJson(Files.readString(file)); - } - - public static Event fromJson(String json) { - Map map = Json.toMap(json); - return builder() - .setType(map.getOrDefault("type", "generic").toString()) - .setPayload(new Payload(map)) - .build(); - } - - public String toJson() throws IOException { - return Json.toString(builder.payload); - } - - @Override - public boolean isNullEvent() { - return builder.type == null; - } - - @Override - public String toString() { - return "Event[path = " + builder.path + ", base = " + builder.base + ", suffix = " + builder.suffix + " payload = " + builder.payload +"]"; - } - - public void success() throws IOException { - if (builder.path != null) { - Files.setLastModifiedTime(builder.path, FileTime.from(Instant.now())); - Files.move(builder.path, builder.path.getParent().resolve(Event.SUCCESS) - .resolve(builder.path.getFileName()).toAbsolutePath(), - StandardCopyOption.REPLACE_EXISTING); - - } - } - - public void fail() throws IOException { - Files.setLastModifiedTime(builder.path, FileTime.from(Instant.now())); - Files.move(builder.path, builder.path.getParent().resolve(Event.FAIL) - .resolve(builder.path.getFileName()).toAbsolutePath(), - StandardCopyOption.REPLACE_EXISTING); - } -} diff --git a/event-common/src/main/java/org/xbib/event/common/EventManager.java b/event-common/src/main/java/org/xbib/event/common/EventManager.java index 3c88271..cb86178 100644 --- a/event-common/src/main/java/org/xbib/event/common/EventManager.java +++ b/event-common/src/main/java/org/xbib/event/common/EventManager.java @@ -1,11 +1,21 @@ package org.xbib.event.common; +import java.lang.reflect.InvocationTargetException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.FileTime; +import java.time.Instant; import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; +import org.xbib.datastructures.json.tiny.Json; import org.xbib.event.Event; import org.xbib.event.EventConsumer; +import org.xbib.event.FileFollowEvent; +import org.xbib.event.Listener; +import org.xbib.event.Payload; import org.xbib.event.bus.AsyncEventBus; import org.xbib.event.bus.EventBus; import org.xbib.event.bus.SubscriberExceptionContext; @@ -31,12 +41,24 @@ public final class EventManager { private static final Logger logger = Logger.getLogger(EventManager.class.getName()); - private final Builder builder; + private final EventManagerBuilder builder; + + private static final Map> eventTypes = new HashMap<>(); private final Map, EventManagerService> eventManagerServices; - private EventManager(Builder builder) { + private EventManager(EventManagerBuilder builder) { this.builder = builder; + eventTypes.put("null", NullEvent.class); + eventTypes.put("generic", GenericEventImpl.class); + eventTypes.put("path", PathEventImpl.class); + eventTypes.put("clock", ClockEventImpl.class); + eventTypes.put("timer", TimerEventImpl.class); + eventTypes.put("filefollow", FileFollowEventImpl.class); + for (Event event : ServiceLoader.load(Event.class)) { + eventTypes.put(event.getType(), event.getClass()); + } + logger.log(Level.INFO, "installed events = " + eventTypes.keySet()); this.eventManagerServices = new HashMap<>(); eventManagerServices.put(GenericEventManagerService.class, new GenericEventManagerService().init(this)); eventManagerServices.put(ClockEventManagerService.class, new ClockEventManagerService().init(this)); @@ -49,6 +71,14 @@ public final class EventManager { logger.log(Level.INFO, "installed event service managers = " + eventManagerServices.keySet()); } + public static EventManagerBuilder builder() { + return new EventManagerBuilder(); + } + + public static EventBuilder eventBuilder() { + return new EventBuilder(); + } + public Settings getSettings() { return builder.settings; } @@ -65,11 +95,7 @@ public final class EventManager { return builder.executorService; } - public static Builder builder() { - return new Builder(); - } - - public void submit(Event event) { + public void dispatch(Event event) { getGenericEventManagerService().post(event); } @@ -113,7 +139,7 @@ public final class EventManager { } } - public static class Builder { + public static class EventManagerBuilder { private Settings settings; @@ -129,48 +155,53 @@ public final class EventManager { private final List eventConsumers; - private Builder() { + private EventManagerBuilder() { this.eventConsumers = new ArrayList<>(); this.subscriberExceptionHandler = new EventManagerExceptionHandler(); } - public Builder setSettings(Settings settings) { + public EventManagerBuilder setSettings(Settings settings) { this.settings = settings; return this; } - public Builder setThreadCount(int threadCount) { + public EventManagerBuilder setThreadCount(int threadCount) { this.threadcount = threadCount; return this; } - public Builder setExecutorService(ExecutorService executorService) { + public EventManagerBuilder setExecutorService(ExecutorService executorService) { this.executorService = executorService; return this; } - public Builder setClassLoader(ClassLoader classLoader) { + public EventManagerBuilder setClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; return this; } - public Builder setEventBus(EventBus eventBus) { + public EventManagerBuilder setEventBus(EventBus eventBus) { this.eventBus = eventBus; return this; } - public Builder register(EventConsumer eventConsumer) { + public EventManagerBuilder register(String eventType, Class eventClass) { + eventTypes.put(eventType, eventClass); + return this; + } + + public EventManagerBuilder register(EventConsumer eventConsumer) { Objects.requireNonNull(eventConsumer, "event consumer must not be null"); eventConsumers.add(eventConsumer); return this; } - public Builder setSubscriberExceptionHandler(SubscriberExceptionHandler subscriberExceptionHandler) { + public EventManagerBuilder setSubscriberExceptionHandler(SubscriberExceptionHandler subscriberExceptionHandler) { this.subscriberExceptionHandler = subscriberExceptionHandler; return this; } - public Builder loadEventConsumers() { + public EventManagerBuilder loadEventConsumers() { if (classLoader == null) { classLoader = EventManager.class.getClassLoader(); } @@ -210,4 +241,226 @@ public final class EventManager { logger.log(Level.SEVERE, exception.getMessage(), exception); } } + + public static Event eventFromFile(Path file) throws IOException { + return eventFromJson(Files.readString(file)); + } + + public static Event eventFromJson(String json) { + Map map = Json.toMap(json); + return eventBuilder() + .setType(map.getOrDefault("type", "generic").toString()) + .setPayload(new Payload(map)) + .build(); + } + + public static class EventImpl implements Event { + + private final EventBuilder builder; + + EventImpl(EventBuilder builder) { + this.builder = builder; + } + + public Listener getListener() { + return builder.listener; + } + + @Override + public String getType() { + return builder.type; + } + + @Override + public String getCode() { + return builder.code; + } + + @Override + public String getMessage() { + return builder.message; + } + + @Override + public Payload getPayload() { + return builder.payload; + } + + @Override + public Instant getCreated() { + return builder.created; + } + + @Override + public Instant getScheduledFor() { + return builder.scheduled; + } + + @Override + public Path getPath() { + return builder.path; + } + + @Override + public String getBase() { + return builder.base; + } + + @Override + public String getSuffix() { + return builder.suffix; + } + + @Override + public long getFileSize() { + return builder.fileSize; + } + + public String toJson() throws IOException { + return Json.toString(builder.payload); + } + + @Override + public boolean isNullEvent() { + return builder.type == null; + } + + @Override + public String toString() { + return "Event[path = " + builder.path + ", base = " + builder.base + ", suffix = " + builder.suffix + " payload = " + builder.payload +"]"; + } + + public void success() throws IOException { + if (builder.path != null) { + Files.setLastModifiedTime(builder.path, FileTime.from(Instant.now())); + Files.move(builder.path, builder.path.getParent().resolve(Event.SUCCESS) + .resolve(builder.path.getFileName()).toAbsolutePath(), + StandardCopyOption.REPLACE_EXISTING); + + } + } + + public void fail() throws IOException { + Files.setLastModifiedTime(builder.path, FileTime.from(Instant.now())); + Files.move(builder.path, builder.path.getParent().resolve(Event.FAIL) + .resolve(builder.path.getFileName()).toAbsolutePath(), + StandardCopyOption.REPLACE_EXISTING); + } + } + + public static class EventBuilder { + + Listener listener; + + String type; + + String code; + + String message; + + Instant scheduled; + + Instant created; + + Payload payload; + + Path path; + + String base; + + String suffix; + + long fileSize; + + long maxFileSize; + + EventBuilder() { + this.maxFileSize = -1L; + } + + public EventBuilder setListener(Listener listener) { + this.listener = listener; + return this; + } + + public EventBuilder setType(String type) { + if (type != null && eventTypes.containsKey(type)) { + this.type = type; + } else { + throw new IllegalArgumentException("unregistered event type: " + type); + } + return this; + } + + public EventBuilder setCode(String code) { + this.code = code; + return this; + } + + public EventBuilder setMessage(String message) { + this.message = message; + return this; + } + + public EventBuilder setScheduledFor(Instant scheduled) { + this.scheduled = scheduled; + return this; + } + + public EventBuilder setPayload(Payload payload) { + this.payload = payload; + return this; + } + + public EventBuilder setMaxFileSize(long maxFileSize) { + this.maxFileSize = maxFileSize; + return this; + } + + public EventBuilder setPath(Path path) throws IOException { + this.path = path; + base = getBase(path); + suffix = getSuffix(path); + fileSize = Files.size(path); + if (maxFileSize != -1L && fileSize > maxFileSize) { + throw new IOException("file size too large"); + } + return this; + } + + public Event build() { + if (type == null) { + type = "null"; + } + Class cl = eventTypes.get(type); + if (cl == null) { + cl = NullEvent.class; + } + try { + this.created = Instant.now(); + if (listener != null) { + return cl.getDeclaredConstructor(EventBuilder.class, Listener.class).newInstance(this, listener); + } else { + return cl.getDeclaredConstructor(EventBuilder.class).newInstance(this); + } + } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | + InvocationTargetException e) { + logger.log(Level.WARNING, "unable to construct event object for type " + type + ", falling back to NullEvent"); + return new NullEvent(this); + } + } + + private static String getBase(Path path) { + String name = path.getFileName().toString(); + int pos = name.lastIndexOf('.'); + return pos >= 0 ? name.substring(0, pos) : name; + } + + private static String getSuffix(Path path) { + String name = path.getFileName().toString(); + int pos = name.lastIndexOf('.'); + return pos >= 0 ? name.substring(pos + 1) : null; + } + } + } diff --git a/event-common/src/main/java/org/xbib/event/common/FileFollowEventImpl.java b/event-common/src/main/java/org/xbib/event/common/FileFollowEventImpl.java index 6708681..d77d0d1 100644 --- a/event-common/src/main/java/org/xbib/event/common/FileFollowEventImpl.java +++ b/event-common/src/main/java/org/xbib/event/common/FileFollowEventImpl.java @@ -2,9 +2,9 @@ package org.xbib.event.common; import org.xbib.event.FileFollowEvent; -public class FileFollowEventImpl extends EventImpl implements FileFollowEvent { +public class FileFollowEventImpl extends EventManager.EventImpl implements FileFollowEvent { - public FileFollowEventImpl(EventBuilder builder) { + public FileFollowEventImpl(EventManager.EventBuilder builder) { super(builder); } } diff --git a/event-common/src/main/java/org/xbib/event/common/GenericEventImpl.java b/event-common/src/main/java/org/xbib/event/common/GenericEventImpl.java index 0b36727..68f119c 100644 --- a/event-common/src/main/java/org/xbib/event/common/GenericEventImpl.java +++ b/event-common/src/main/java/org/xbib/event/common/GenericEventImpl.java @@ -4,15 +4,15 @@ import org.xbib.event.Listener; import java.util.Objects; -public class GenericEventImpl extends EventImpl { +public class GenericEventImpl extends EventManager.EventImpl { - private final EventBuilder builder; + private final EventManager.EventBuilder builder; - public GenericEventImpl(EventBuilder builder) { + public GenericEventImpl(EventManager.EventBuilder builder) { this(builder, null); } - public GenericEventImpl(EventBuilder builder, Listener listener) { + public GenericEventImpl(EventManager.EventBuilder builder, Listener listener) { super(builder); this.builder = builder; this.builder.listener = Objects.requireNonNull(listener); diff --git a/event-common/src/main/java/org/xbib/event/common/NullEvent.java b/event-common/src/main/java/org/xbib/event/common/NullEvent.java new file mode 100644 index 0000000..d501b90 --- /dev/null +++ b/event-common/src/main/java/org/xbib/event/common/NullEvent.java @@ -0,0 +1,9 @@ +package org.xbib.event.common; + +import org.xbib.event.Event; + +public class NullEvent extends EventManager.EventImpl implements Event { + NullEvent(EventManager.EventBuilder builder) { + super(builder); + } +} diff --git a/event-common/src/main/java/org/xbib/event/common/PathEventImpl.java b/event-common/src/main/java/org/xbib/event/common/PathEventImpl.java index 88c5c4b..932b97a 100644 --- a/event-common/src/main/java/org/xbib/event/common/PathEventImpl.java +++ b/event-common/src/main/java/org/xbib/event/common/PathEventImpl.java @@ -2,9 +2,9 @@ package org.xbib.event.common; import org.xbib.event.PathEvent; -public class PathEventImpl extends EventImpl implements PathEvent { +public class PathEventImpl extends EventManager.EventImpl implements PathEvent { - public PathEventImpl(EventBuilder builder) { + public PathEventImpl(EventManager.EventBuilder builder) { super(builder); } } diff --git a/event-common/src/main/java/org/xbib/event/common/TimerEventImpl.java b/event-common/src/main/java/org/xbib/event/common/TimerEventImpl.java index 30954da..844beb8 100644 --- a/event-common/src/main/java/org/xbib/event/common/TimerEventImpl.java +++ b/event-common/src/main/java/org/xbib/event/common/TimerEventImpl.java @@ -2,9 +2,9 @@ package org.xbib.event.common; import org.xbib.event.TimerEvent; -public class TimerEventImpl extends EventImpl implements TimerEvent { +public class TimerEventImpl extends EventManager.EventImpl implements TimerEvent { - public TimerEventImpl(EventBuilder builder) { + public TimerEventImpl(EventManager.EventBuilder builder) { super(builder); } } diff --git a/event-common/src/main/java/org/xbib/event/path/FileFollowEventService.java b/event-common/src/main/java/org/xbib/event/path/FileFollowEventService.java index e28f541..dcd88c1 100644 --- a/event-common/src/main/java/org/xbib/event/path/FileFollowEventService.java +++ b/event-common/src/main/java/org/xbib/event/path/FileFollowEventService.java @@ -2,7 +2,7 @@ package org.xbib.event.path; import org.xbib.event.Event; import org.xbib.event.bus.EventBus; -import org.xbib.event.common.EventImpl; +import org.xbib.event.common.EventManager; import org.xbib.settings.Settings; import java.io.Closeable; @@ -89,7 +89,7 @@ public class FileFollowEventService implements Callable, Closeable { String content = readRange(channel, lastSize, currentSize); // split content by line, this allows pattern matching without preprocessing in worker for (String line : content.split("\n")) { - Event event = EventImpl.builder() + Event event = EventManager.eventBuilder() .setType("filefollow") .setCode(base.toString()) .setPath(path) diff --git a/event-common/src/main/java/org/xbib/event/path/PathEventService.java b/event-common/src/main/java/org/xbib/event/path/PathEventService.java index e612383..00528ed 100644 --- a/event-common/src/main/java/org/xbib/event/path/PathEventService.java +++ b/event-common/src/main/java/org/xbib/event/path/PathEventService.java @@ -3,7 +3,7 @@ package org.xbib.event.path; import org.xbib.datastructures.api.TimeValue; import org.xbib.event.Event; import org.xbib.event.bus.EventBus; -import org.xbib.event.common.EventImpl; +import org.xbib.event.common.EventManager; import java.io.Closeable; import java.io.IOException; @@ -147,7 +147,7 @@ public class PathEventService implements Callable, Closeable { private void postEvent(Path file) { try { - Event event = EventImpl.fromFile(file); + Event event = EventManager.eventFromFile(file); eventBus.post(event); eventCount++; } catch (IOException e) { @@ -157,7 +157,7 @@ public class PathEventService implements Callable, Closeable { private void failEvent(Path file) { try { - Event event = EventImpl.fromFile(file); + Event event = EventManager.eventFromFile(file); event.fail(); } catch (IOException e) { logger.log(Level.SEVERE, "unable to fail event because of " + e.getMessage()); diff --git a/event-common/src/main/java/org/xbib/event/timer/TimerEventManagerService.java b/event-common/src/main/java/org/xbib/event/timer/TimerEventManagerService.java index b5dd10f..509b89a 100644 --- a/event-common/src/main/java/org/xbib/event/timer/TimerEventManagerService.java +++ b/event-common/src/main/java/org/xbib/event/timer/TimerEventManagerService.java @@ -34,7 +34,6 @@ public class TimerEventManagerService implements EventManagerService { public TimerEventManagerService init(EventManager eventManager) { Settings settings = eventManager.getSettings(); EventBus eventBus = eventManager.getEventBus(); - ClassLoader classLoader = eventManager.getClassLoader(); this.services = new LinkedHashMap<>(); for (Map.Entry entry : settings.getGroups("event.timer").entrySet()) { String name = entry.getKey(); @@ -51,9 +50,9 @@ public class TimerEventManagerService implements EventManagerService { return this; } - public boolean put(String service, - String timeSpec, - Payload payload) throws ParseException, IOException { + public boolean publish(String service, + String timeSpec, + Payload payload) throws ParseException, IOException { if (services.containsKey(service)) { Span span = Chronic.parse(timeSpec); if (span != null) { @@ -71,9 +70,9 @@ public class TimerEventManagerService implements EventManagerService { return false; } - public boolean put(String service, - Instant instant, - Payload payload) throws IOException { + public boolean publish(String service, + Instant instant, + Payload payload) throws IOException { if (services.containsKey(service)) { services.get(service).schedule(instant, payload); return true; diff --git a/event-common/src/main/java/org/xbib/event/timer/TimerEventService.java b/event-common/src/main/java/org/xbib/event/timer/TimerEventService.java index 9be77a3..a979c06 100644 --- a/event-common/src/main/java/org/xbib/event/timer/TimerEventService.java +++ b/event-common/src/main/java/org/xbib/event/timer/TimerEventService.java @@ -3,7 +3,7 @@ package org.xbib.event.timer; import org.xbib.event.Event; import org.xbib.event.Payload; import org.xbib.event.bus.EventBus; -import org.xbib.event.common.EventImpl; +import org.xbib.event.common.EventManager; import org.xbib.event.persistence.PersistenceStore; import java.io.Closeable; @@ -97,7 +97,7 @@ class TimerEventService implements Closeable { @Override public void run() { try { - Event timerEvent = EventImpl.builder() + Event timerEvent = EventManager.eventBuilder() .setType("timer") .setPayload(payload) .build(); diff --git a/event-common/src/test/java/org/xbib/event/clock/TestClockEvent.java b/event-common/src/test/java/org/xbib/event/clock/TestClockEvent.java deleted file mode 100644 index fe5e7fc..0000000 --- a/event-common/src/test/java/org/xbib/event/clock/TestClockEvent.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.xbib.event.clock; - -import org.xbib.event.common.ClockEventImpl; -import org.xbib.event.common.EventBuilder; - -public class TestClockEvent extends ClockEventImpl { - public TestClockEvent(EventBuilder builder) { - super(builder); - } -} diff --git a/event-common/src/test/java/org/xbib/event/clock/TestClockEventConsumer.java b/event-common/src/test/java/org/xbib/event/clock/TestClockEventConsumer.java index 76fd882..dcceb46 100644 --- a/event-common/src/test/java/org/xbib/event/clock/TestClockEventConsumer.java +++ b/event-common/src/test/java/org/xbib/event/clock/TestClockEventConsumer.java @@ -1,10 +1,10 @@ package org.xbib.event.clock; +import org.xbib.event.ClockEvent; import org.xbib.event.EventConsumer; import org.xbib.event.bus.AllowConcurrentEvents; import org.xbib.event.bus.Subscribe; -import java.io.IOException; import java.time.Instant; import java.util.logging.Level; import java.util.logging.Logger; @@ -15,7 +15,7 @@ public class TestClockEventConsumer implements EventConsumer { @Subscribe @AllowConcurrentEvents - void onEvent(TestClockEvent event) { - logger.log(Level.INFO, "received test clock event on " + Instant.now() + " event instant = " + event.getCreated()); + void onEvent(ClockEvent event) { + logger.log(Level.INFO, "received clock event on " + Instant.now() + " event instant = " + event.getCreated()); } } diff --git a/event-common/src/test/java/org/xbib/event/generic/GenericEventManagerTest.java b/event-common/src/test/java/org/xbib/event/generic/GenericEventManagerTest.java index 15e9820..872138e 100644 --- a/event-common/src/test/java/org/xbib/event/generic/GenericEventManagerTest.java +++ b/event-common/src/test/java/org/xbib/event/generic/GenericEventManagerTest.java @@ -4,7 +4,6 @@ import org.junit.jupiter.api.Test; import org.xbib.event.Event; import org.xbib.event.EventConsumer; import org.xbib.event.bus.Subscribe; -import org.xbib.event.common.EventImpl; import org.xbib.event.common.EventManager; import org.xbib.event.common.GenericEventImpl; import org.xbib.settings.Settings; @@ -29,7 +28,7 @@ public class GenericEventManagerTest { .setSettings(settings) .register(consumer) .build(); - Event event = EventImpl.builder() + Event event = EventManager.eventBuilder() .setType("generic") .setListener(e -> logger.log(Level.INFO, "received event " + e)) .build(); @@ -47,7 +46,7 @@ public class GenericEventManagerTest { .register(consumer) .build(); CompletableFuture future = new CompletableFuture<>(); - Event event = EventImpl.builder() + Event event = EventManager.eventBuilder() .setType("generic") .setListener(e -> { logger.log(Level.INFO, "received event " + e); @@ -72,7 +71,7 @@ public class GenericEventManagerTest { .loadEventConsumers() .build(); CompletableFuture future = new CompletableFuture<>(); - Event event = GenericEventImpl.builder() + Event event = EventManager.eventBuilder() .setType("generic") .setListener(e -> { logger.log(Level.INFO, "received event " + e); diff --git a/event-common/src/test/java/org/xbib/event/path/TestFileFollowEvent.java b/event-common/src/test/java/org/xbib/event/path/TestFileFollowEvent.java deleted file mode 100644 index 71e5e1c..0000000 --- a/event-common/src/test/java/org/xbib/event/path/TestFileFollowEvent.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.xbib.event.path; - -import org.xbib.event.common.EventBuilder; -import org.xbib.event.common.FileFollowEventImpl; - -public class TestFileFollowEvent extends FileFollowEventImpl { - public TestFileFollowEvent(EventBuilder builder) { - super(builder); - } -} diff --git a/event-common/src/test/java/org/xbib/event/path/TestFileFollowEventConsumer.java b/event-common/src/test/java/org/xbib/event/path/TestFileFollowEventConsumer.java index d84300d..6017293 100644 --- a/event-common/src/test/java/org/xbib/event/path/TestFileFollowEventConsumer.java +++ b/event-common/src/test/java/org/xbib/event/path/TestFileFollowEventConsumer.java @@ -1,6 +1,7 @@ package org.xbib.event.path; import org.xbib.event.EventConsumer; +import org.xbib.event.FileFollowEvent; import org.xbib.event.bus.AllowConcurrentEvents; import org.xbib.event.bus.Subscribe; @@ -13,7 +14,7 @@ public class TestFileFollowEventConsumer implements EventConsumer { @Subscribe @AllowConcurrentEvents - void onEvent(TestFileFollowEvent event) { - logger.log(Level.INFO, "received filefollow event path = " + event.getPath() + " content = " + event.getMessage()); + void onEvent(FileFollowEvent event) { + logger.log(Level.INFO, "received filefollow event, path = " + event.getPath() + " content = " + event.getMessage()); } } diff --git a/event-common/src/test/java/org/xbib/event/timer/TestTimerEvent.java b/event-common/src/test/java/org/xbib/event/timer/TestTimerEvent.java deleted file mode 100644 index 8b894ca..0000000 --- a/event-common/src/test/java/org/xbib/event/timer/TestTimerEvent.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.xbib.event.timer; - -import org.xbib.event.common.EventBuilder; -import org.xbib.event.common.TimerEventImpl; - -public class TestTimerEvent extends TimerEventImpl { - - public TestTimerEvent(EventBuilder builder) { - super(builder); - } -} diff --git a/event-common/src/test/java/org/xbib/event/timer/TestTimerEventConsumer.java b/event-common/src/test/java/org/xbib/event/timer/TestTimerEventConsumer.java index afaee4d..f05c929 100644 --- a/event-common/src/test/java/org/xbib/event/timer/TestTimerEventConsumer.java +++ b/event-common/src/test/java/org/xbib/event/timer/TestTimerEventConsumer.java @@ -1,6 +1,7 @@ package org.xbib.event.timer; import org.xbib.event.EventConsumer; +import org.xbib.event.TimerEvent; import org.xbib.event.bus.AllowConcurrentEvents; import org.xbib.event.bus.Subscribe; @@ -14,8 +15,8 @@ public class TestTimerEventConsumer implements EventConsumer { @Subscribe @AllowConcurrentEvents - void onEvent(TestTimerEvent event) { - logger.log(Level.INFO, "received test timer event on " + Instant.now() + " event instant = " + event.getCreated()); + void onEvent(TimerEvent event) { + logger.log(Level.INFO, "received timer event on " + Instant.now() + " event instant = " + event.getCreated()); } } diff --git a/event-common/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java b/event-common/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java index 2304543..6e12683 100644 --- a/event-common/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java +++ b/event-common/src/test/java/org/xbib/event/timer/TimerEventManagerTest.java @@ -23,7 +23,7 @@ public class TimerEventManagerTest { .build(); TimerEventManagerService timerEventManager = eventManager.getTimerEventManagerService(); Payload payload = new Payload(Map.of("a", "b")); - timerEventManager.put("testtimerevent", Instant.now().plusSeconds(5L), payload); + timerEventManager.publish("testtimerevent", Instant.now().plusSeconds(5L), payload); Thread.sleep(10000L); timerEventManager.shutdown(); } diff --git a/event-net-http/src/main/java/org/xbib/event/net/http/HttpEventReceiverService.java b/event-net-http/src/main/java/org/xbib/event/net/http/HttpEventReceiverService.java index f3ecf0c..d18804a 100644 --- a/event-net-http/src/main/java/org/xbib/event/net/http/HttpEventReceiverService.java +++ b/event-net-http/src/main/java/org/xbib/event/net/http/HttpEventReceiverService.java @@ -2,7 +2,6 @@ package org.xbib.event.net.http; import org.xbib.event.Event; import org.xbib.event.common.EventManager; -import org.xbib.event.common.EventImpl; import org.xbib.net.http.HttpHeaderNames; import org.xbib.net.http.HttpHeaderValues; import org.xbib.net.http.HttpMethod; @@ -28,11 +27,11 @@ public class HttpEventReceiverService { .setPath("/event/{type}") .setMethod(HttpMethod.POST) .setHandler(ctx -> { - Event event = EventImpl.fromJson(ctx.getRequest().asJson()); + Event event = EventManager.eventFromJson(ctx.getRequest().asJson()); if (event.isNullEvent()) { ctx.status(NOT_FOUND).done(); } else { - eventManager.submit(event); + eventManager.dispatch(event); ctx.status(OK) .header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON) .charset(StandardCharsets.UTF_8)