add event listener and future completion to generic event manager

This commit is contained in:
Jörg Prante 2023-12-11 11:38:35 +01:00
parent 3145720f40
commit ef0a26bbd1
42 changed files with 1424 additions and 1253 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib group = org.xbib
name = event name = event
version = 0.0.7 version = 0.0.8
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

Binary file not shown.

View file

@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip
networkTimeout=10000 networkTimeout=10000
validateDistributionUrl=true validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME

View file

@ -15,11 +15,10 @@ pluginManagement {
dependencyResolutionManagement { dependencyResolutionManagement {
versionCatalogs { versionCatalogs {
libs { libs {
version('gradle', '8.4') version('gradle', '8.5')
version('groovy', '4.0.13')
version('datastructures', '5.0.5') version('datastructures', '5.0.5')
version('netty', '4.1.100.Final') version('netty', '4.1.101.Final')
version('net', '4.0.0') version('net', '4.0.2')
library('netty-handler', 'io.netty', 'netty-handler').versionRef('netty') library('netty-handler', 'io.netty', 'netty-handler').versionRef('netty')
library('net', 'org.xbib', 'net').versionRef('net') library('net', 'org.xbib', 'net').versionRef('net')
library('datastructures-common', 'org.xbib', 'datastructures-common').versionRef('datastructures') library('datastructures-common', 'org.xbib', 'datastructures-common').versionRef('datastructures')

View file

@ -34,6 +34,7 @@ public abstract class CompleteFuture<V> extends AbstractFuture<V> {
return this; return this;
} }
@SuppressWarnings("unchecked")
@Override @Override
public Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners) { public Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners) {
for (GenericFutureListener<? extends Future<? super V>> l: for (GenericFutureListener<? extends Future<? super V>> l:
@ -53,6 +54,7 @@ public abstract class CompleteFuture<V> extends AbstractFuture<V> {
return this; return this;
} }
@SuppressWarnings("unchecked")
@Override @Override
public Future<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners) { public Future<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners) {
// NOOP // NOOP

View file

@ -9,8 +9,7 @@ final class DefaultFutureListeners {
private int progressiveSize; // the number of progressive listeners private int progressiveSize; // the number of progressive listeners
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
DefaultFutureListeners( DefaultFutureListeners(GenericFutureListener<? extends Future<?>> first, GenericFutureListener<? extends Future<?>> second) {
GenericFutureListener<? extends Future<?>> first, GenericFutureListener<? extends Future<?>> second) {
listeners = new GenericFutureListener[2]; listeners = new GenericFutureListener[2];
listeners[0] = first; listeners[0] = first;
listeners[1] = second; listeners[1] = second;

View file

@ -60,6 +60,7 @@ public class DefaultProgressivePromise<V> extends DefaultPromise<V> implements P
return this; return this;
} }
@SuppressWarnings("unchecked")
@Override @Override
public ProgressivePromise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners) { public ProgressivePromise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners) {
super.addListeners(listeners); super.addListeners(listeners);
@ -72,6 +73,7 @@ public class DefaultProgressivePromise<V> extends DefaultPromise<V> implements P
return this; return this;
} }
@SuppressWarnings("unchecked")
@Override @Override
public ProgressivePromise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners) { public ProgressivePromise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners) {
super.removeListeners(listeners); super.removeListeners(listeners);

View file

@ -170,6 +170,7 @@ public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V> {
return this; return this;
} }
@SuppressWarnings("unchecked")
@Override @Override
public Promise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners) { public Promise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners) {
Objects.requireNonNull(listeners, "listeners"); Objects.requireNonNull(listeners, "listeners");

View file

@ -1,6 +1,4 @@
package org.xbib.event; package org.xbib.event;
import java.io.Closeable; public interface EventConsumer {
public interface EventConsumer extends Closeable {
} }

View file

@ -23,7 +23,7 @@ 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 implements Closeable { public final class EventManager {
private static final Logger logger = Logger.getLogger(EventManager.class.getName()); private static final Logger logger = Logger.getLogger(EventManager.class.getName());
@ -79,17 +79,12 @@ public final class EventManager implements Closeable {
return syslogEventManager; return syslogEventManager;
} }
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public void close() throws IOException { public void close() throws IOException {
for (EventConsumer eventConsumer : builder.eventConsumers) { for (EventConsumer eventConsumer : builder.eventConsumers) {
eventConsumer.close(); if (eventConsumer instanceof Closeable closeable) {
closeable.close();
}
} }
genericEventManager.close();
clockEventManager.close(); clockEventManager.close();
timerEventManager.close(); timerEventManager.close();
fileFollowEventManager.close(); fileFollowEventManager.close();

View file

@ -11,10 +11,12 @@ public interface Future<V> extends java.util.concurrent.Future<V> {
Future<V> addListener(GenericFutureListener<? extends Future<? super V>> var1); Future<V> addListener(GenericFutureListener<? extends Future<? super V>> var1);
@SuppressWarnings("unchecked")
Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... var1); Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... var1);
Future<V> removeListener(GenericFutureListener<? extends Future<? super V>> var1); Future<V> removeListener(GenericFutureListener<? extends Future<? super V>> var1);
@SuppressWarnings("unchecked")
Future<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... var1); Future<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... var1);
Future<V> sync() throws InterruptedException; Future<V> sync() throws InterruptedException;

View file

@ -8,12 +8,14 @@ public interface ProgressiveFuture<V> extends Future<V> {
@Override @Override
ProgressiveFuture<V> addListener(GenericFutureListener<? extends Future<? super V>> listener); ProgressiveFuture<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);
@SuppressWarnings("unchecked")
@Override @Override
ProgressiveFuture<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners); ProgressiveFuture<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
@Override @Override
ProgressiveFuture<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener); ProgressiveFuture<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);
@SuppressWarnings("unchecked")
@Override @Override
ProgressiveFuture<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners); ProgressiveFuture<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners);

