public final class BasicEventBus extends Object implements EventBus
The BasicEventBus class is thread safe and uses a background thread to notify
the subscribers of the event. The subscribers are notified in a serial
fashion, and only one event will be published at a time. Though, the
publish(Object) method is done in a non-blocking way.
Subscribers subscribe to the EventBus using the subscribe(Object)
method. A specific subscriber type is not required, but the subscriber will
be reflected to find all methods annotated with the EventHandler
annotations. These methods will be invoked as needed by the event bus based
on the type of the first parameter to the annotated method.
An event handler can indicate that it can veto events by setting the
EventHandler.canVeto() value to true. This will inform the EventBus
of the subscriber's desire to veto the event. A vetoed event will not be sent
to the regular subscribers.
During publication of an event, all veto EventHandler methods will be
notified first and allowed to throw a VetoException indicating that
the event has been vetoed and should not be published to the remaining event
handlers. If no vetoes have been made, the regular subscriber handlers will
be notified of the event.
Subscribers are stored using a WeakReference such that a memory leak
can be avoided if the client fails to unsubscribe at the end of the use.
However, calling the unsubscribe(Object) method is highly
recommended none-the-less.
| Constructor and Description |
|---|
BasicEventBus()
Default constructor sets up the executorService property to use the
Executors.newCachedThreadPool() implementation. |
BasicEventBus(ExecutorService executorService,
boolean waitForHandlers) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
hasPendingEvents()
Returns if the event bus has pending events.
|
void |
publish(Object event)
Publish the specified event to the event bus.
|
void |
shutdown(long timeout,
TimeUnit unit)
Shutdown underlaying events and wait until terminaison or time out
occurs.
|
void |
subscribe(Object subscriber)
Subscribe the specified instance as a potential event subscriber.
|
void |
unsubscribe(Object subscriber)
Unsubscribe the specified subscriber from receiving future published
events.
|
public BasicEventBus()
Executors.newCachedThreadPool() implementation. The configured
ExecutorService will have a custom ThreadFactory such that the threads
returned will be daemon threads (and thus not block the application from
shutting down).public BasicEventBus(ExecutorService executorService, boolean waitForHandlers)
public void subscribe(Object subscriber)
EventHandler
annotation if it expects to receive notifications.
Note that the EventBus maintains a WeakReference to the
subscriber, but it is still adviced to call the
unsubscribe(Object) method if the subscriber does not wish to
receive events any longer.
subscribe in interface EventBussubscriber - The subscriber object which will receive notifications on
EventHandler annotated methods.public void unsubscribe(Object subscriber)
unsubscribe in interface EventBussubscriber - The object to unsubcribe from future events.public void publish(Object event)
public boolean hasPendingEvents()
hasPendingEvents in interface EventBuspublic void shutdown(long timeout,
TimeUnit unit)
throws InterruptedException
timeout - unit - InterruptedExceptionCopyright © 2011–2014 Intelligents-ia. All rights reserved.