From 6689342c241ab3fd4670fe981b62d58a16ad7950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Fri, 22 Mar 2024 16:25:41 +0100 Subject: [PATCH] add clock event type --- .../src/main/java/org/xbib/event/Event.java | 4 +- .../event/clock/ClockEventManagerService.java | 11 +++-- .../xbib/event/clock/ClockEventService.java | 12 +++-- .../event/clock/SimpleClockEventConsumer.java | 23 --------- .../org/xbib/event/common/EventManager.java | 47 +++++++------------ .../event/clock/ClockEventManagerTest.java | 2 + .../org/xbib/event/clock/TestClockEvent.java | 11 +++++ .../event/clock/TestClockEventConsumer.java | 6 +-- .../event/timer/TestTimerEventConsumer.java | 2 +- 9 files changed, 51 insertions(+), 67 deletions(-) delete mode 100644 event-common/src/main/java/org/xbib/event/clock/SimpleClockEventConsumer.java create mode 100644 event-common/src/test/java/org/xbib/event/clock/TestClockEvent.java diff --git a/event-api/src/main/java/org/xbib/event/Event.java b/event-api/src/main/java/org/xbib/event/Event.java index 2f15388..4bfbe18 100644 --- a/event-api/src/main/java/org/xbib/event/Event.java +++ b/event-api/src/main/java/org/xbib/event/Event.java @@ -16,9 +16,7 @@ public interface Event { String getMessage(); - Instant getCreated(); - - Instant getScheduledFor(); + Instant getInstant(); Payload getPayload(); diff --git a/event-common/src/main/java/org/xbib/event/clock/ClockEventManagerService.java b/event-common/src/main/java/org/xbib/event/clock/ClockEventManagerService.java index cd43433..5b3f6a2 100644 --- a/event-common/src/main/java/org/xbib/event/clock/ClockEventManagerService.java +++ b/event-common/src/main/java/org/xbib/event/clock/ClockEventManagerService.java @@ -46,17 +46,20 @@ public class ClockEventManagerService extends AbstractEventManagerService implem Settings entrySettings = mapEntry.getValue(); if (entrySettings.getAsBoolean("enabled", true)) { String entry = entrySettings.get("entry"); - if (entry != null) { + String type = entrySettings.get("type", "clock"); + if (entry != null && type != null) { try { - ClockEventService clockEventService = new ClockEventService(this, name); + ClockEventService clockEventService = new ClockEventService(this, name, type); cronSchedule.add(name, CronExpression.parse(entry), clockEventService); - logger.log(Level.INFO, "cron job " + name + " scheduled on " + entry); + logger.log(Level.INFO, "cron job " + name + "with type " + type + " scheduled on " + entry); } catch (Exception e) { logger.log(Level.WARNING, "unable to schedule cron job " + mapEntry.getKey() + ", reason " + e.getMessage()); } + } else { + logger.log(Level.WARNING, "clock event service in configuration is incompletely defined, name = " + name ); } } else { - logger.log(Level.WARNING, "clock event service " + name + " in configuration not enabled"); + logger.log(Level.WARNING, "clock event service in configuration not enabled, name = " + name ); } } logger.log(Level.INFO, "entries = " + cronSchedule.getEntries()); 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 735357c..2d6d740 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 @@ -1,5 +1,6 @@ package org.xbib.event.clock; +import java.util.Objects; import java.util.concurrent.Callable; import java.util.logging.Level; import java.util.logging.Logger; @@ -15,21 +16,26 @@ public class ClockEventService implements Callable { private final String name; + private final String eventType; + public ClockEventService(ClockEventManagerService clockEventManagerService, - String name) { + String name, + String eventType) { this.clockEventManagerService = clockEventManagerService; this.name = name; + this.eventType = Objects.requireNonNull(eventType, "clock event type must not be null"); } @Override public Boolean call() { try { if (clockEventManagerService.getSuspended().contains(name)) { - logger.log(Level.FINE, "clock event " + name + " suspended"); + logger.log(Level.FINE, "clock event service " + name + " is suspended, unable to continue"); return false; } else { Event clockEvent = EventManager.eventBuilder() - .setType("clock") + .setType(eventType) + .build(); clockEventManagerService.publish(clockEvent); return true; diff --git a/event-common/src/main/java/org/xbib/event/clock/SimpleClockEventConsumer.java b/event-common/src/main/java/org/xbib/event/clock/SimpleClockEventConsumer.java deleted file mode 100644 index 3eabd12..0000000 --- a/event-common/src/main/java/org/xbib/event/clock/SimpleClockEventConsumer.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.xbib.event.clock; - -import org.xbib.event.ClockEvent; -import org.xbib.event.EventConsumer; - -import java.util.logging.Logger; -import org.xbib.event.bus.AllowConcurrentEvents; -import org.xbib.event.bus.Subscribe; - -public class SimpleClockEventConsumer implements EventConsumer { - - private static final Logger logger = Logger.getLogger(SimpleClockEventConsumer.class.getName()); - - public SimpleClockEventConsumer() { - } - - @Subscribe - @AllowConcurrentEvents - void onEvent(ClockEvent event) { - logger.info("received demo clock event, created = " + event.getCreated()); - } - -} 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 1639edc..9cabd16 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 @@ -96,10 +96,10 @@ public final class EventManager extends AbstractEventManagerService implements E } public static Event eventOf(String eventType, - Instant scheduled) { + Instant instant) { return eventBuilder() .setType(eventType) - .setScheduledFor(scheduled) + .setInstant(instant) .build(); } @@ -120,13 +120,9 @@ public final class EventManager extends AbstractEventManagerService implements E if (map.containsKey("message")) { builder.setMessage(map.getOrDefault("message", "").toString()); } - if (map.containsKey("created")) { - String created = map.getOrDefault("created", "").toString(); - builder.setCreated(Instant.parse(created)); - } - if (map.containsKey("scheduled")) { - String scheduled = map.getOrDefault("scheduled", "").toString(); - builder.setCreated(Instant.parse(scheduled)); + if (map.containsKey("instant")) { + String instant = map.getOrDefault("instant", "").toString(); + builder.setInstant(Instant.parse(instant)); } if (map.containsKey("payload")) { PayloadImpl payload = new PayloadImpl((Map) map.get("payload")); @@ -383,13 +379,8 @@ public final class EventManager extends AbstractEventManagerService implements E } @Override - public Instant getCreated() { - return builder.created; - } - - @Override - public Instant getScheduledFor() { - return builder.scheduled; + public Instant getInstant() { + return builder.instant; } @Override @@ -422,9 +413,12 @@ public final class EventManager extends AbstractEventManagerService implements E if (getPayload() != null && !getPayload().isEmpty()) { 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); + if (getInstant() != null) { + builder.fieldIfNotNull("instant", getInstant().toString()); + } + if (getPath() != null) { + builder.fieldIfNotNull("path", getPath().toAbsolutePath().toString()); + } builder.endMap(); return builder.build(); } @@ -482,9 +476,7 @@ public final class EventManager extends AbstractEventManagerService implements E String message; - Instant created; - - Instant scheduled; + Instant instant; PayloadImpl payload; @@ -526,13 +518,8 @@ public final class EventManager extends AbstractEventManagerService implements E return this; } - public EventBuilder setCreated(Instant created) { - this.created = created; - return this; - } - - public EventBuilder setScheduledFor(Instant scheduled) { - this.scheduled = scheduled; + public EventBuilder setInstant(Instant instant) { + this.instant = instant; return this; } @@ -574,7 +561,7 @@ public final class EventManager extends AbstractEventManagerService implements E cl = NullEvent.class; } try { - this.created = Instant.now(); + this.instant = Instant.now(); if (listener != null) { return cl.getDeclaredConstructor(EventBuilder.class, Listener.class).newInstance(this, listener); } else { diff --git a/event-common/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java b/event-common/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java index 2e39ec5..ed5a057 100644 --- a/event-common/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java +++ b/event-common/src/test/java/org/xbib/event/clock/ClockEventManagerTest.java @@ -13,10 +13,12 @@ public class ClockEventManagerTest { TestClockEventConsumer clockEventConsumer = new TestClockEventConsumer(); Settings settings = Settings.settingsBuilder() .put("event.clock.testclockevent.enabled", "true") + .put("event.clock.testclockevent.type", "test-clock-event") .put("event.clock.testclockevent.entry", "*/1 6-21 * * *") .build(); EventManager eventManager = EventManager.builder() .setSettings(settings) + .register("test-clock-event", TestClockEvent.class) .register(clockEventConsumer) .build(); Thread.sleep(90000L); 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 new file mode 100644 index 0000000..e28a810 --- /dev/null +++ b/event-common/src/test/java/org/xbib/event/clock/TestClockEvent.java @@ -0,0 +1,11 @@ +package org.xbib.event.clock; + +import org.xbib.event.common.ClockEventImpl; +import org.xbib.event.common.EventManager; + +public class TestClockEvent extends ClockEventImpl { + + public TestClockEvent(EventManager.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 dcceb46..cbb29ea 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,6 +1,5 @@ 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; @@ -15,7 +14,8 @@ public class TestClockEventConsumer implements EventConsumer { @Subscribe @AllowConcurrentEvents - void onEvent(ClockEvent event) { - logger.log(Level.INFO, "received clock event on " + Instant.now() + " event instant = " + event.getCreated()); + void onEvent(TestClockEvent event) { + logger.log(Level.INFO, "received test clock event on " + Instant.now() + + " event instant = " + event.getInstant()); } } 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 f05c929..2f82c6a 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 @@ -16,7 +16,7 @@ public class TestTimerEventConsumer implements EventConsumer { @Subscribe @AllowConcurrentEvents void onEvent(TimerEvent event) { - logger.log(Level.INFO, "received timer event on " + Instant.now() + " event instant = " + event.getCreated()); + logger.log(Level.INFO, "received timer event on " + Instant.now() + " event instant = " + event.getInstant()); } }