View file

@ -27,12 +27,14 @@ public interface ProgressivePromise<V> extends Promise<V>, ProgressiveFuture<V>
@Override @Override
ProgressivePromise<V> addListener(GenericFutureListener<? extends Future<? super V>> listener); ProgressivePromise<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);
@SuppressWarnings("unchecked")
@Override @Override
ProgressivePromise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners); ProgressivePromise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
@Override @Override
ProgressivePromise<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener); ProgressivePromise<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);
@SuppressWarnings("unchecked")
@Override @Override
ProgressivePromise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners); ProgressivePromise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners);

View file

@ -13,10 +13,12 @@ public interface Promise<V> extends Future<V> {
Promise<V> addListener(GenericFutureListener<? extends Future<? super V>> value); Promise<V> addListener(GenericFutureListener<? extends Future<? super V>> value);
@SuppressWarnings("unchecked")
Promise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... value); Promise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... value);
Promise<V> removeListener(GenericFutureListener<? extends Future<? super V>> value); Promise<V> removeListener(GenericFutureListener<? extends Future<? super V>> value);
@SuppressWarnings("unchecked")
Promise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... value); Promise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... value);
Promise<V> await() throws InterruptedException; Promise<V> await() throws InterruptedException;

View file

@ -59,7 +59,7 @@ public interface CompositeFuture extends Future<CompositeFuture> {
* *
* When the list is empty, the returned future will be already completed. * When the list is empty, the returned future will be already completed.
*/ */
static CompositeFuture all(List<Future> futures) { static CompositeFuture all(List<Future<?>> futures) {
return CompositeFutureImpl.all(futures.toArray(new Future[0])); return CompositeFutureImpl.all(futures.toArray(new Future[0]));
} }
@ -109,7 +109,7 @@ public interface CompositeFuture extends Future<CompositeFuture> {
* *
* When the list is empty, the returned future will be already completed. * When the list is empty, the returned future will be already completed.
*/ */
static CompositeFuture any(List<Future> futures) { static CompositeFuture any(List<Future<?>> futures) {
return CompositeFutureImpl.any(futures.toArray(new Future[0])); return CompositeFutureImpl.any(futures.toArray(new Future[0]));
} }
@ -159,7 +159,7 @@ public interface CompositeFuture extends Future<CompositeFuture> {
* *
* When the list is empty, the returned future will be already completed. * When the list is empty, the returned future will be already completed.
*/ */
static CompositeFuture join(List<Future> futures) { static CompositeFuture join(List<Future<?>> futures) {
return CompositeFutureImpl.join(futures.toArray(new Future[0])); return CompositeFutureImpl.join(futures.toArray(new Future[0]));
} }

View file

@ -36,6 +36,7 @@ public interface Future<T> extends AsyncResult<T> {
* @param <T> the result type * @param <T> the result type
* @return the future * @return the future
*/ */
@SuppressWarnings("unchecked")
static <T> Future<T> succeededFuture() { static <T> Future<T> succeededFuture() {
return (Future<T>) SucceededFuture.EMPTY; return (Future<T>) SucceededFuture.EMPTY;
} }

View file

@ -100,7 +100,7 @@ public class CompositeFutureImpl extends FutureImpl<CompositeFuture> implements
return composite; return composite;
} }
private final Future[] results; private final Future<?>[] results;
private int count; private int count;
private CompositeFutureImpl(Future<?>... results) { private CompositeFutureImpl(Future<?>... results) {
@ -132,6 +132,7 @@ public class CompositeFutureImpl extends FutureImpl<CompositeFuture> implements
return this.<T>future(index).result(); return this.<T>future(index).result();
} }
@SuppressWarnings("unchecked")
private <T> Future<T> future(int index) { private <T> Future<T> future(int index) {
if (index < 0 || index >= results.length) { if (index < 0 || index >= results.length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
@ -148,10 +149,6 @@ public class CompositeFutureImpl extends FutureImpl<CompositeFuture> implements
tryComplete(this); tryComplete(this);
} }
private void fail(Throwable t) {
complete(t);
}
private void complete(Object result) { private void complete(Object result) {
if (result == this) { if (result == this) {
tryComplete(this); tryComplete(this);

View file

@ -53,6 +53,7 @@ public final class FailedFuture<T> extends FutureBase<T> {
return true; return true;
} }
@SuppressWarnings("unchecked")
@Override @Override
public Future<T> onComplete(Handler<AsyncResult<T>> handler) { public Future<T> onComplete(Handler<AsyncResult<T>> handler) {
if (handler instanceof Listener) { if (handler instanceof Listener) {
@ -105,11 +106,13 @@ public final class FailedFuture<T> extends FutureBase<T> {
return true; return true;
} }
@SuppressWarnings("unchecked")
@Override @Override
public <U> Future<U> map(Function<T, U> mapper) { public <U> Future<U> map(Function<T, U> mapper) {
return (Future<U>) this; return (Future<U>) this;
} }
@SuppressWarnings("unchecked")
@Override @Override
public <V> Future<V> map(V value) { public <V> Future<V> map(V value) {
return (Future<V>) this; return (Future<V>) this;

View file

@ -36,6 +36,7 @@ class FutureImpl<T> extends FutureBase<T> {
/** /**
* The result of the operation. This will be null if the operation failed. * The result of the operation. This will be null if the operation failed.
*/ */
@SuppressWarnings("unchecked")
public synchronized T result() { public synchronized T result() {
return value instanceof CauseHolder ? null : value == NULL_VALUE ? null : (T) value; return value instanceof CauseHolder ? null : value == NULL_VALUE ? null : (T) value;
} }
@ -114,6 +115,7 @@ class FutureImpl<T> extends FutureBase<T> {
return this; return this;
} }
@SuppressWarnings("unchecked")
@Override @Override
public Future<T> onComplete(Handler<AsyncResult<T>> handler) { public Future<T> onComplete(Handler<AsyncResult<T>> handler) {
Objects.requireNonNull(handler, "No null handler accepted"); Objects.requireNonNull(handler, "No null handler accepted");
@ -152,6 +154,7 @@ class FutureImpl<T> extends FutureBase<T> {
return this; return this;
} }
@SuppressWarnings("unchecked")
@Override @Override
public void addListener(Listener<T> listener) { public void addListener(Listener<T> listener) {
Object v; Object v;
@ -242,6 +245,7 @@ class FutureImpl<T> extends FutureBase<T> {
sb.append(value); sb.append(value);
} }
@SuppressWarnings("serial")
private static class ListenerArray<T> extends ArrayList<Listener<T>> implements Listener<T> { private static class ListenerArray<T> extends ArrayList<Listener<T>> implements Listener<T> {
@Override @Override
public void onSuccess(T value) { public void onSuccess(T value) {

View file

@ -16,7 +16,7 @@ public final class SucceededFuture<T> extends FutureBase<T> {
/** /**
* Stateless instance of empty results that can be shared safely. * Stateless instance of empty results that can be shared safely.
*/ */
public static final SucceededFuture EMPTY = new SucceededFuture(null, null); public static final SucceededFuture<?> EMPTY = new SucceededFuture<>(null, null);
private final T result; private final T result;

View file

@ -85,7 +85,6 @@ public abstract class Dispatcher {
Objects.requireNonNull(subscribers); Objects.requireNonNull(subscribers);
Queue<Event> queueForThread = queue.get(); Queue<Event> queueForThread = queue.get();
queueForThread.offer(new Event(event, subscribers)); queueForThread.offer(new Event(event, subscribers));
if (!dispatching.get()) { if (!dispatching.get()) {
dispatching.set(true); dispatching.set(true);
try { try {

View file

@ -1,11 +1,9 @@
package org.xbib.event.bus; package org.xbib.event.bus;
import java.lang.reflect.Method;
import java.util.Iterator; import java.util.Iterator;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
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;
@ -134,6 +132,10 @@ public class EventBus {
return identifier; return identifier;
} }
public SubscriberRegistry getSubscribers() {
return subscribers;
}
/** Returns the default executor this event bus uses for dispatching events to subscribers. */ /** Returns the default executor this event bus uses for dispatching events to subscribers. */
final Executor executor() { final Executor executor() {
return executor; return executor;
@ -183,7 +185,7 @@ public class EventBus {
* @param event event to post. * @param event event to post.
*/ */
public void post(Object event) { public void post(Object event) {
Iterator<Subscriber> eventSubscribers = subscribers.getSubscribers(event); Iterator<Subscriber> eventSubscribers = subscribers.getIterator(event);
if (eventSubscribers.hasNext()) { if (eventSubscribers.hasNext()) {
dispatcher.dispatch(event, eventSubscribers); dispatcher.dispatch(event, eventSubscribers);
} else if (!(event instanceof DeadEvent)) { } else if (!(event instanceof DeadEvent)) {
@ -197,33 +199,4 @@ public class EventBus {
return "EventBus{" + identifier + "}"; return "EventBus{" + identifier + "}";
} }
/** Simple logging handler for subscriber exceptions. */
static final class LoggingHandler implements SubscriberExceptionHandler {
static final LoggingHandler INSTANCE = new LoggingHandler();
@Override
public void handleException(Throwable exception, SubscriberExceptionContext context) {
Logger logger = logger(context);
if (logger.isLoggable(Level.SEVERE)) {
logger.log(Level.SEVERE, message(context), exception);
}
}
private static Logger logger(SubscriberExceptionContext context) {
return Logger.getLogger(EventBus.class.getName() + "." + context.getEventBus().identifier());
}
private static String message(SubscriberExceptionContext context) {
Method method = context.getSubscriberMethod();
return "Exception thrown by subscriber method "
+ method.getName()
+ '('
+ method.getParameterTypes()[0].getName()
+ ')'
+ " on subscriber "
+ context.getSubscriber()
+ " when dispatching event: "
+ context.getEvent();
}
}
} }

View file

@ -0,0 +1,37 @@
package org.xbib.event.bus;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Simple logging handler for subscriber exceptions.
*/
final class LoggingHandler implements SubscriberExceptionHandler {
static final LoggingHandler INSTANCE = new LoggingHandler();
@Override
public void handleException(Throwable exception, SubscriberExceptionContext context) {
Logger logger = logger(context);
if (logger.isLoggable(Level.SEVERE)) {
logger.log(Level.SEVERE, message(context), exception);
}
}
private static Logger logger(SubscriberExceptionContext context) {
return Logger.getLogger(EventBus.class.getName() + "." + context.getEventBus().identifier());
}
private static String message(SubscriberExceptionContext context) {
Method method = context.getSubscriberMethod();
return "Exception thrown by subscriber method "
+ method.getName()
+ '('
+ method.getParameterTypes()[0].getName()
+ ')'
+ " on subscriber "
+ context.getSubscriber()
+ " when dispatching event: "
+ context.getEvent();
}
}

View file

@ -23,7 +23,7 @@ import org.xbib.event.bus.util.TypeToken;
/** /**
* Registry of subscribers to a single event bus. * Registry of subscribers to a single event bus.
*/ */
final class SubscriberRegistry { public final class SubscriberRegistry {
/** /**
* All registered subscribers, indexed by event type. * All registered subscribers, indexed by event type.
@ -43,7 +43,6 @@ final class SubscriberRegistry {
/** Registers all subscriber methods on the given listener object. */ /** Registers all subscriber methods on the given listener object. */
void register(Object listener) { void register(Object listener) {
MultiMap<Class<?>, Subscriber> listenerMethods = findAllSubscribers(listener); MultiMap<Class<?>, Subscriber> listenerMethods = findAllSubscribers(listener);
for (Entry<Class<?>, Collection<Subscriber>> entry : listenerMethods.asMap().entrySet()) { for (Entry<Class<?>, Collection<Subscriber>> entry : listenerMethods.asMap().entrySet()) {
Class<?> eventType = entry.getKey(); Class<?> eventType = entry.getKey();
@ -53,7 +52,6 @@ final class SubscriberRegistry {
CopyOnWriteArraySet<Subscriber> newSet = new CopyOnWriteArraySet<>(); CopyOnWriteArraySet<Subscriber> newSet = new CopyOnWriteArraySet<>();
eventSubscribers = firstNonNull(subscribers.putIfAbsent(eventType, newSet), newSet); eventSubscribers = firstNonNull(subscribers.putIfAbsent(eventType, newSet), newSet);
} }
eventSubscribers.addAll(eventMethodsInListener); eventSubscribers.addAll(eventMethodsInListener);
} }
} }
@ -73,13 +71,12 @@ final class SubscriberRegistry {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"missing event subscriber for an annotated method. Is " + listener + " registered?"); "missing event subscriber for an annotated method. Is " + listener + " registered?");
} }
// don't try to remove the set if it's empty; that can't be done safely without a lock // don't try to remove the set if it's empty; that can't be done safely without a lock
// anyway, if the set is empty it'll just be wrapping an array of length 0 // anyway, if the set is empty it'll just be wrapping an array of length 0
} }
} }
Set<Subscriber> getSubscribersForTesting(Class<?> eventType) { public Set<Subscriber> getSubscribersForTesting(Class<?> eventType) {
return firstNonNull(subscribers.get(eventType), new LinkedHashSet<>()); return firstNonNull(subscribers.get(eventType), new LinkedHashSet<>());
} }
@ -87,7 +84,7 @@ final class SubscriberRegistry {
* Gets an iterator representing an immutable snapshot of all subscribers to the given event at * Gets an iterator representing an immutable snapshot of all subscribers to the given event at
* the time this method is called. * the time this method is called.
*/ */
Iterator<Subscriber> getSubscribers(Object event) { Iterator<Subscriber> getIterator(Object event) {
Set<Class<?>> eventTypes = flattenHierarchy(event.getClass()); Set<Class<?>> eventTypes = flattenHierarchy(event.getClass());
List<Iterator<Subscriber>> subscriberIterators = new ArrayList<>(eventTypes.size()); List<Iterator<Subscriber>> subscriberIterators = new ArrayList<>(eventTypes.size());
for (Class<?> eventType : eventTypes) { for (Class<?> eventType : eventTypes) {

View file

@ -20,7 +20,4 @@ public class SimpleClockEventConsumer implements EventConsumer {
logger.info("received demo clock event, instant = " + event.getInstant()); logger.info("received demo clock event, instant = " + event.getInstant());
} }
@Override
public void close() throws IOException {
}
} }

View file

@ -0,0 +1,31 @@
package org.xbib.event.generic;
import org.xbib.event.DefaultEvent;
public class DefaultGenericEvent extends DefaultEvent implements GenericEvent {
private Listener listener;
public DefaultGenericEvent() {
this(null);
}
public DefaultGenericEvent(Listener listener) {
this.listener = listener;
}
public DefaultGenericEvent setListener(Listener listener) {
this.listener = listener;
return this;
}
public Listener getListener() {
return listener;
}
public void received() {
if (listener != null) {
listener.listen(this);
}
}
}

View file

@ -0,0 +1,6 @@
package org.xbib.event.generic;
import org.xbib.event.Event;
public interface GenericEvent extends Event {
}

View file

@ -1,9 +1,13 @@
package org.xbib.event.generic; package org.xbib.event.generic;
import java.io.Closeable; import java.util.Set;
import org.xbib.event.bus.AsyncEventBus; import java.util.concurrent.CompletableFuture;
public class GenericEventManager implements Closeable { import org.xbib.event.bus.AsyncEventBus;
import org.xbib.event.bus.Subscriber;
import org.xbib.event.bus.SubscriberRegistry;
public class GenericEventManager {
private final AsyncEventBus eventBus; private final AsyncEventBus eventBus;
@ -15,7 +19,36 @@ public class GenericEventManager implements Closeable {
eventBus.post(event); eventBus.post(event);
} }
public void post(DefaultGenericEvent event,
CompletableFuture<Boolean> future) {
SubscriberRegistry subscriberRegistry = eventBus.getSubscribers();
Set<Subscriber> set = subscriberRegistry.getSubscribersForTesting(event.getClass());
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<Boolean> future;
public WrappedListener(Listener listener, int size, CompletableFuture<Boolean> future) {
this.listener = listener;
this.size = size;
this.future = future;
}
@Override @Override
public void close() { public void listen(GenericEvent event) {
if (listener != null) {
listener.listen(event);
}
if (--size == 0) {
future.complete(true);
}
}
} }
} }

View file

@ -0,0 +1,7 @@
package org.xbib.event.generic;
@FunctionalInterface
public interface Listener {
void listen(GenericEvent event);
}

View file

@ -10,6 +10,10 @@ import org.reactivestreams.Subscription;
* @param <T> * @param <T>
*/ */
public class EmptySubscriber<T> implements org.reactivestreams.Subscriber<T> { public class EmptySubscriber<T> implements org.reactivestreams.Subscriber<T> {
public EmptySubscriber() {
}
@Override @Override
public void onSubscribe(Subscription subscription) { public void onSubscribe(Subscription subscription) {
} }

View file

@ -10,6 +10,9 @@ public class DefaultFileFollowEvent extends DefaultEvent implements FileFollowEv
private String content; private String content;
public DefaultFileFollowEvent() {
}
@Override @Override
public void setPath(Path path) { public void setPath(Path path) {
this.path = path; this.path = path;

View file

@ -15,6 +15,10 @@ import java.util.concurrent.TimeoutException;
* Abstract base class for {@link EventExecutorGroup} implementations. * Abstract base class for {@link EventExecutorGroup} implementations.
*/ */
public abstract class AbstractEventExecutorGroup implements EventExecutorGroup { public abstract class AbstractEventExecutorGroup implements EventExecutorGroup {
protected AbstractEventExecutorGroup() {
}
@Override @Override
public Future<?> submit(Runnable task) { public Future<?> submit(Runnable task) {
return next().submit(task); return next().submit(task);

View file

@ -1097,7 +1097,7 @@ public abstract class SingleThreadEventExecutor extends AbstractScheduledEventEx
@Override @Override
public long id() { public long id() {
return t.getId(); return t.threadId();
} }
@Override @Override

View file

@ -7,7 +7,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
public class CEFMessageParser extends MessageParser { public final class CEFMessageParser extends MessageParser {
private static final String CEF_PREFIX_PATTERN = "^(<(?<priority>\\d+)>)?(?<date>([a-zA-Z]{3}\\s+\\d+\\s+\\d+:\\d+:\\d+)|([0-9T:.Z-]+))\\s+(?<host>\\S+)\\s+CEF:(?<version>\\d+)\\|(?<data>.*)$"; private static final String CEF_PREFIX_PATTERN = "^(<(?<priority>\\d+)>)?(?<date>([a-zA-Z]{3}\\s+\\d+\\s+\\d+:\\d+:\\d+)|([0-9T:.Z-]+))\\s+(?<host>\\S+)\\s+CEF:(?<version>\\d+)\\|(?<data>.*)$";

View file

@ -201,6 +201,9 @@ public class DefaultSyslogMessage implements SyslogMessage {
Map<String, Object> map; Map<String, Object> map;
private Builder() {
}
public Builder date(LocalDateTime date) { public Builder date(LocalDateTime date) {
this.date = date; this.date = date;
return this; return this;

View file

@ -3,7 +3,7 @@ package org.xbib.event.syslog;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.regex.Matcher; import java.util.regex.Matcher;
public class RFC3164MessageParser extends MessageParser { public final class RFC3164MessageParser extends MessageParser {
private static final String PATTERN = "^(<(?<priority>\\d+)>)?(?<date>([a-zA-Z]{3}\\s+\\d+\\s+\\d+:\\d+:\\d+)|([0-9T:.Z-]+))\\s+(?<host>\\S+)\\s+((?<tag>[^\\[\\s\\]]+)(\\[(?<procid>\\d+)\\])?:)*\\s*(?<message>.+)$"; private static final String PATTERN = "^(<(?<priority>\\d+)>)?(?<date>([a-zA-Z]{3}\\s+\\d+\\s+\\d+:\\d+:\\d+)|([0-9T:.Z-]+))\\s+(?<host>\\S+)\\s+((?<tag>[^\\[\\s\\]]+)(\\[(?<procid>\\d+)\\])?:)*\\s*(?<message>.+)$";

View file

@ -7,7 +7,7 @@ import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
public class RFC5424MessageParser extends MessageParser { public final class RFC5424MessageParser extends MessageParser {
private static final String PATTERN = "^<(?<priority>\\d+)>(?<version>\\d{1,3})\\s*(?<date>[0-9:+-TZ]+)\\s*(?<host>\\S+)\\s*(?<appname>\\S+)\\s*(?<procid>\\S+)\\s*(?<msgid>\\S+)\\s*(?<structureddata>(-|\\[.+\\]))\\s*(?<message>.+)$"; private static final String PATTERN = "^<(?<priority>\\d+)>(?<version>\\d{1,3})\\s*(?<date>[0-9:+-TZ]+)\\s*(?<host>\\S+)\\s*(?<appname>\\S+)\\s*(?<procid>\\S+)\\s*(?<msgid>\\S+)\\s*(?<structureddata>(-|\\[.+\\]))\\s*(?<message>.+)$";

View file

@ -64,7 +64,6 @@ public final class InternalThreadLocalMap {
private BitSet cleanerFlags; private BitSet cleanerFlags;
/** @deprecated These padding fields will be removed in the future. */
public long rp1, rp2, rp3, rp4, rp5, rp6, rp7, rp8; public long rp1, rp2, rp3, rp4, rp5, rp6, rp7, rp8;
static { static {

View file

@ -0,0 +1,80 @@
package org.xbib.event;
import org.junit.jupiter.api.Test;
import org.xbib.event.bus.Subscribe;
import org.xbib.event.generic.DefaultGenericEvent;
import org.xbib.event.generic.GenericEvent;
import org.xbib.settings.Settings;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class EventManagerTest {
private static final Logger logger = Logger.getLogger(EventManagerTest.class.getName());
@Test
void testGenericEvents() {
Settings settings = Settings.settingsBuilder()
.build();
TestEventConsumer consumer = new TestEventConsumer();
EventManager eventManager = EventManager.builder(settings)
.register(consumer)
.build();
eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> {
logger.log(Level.INFO, "received event " + e);
}));
}
@Test
void testGenericEventWithWaitForSubscriber() throws InterruptedException, ExecutionException {
Settings settings = Settings.settingsBuilder()
.build();
TestEventConsumer consumer = new TestEventConsumer();
EventManager eventManager = EventManager.builder(settings)
.register(consumer)
.build();
CompletableFuture<GenericEvent> future = new CompletableFuture<>();
eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> {
logger.log(Level.INFO, "received event " + e);
future.complete(e);
}));
GenericEvent e = future.get();
logger.log(Level.INFO, "the event was received with result " + e + ", continuing");
}
@Test
void testGenericEventWithWaitForAllConsumers() throws ExecutionException, InterruptedException {
Settings settings = Settings.settingsBuilder()
.build();
TestEventConsumer consumer1 = new TestEventConsumer();
TestEventConsumer consumer2 = new TestEventConsumer();
EventManager eventManager = EventManager.builder(settings)
.register(consumer1)
.register(consumer2)
.loadEventConsumers()
.build();
CompletableFuture<Boolean> future = new CompletableFuture<>();
eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> {
logger.log(Level.INFO, "received event " + e);
}), future);
Boolean b = future.get();
if (b != null && b) {
logger.log(Level.INFO, "the event was received by all consumers, continuing");
}
}
private static class TestEventConsumer implements EventConsumer {
TestEventConsumer() {
}
@Subscribe
public void onEvent(DefaultGenericEvent event) {
event.received();
}
}
}

View file

@ -18,8 +18,4 @@ public class TestClockEventConsumer implements EventConsumer {
void onEvent(TestClockEvent event) { void onEvent(TestClockEvent event) {
logger.log(Level.INFO, "received test clock event on " + Instant.now() + " event instant = " + event.getInstant()); logger.log(Level.INFO, "received test clock event on " + Instant.now() + " event instant = " + event.getInstant());
} }
@Override
public void close() throws IOException {
}
} }

View file

@ -19,8 +19,4 @@ public class TestFileFollowEventConsumer implements EventConsumer {
void onEvent(TestFileFollowEvent event) { void onEvent(TestFileFollowEvent event) {
logger.log(Level.INFO, "received filefollow event path = " + event.getPath() + " content = " + event.getContent()); logger.log(Level.INFO, "received filefollow event path = " + event.getPath() + " content = " + event.getContent());
} }
@Override
public void close() throws IOException {
}
} }

View file

@ -19,7 +19,4 @@ public class TestTimerEventConsumer implements EventConsumer {
logger.log(Level.INFO, "received test timer event on " + Instant.now() + " event instant = " + event.getInstant()); logger.log(Level.INFO, "received test timer event on " + Instant.now() + " event instant = " + event.getInstant());
} }
@Override
public void close() throws IOException {
}
} }