public abstract class AbstractEventBus extends AbstractLoggableComponent implements EventBus
EventBus
. It is compatible to work in limited environments such as GWT.Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractEventBus.EventDispatcher<E>
A dispatcher for all
EventListener s of a particular event
type . |
protected static class |
AbstractEventBus.EventListenerContainer<E>
A simple container for a
registered EventListener . |
Modifier and Type | Field and Description |
---|---|
private Queue<Object> |
eventQueue |
private Map<Class<?>,AbstractEventBus.EventDispatcher> |
eventType2dispatcherMap |
private GlobalExceptionHandler |
globalExceptionHandler |
private CollectionFactory<?> |
queueFactory |
Constructor and Description |
---|
AbstractEventBus()
The constructor.
|
AbstractEventBus(QueueFactory queueFactory,
MapFactory<?> mapFactory)
The constructor.
|
Modifier and Type | Method and Description |
---|---|
<E> void |
addListener(Class<E> eventType,
EventListener<E> listener)
This method registers a listener that is interested in
Event s. |
protected <E> void |
dispatchEvent(E event,
Collection<Throwable> errors)
Dispatches the given
Event . |
protected void |
dispatchEvents()
Dispatches all events in the event queue.
|
private AbstractEventBus.EventDispatcher |
getEventDispatcher(Class<?> eventType,
boolean createIfNotExists)
Gets or creates the
AbstractEventBus.EventDispatcher for the given eventType . |
protected <E> AbstractEventBus.EventDispatcher<? super E> |
getEventDispatcherOptional(Class<E> eventType)
Gets the most specific
AbstractEventBus.EventDispatcher responsible the given eventType . |
protected <E> AbstractEventBus.EventDispatcher<E> |
getEventDispatcherRequired(Class<E> eventType)
Gets or creates the
AbstractEventBus.EventDispatcher for the given eventType . |
GlobalExceptionHandler |
getGlobalExceptionHandler() |
protected void |
handleErrors(Collection<Throwable> errors,
Object event)
This method is called if errors occurred while dispatching events.
|
protected void |
handleUndispatchedEvent(Object event)
|
boolean |
removeListener(EventListener<?> listener)
This method removes a listener.
|
<E> void |
sendEvent(E event)
This method sends an event to all
registered listeners . |
void |
setGlobalExceptionHandler(GlobalExceptionHandler errorHandler) |
protected abstract void |
triggerDispatchEvents()
Called from
sendEvent(Object) to ensure dispatchEvents() is triggered. |
createLogger, doInitialize, getLogger
doInitialized, getInitializationState, initialize
private final CollectionFactory<?> queueFactory
private final Map<Class<?>,AbstractEventBus.EventDispatcher> eventType2dispatcherMap
private GlobalExceptionHandler globalExceptionHandler
public AbstractEventBus()
public AbstractEventBus(QueueFactory queueFactory, MapFactory<?> mapFactory)
queueFactory
- is the factory used to create simple Queue
s for Event
s and EventListener
s.mapFactory
- is the factory used to create Map
s.public GlobalExceptionHandler getGlobalExceptionHandler()
GlobalExceptionHandler
instance.@Inject public void setGlobalExceptionHandler(GlobalExceptionHandler errorHandler)
errorHandler
- is the GlobalExceptionHandler
to Inject
.public <E> void sendEvent(E event)
EventBus
registered listeners
.sendEvent
in interface EventBus
E
- is the generic type of the Event
to send. May be any Object such as a String
. However in
most cases it is recommended to create real Event
classes.event
- is the event to send. May be any Object such as a String
. However in most cases it is
recommended to create real Event
classes.protected abstract void triggerDispatchEvents()
sendEvent(Object)
to ensure dispatchEvents()
is triggered. This can be done
synchronous or asynchronous.protected void dispatchEvents()
protected <E> void dispatchEvent(E event, Collection<Throwable> errors)
Event
.E
- is the generic type of event
.event
- is the Event
to dispatch.errors
- is a Collection
where errors are collected.protected void handleUndispatchedEvent(Object event)
event
- is the un-dispatched event.protected void handleErrors(Collection<Throwable> errors, Object event)
errors
- is the Collection
with the errors. Will NOT be empty
.event
- is the event that has been send and caused the errors.protected <E> AbstractEventBus.EventDispatcher<E> getEventDispatcherRequired(Class<E> eventType)
AbstractEventBus.EventDispatcher
for the given eventType
.E
- is the generic type of eventType
.eventType
- is the Class
reflecting the event
.AbstractEventBus.EventDispatcher
responsible for the given eventType
.protected <E> AbstractEventBus.EventDispatcher<? super E> getEventDispatcherOptional(Class<E> eventType)
AbstractEventBus.EventDispatcher
responsible the given eventType
.E
- is the generic type of eventType
.eventType
- is the Class
reflecting the event
.AbstractEventBus.EventDispatcher
responsible for the given eventType
. May be null
if no EventListener
is registered
for a compatible
eventType
.private AbstractEventBus.EventDispatcher getEventDispatcher(Class<?> eventType, boolean createIfNotExists)
AbstractEventBus.EventDispatcher
for the given eventType
.eventType
- is the Class
reflecting the event
.createIfNotExists
- - if true
the requested AbstractEventBus.EventDispatcher
will be created if it does not
exists, if false
this method will return null
if it does not exist.AbstractEventBus.EventDispatcher
responsible for the given eventType
. May be null
if
createIfNotExists
is null
and not available.public <E> void addListener(Class<E> eventType, EventListener<E> listener)
EventBus
Event
s.addListener
in interface EventBus
E
- is the generic type of the Event
s to listen to. May be any Object such as a String
.
However in most cases it is recommended to create real Event
classes.eventType
- is the type of the Event
s to listen to (including all derived types). In limited
environments such as GWT, interfaces might not be supported.listener
- is the listener to add.public boolean removeListener(EventListener<?> listener)
EventBus
removeListener
in interface EventBus
listener
- is the listener to remove.true
if the given listener
has successfully been removed, false
if the
listener
was NOT registered
.Copyright © 2001–2016 mmm-Team. All rights reserved.