mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Debugs for EventBus and fixed unsubscribe all (#266)
* Added unsubscribe to EventFlow. ListenerHandler now functional. GlobalEventbus now user listenerHandler * getAllListeners * Removed nulls * Fixed stress tests * Added docs, no more list creation when adding events to the bus. * Fixed unsubscribe not working. * Moved away from deprecated functions * moved from wildcard to typed * Moved away from deprecated function * Added debugging to GlobalEventBus * Fixed cleaning flow * Fixed unsubscribe all * Fixed unsubscribe all * Removed unused import
This commit is contained in:
committed by
GitHub
parent
25c02c7ad0
commit
81740acd04
@@ -464,14 +464,17 @@ public class EventFlow {
|
|||||||
* Unsubscribe all events.
|
* Unsubscribe all events.
|
||||||
*/
|
*/
|
||||||
public void unsubscribeAll() {
|
public void unsubscribeAll() {
|
||||||
this.listeners.forEach(this::unsubscribe);
|
listeners.removeIf(handler -> {
|
||||||
|
GlobalEventBus.unsubscribe(handler);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clean and remove everything inside {@link EventFlow}.
|
* Clean and remove everything inside {@link EventFlow}.
|
||||||
*/
|
*/
|
||||||
private void clean() {
|
private void clean() {
|
||||||
this.listeners.clear();
|
unsubscribeAll();
|
||||||
this.event = null;
|
this.event = null;
|
||||||
this.result = null;
|
this.result = null;
|
||||||
} // TODO
|
} // TODO
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import com.lmax.disruptor.dsl.ProducerType;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.toop.framework.eventbus.events.EventType;
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.events.UniqueEvent;
|
import org.toop.framework.eventbus.events.UniqueEvent;
|
||||||
|
|
||||||
@@ -14,6 +17,7 @@ import org.toop.framework.eventbus.events.UniqueEvent;
|
|||||||
* publishing.
|
* publishing.
|
||||||
*/
|
*/
|
||||||
public final class GlobalEventBus {
|
public final class GlobalEventBus {
|
||||||
|
private static final Logger logger = LogManager.getLogger(GlobalEventBus.class);
|
||||||
|
|
||||||
/** Map of event class to type-specific listeners. */
|
/** Map of event class to type-specific listeners. */
|
||||||
private static final Map<Class<?>, CopyOnWriteArrayList<ListenerHandler<?>>>
|
private static final Map<Class<?>, CopyOnWriteArrayList<ListenerHandler<?>>>
|
||||||
@@ -73,6 +77,7 @@ public final class GlobalEventBus {
|
|||||||
// Subscription
|
// Subscription
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
public static <T extends EventType> void subscribe(ListenerHandler<T> listener) {
|
public static <T extends EventType> void subscribe(ListenerHandler<T> listener) {
|
||||||
|
logger.debug("Subscribing to {}: {}", listener.getListenerClass().getSimpleName(), listener.getListener().getClass().getSimpleName());
|
||||||
LISTENERS.computeIfAbsent(listener.getListenerClass(), _ -> new CopyOnWriteArrayList<>()).add(listener);
|
LISTENERS.computeIfAbsent(listener.getListenerClass(), _ -> new CopyOnWriteArrayList<>()).add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +90,7 @@ public final class GlobalEventBus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void unsubscribe(ListenerHandler<?> listener) {
|
public static void unsubscribe(ListenerHandler<?> listener) {
|
||||||
// TODO suspicious call
|
logger.debug("Unsubscribing from {}: {}", listener.getListenerClass().getSimpleName(), listener.getListener().getClass().getSimpleName());
|
||||||
LISTENERS.getOrDefault(listener.getListenerClass(), new CopyOnWriteArrayList<>())
|
LISTENERS.getOrDefault(listener.getListenerClass(), new CopyOnWriteArrayList<>())
|
||||||
.remove(listener);
|
.remove(listener);
|
||||||
LISTENERS.entrySet().removeIf(entry -> entry.getValue().isEmpty());
|
LISTENERS.entrySet().removeIf(entry -> entry.getValue().isEmpty());
|
||||||
@@ -129,6 +134,8 @@ public final class GlobalEventBus {
|
|||||||
private static void dispatchEvent(EventType event) {
|
private static void dispatchEvent(EventType event) {
|
||||||
Class<?> clazz = event.getClass();
|
Class<?> clazz = event.getClass();
|
||||||
|
|
||||||
|
logger.debug("Triggered event: {}", event.getClass().getSimpleName());
|
||||||
|
|
||||||
CopyOnWriteArrayList<ListenerHandler<?>> classListeners = LISTENERS.get(clazz);
|
CopyOnWriteArrayList<ListenerHandler<?>> classListeners = LISTENERS.get(clazz);
|
||||||
if (classListeners != null) {
|
if (classListeners != null) {
|
||||||
for (ListenerHandler<?> listener : classListeners) {
|
for (ListenerHandler<?> listener : classListeners) {
|
||||||
|
|||||||
Reference in New Issue
Block a user