add clock event type
This commit is contained in:
parent
0b78c32b91
commit
6689342c24
9 changed files with 51 additions and 67 deletions
|
@ -16,9 +16,7 @@ public interface Event {
|
|||
|
||||
String getMessage();
|
||||
|
||||
Instant getCreated();
|
||||
|
||||
Instant getScheduledFor();
|
||||
Instant getInstant();
|
||||
|
||||
Payload getPayload();
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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<Boolean> {
|
|||
|
||||
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;
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String, Object>) 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 {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue