json format of event
parent
788ce1b7d7
commit
757e62040b
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue