public interface EventBus
A traditional use case for an event bus is a Swing based application, where multiple actions and listeners are configured to capture the various events of the application, such as mouse clicks, window events, data loading events, etc. Swing promotes a one-to-one mapping between listener/listenee components, and as such, it can become difficult to configure the various listeners without tightly coupling all the various componenets together.
With an event bus, events can be published to the bus and any class can be configured to listen for such events. Thus, each individual component only needs to be tightly coupled with the event bus, and it can then receive notifications about events that it cares to know about.
The event bus pattern has a simple interface, with a subscribe/publish type
model. Any object can be subscribed to the event bus, but to received
messages from the bus, the object must have its methods annotated with the
EventHandler annotation. This annotation marks the methods of the
subscriber class which should be used to receive event bus events.
A published event has the potential to be vetoed and thus not propagated to
other non-vetoing subscribers. This is accomplished by setting the
EventHandler.canVeto() property to true and throwing a
VetoException when the method is called from the EventBus. The event
bus will note the veto and not relay the message to the subscribers, but will
instead send a VetoEvent out on the bus indicating that the published
event has been vetoed.
| Modifier and Type | Method and Description |
|---|---|
boolean |
hasPendingEvents()
Indicates whether the bus has pending events to publish.
|
void |
publish(Object event)
Sends a message on the bus which will be propagated to the appropriate
subscribers of the event type.
|
void |
subscribe(Object subscriber)
Subscribes the specified subscriber to the event bus.
|
void |
unsubscribe(Object subscriber)
Removes the specified object from the event bus subscription list.
|
void subscribe(Object subscriber)
EventHandler annotation.
Each event handler method should take a single parameter indicating the type of event it wishes to receive. When events are published on the bus, only subscribers who have an EventHandler method with a matching parameter of the same type as the published event will receive the event notification from the bus.
subscriber - The object to subscribe to the event bus.void unsubscribe(Object subscriber)
subscriber - The object previous subscribed to the event bus.void publish(Object event)
Events can be vetoed, indicating that the event should not propagate to
the subscribers that don't have a veto. The subscriber can veto by
setting the EventHandler.canVeto() return to true and by throwing
a VetoException.
There is no specification given as to how the messages will be delivered, in terms of synchronous or asynchronous. The only requirement is that all the event handlers that can issue vetos be called before non-vetoing handlers. Most implementations will likely deliver messages asynchronously.
event - The event to send out to the subscribers of the same type.boolean hasPendingEvents()
Copyright © 2011–2014 Intelligents-ia. All rights reserved.