Example usage for com.google.common.eventbus DeadEvent DeadEvent

List of usage examples for com.google.common.eventbus DeadEvent DeadEvent

Introduction

In this page you can find the example usage for com.google.common.eventbus DeadEvent DeadEvent.

Prototype

public DeadEvent(Object source, Object event) 

Source Link

Document

Creates a new DeadEvent.

Usage

From source file:de.ks.eventsystem.bus.EventBus.java

private void postToEventHandlers(Object event, boolean wait) {
    List<Class<?>> hierarchy = ReflectionUtil.getClassHierarchy(event.getClass(), false);
    LinkedList<EventExecution> executions = new LinkedList<>();
    lock.readLock().lock();//w w w  .  jav a  2s .c  om
    try {
        for (Class<?> eventType : hierarchy) {
            List<EventHandler> eventHandlers = handlers.get(eventType);
            for (EventHandler eventHandler : eventHandlers) {
                executions.add(new EventExecution(event, eventHandler));
            }
        }
    } finally {
        lock.readLock().unlock();
    }

    for (EventExecution execution : executions) {
        boolean consumed = execution.handler.handleEvent(event, wait);
        if (consumed) {
            break;
        }
    }

    boolean noHandlerFound = executions.isEmpty();
    boolean isDeadEvent = event instanceof DeadEvent;

    if (noHandlerFound && !isDeadEvent) {
        postToEventHandlers(new DeadEvent(this, event), false);
    } else if (noHandlerFound) {
        log.warn("Could not find dead event handler.");
    }
}

From source file:blow.eventbus.OrderedEventBus.java

/**
 * Posts an event to all registered handlers.  This method will return
 * successfully after the event has been posted to all handlers, and
 * regardless of any exceptions thrown by handlers.
 *
 * <p>If no handlers have been subscribed for {@code event}'s class, and
 * {@code event} is not already a {@link DeadEvent}, it will be wrapped in a
 * DeadEvent and reposted./*  ww  w .  j a v a  2s  . c  o m*/
 *
 * @param event  event to post.
 */
public void post(Object event) throws InvocationTargetException {
    Set<Class<?>> dispatchTypes = flattenHierarchy(event.getClass());

    boolean dispatched = false;
    for (Class<?> eventType : dispatchTypes) {
        List<EventHandler> wrappers = getHandlersForEventType(eventType);

        if (wrappers != null && !wrappers.isEmpty()) {
            dispatched = true;
            for (EventHandler wrapper : wrappers) {
                enqueueEvent(event, wrapper);
            }
        }
    }

    if (!dispatched && !(event instanceof DeadEvent)) {
        post(new DeadEvent(this, event));
    }

    dispatchQueuedEvents();
}