The EventBus allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other). It is designed exclusively to replace traditional Java in-process event distribution using explicit registration. It is not a general-purpose publish-subscribe system, nor is it intended for interprocess communication.
Events are routed based on their type — an event will be delivered to any handler for any type to which the event is assignable. This includes implemented interfaces, all superclasses, and all interfaces implemented by superclasses.
When {@code post} is called, all registered handlers for an event are runin sequence, so handlers should be reasonably quick. If an event may trigger an extended process (such as a database load), spawn a thread or queue it for later. (For a convenient way to do this, use an {@link AsyncEventBus}.)
Handlers should not, in general, throw. If they do, the EventBus will catch and log the exception. This is rarely the right solution for error handling and should not be relied upon; it is intended solely to help find problems during development.
The EventBus guarantees that it will not call a handler method from multiple threads simultaneously, unless the method explicitly allows it by bearing the {@link AllowConcurrentEvents} annotation. If this annotation isnot present, handler methods need not worry about being reentrant, unless also called from outside the EventBus.
If a handler for a supertype of all events (such as Object) is registered, no event will ever be considered dead, and no DeadEvents will be generated. Accordingly, while DeadEvent extends {@link Object}, a handler registered to receive any Object will never receive a DeadEvent.
This class is safe for concurrent use. @author Cliff Biffle @since Guava release 10
true
, then that subscriber is interested in the event. false
. In that case, theevents will be sent to the subscriber asynchronously. See {@link SyncSubscribersGatekeeper} for more details. There are multiple facets of this async dispatch mode which are described below: this.eventBus = new EventBus(this);The {@code EventBus} will register itself in the application once instantiated. It might bepractical to keep a reference in the application, but you can always get it using {@link #get()}. @author papegaaij
There are three event scopes, and therefore three event bus types:
The event buses are chained in the following way:
The primary {@code EventBus} implementation is always UI-scoped and can be injected into your UI classes like this: @Autowired EventBus myUIScopedEventBus;
With this implementation, you can subscribe to and publish events of all scopes (see {@link #publish(EventScope,Object,Object)}).
However, if you want to inject any of the other event buses, you have to use the {@link org.vaadin.spring.events.EventBusScope} qualifier. For example, the following code will inject the session event bus: @Autowired @EventBusScope(EventScope.SESSION) EventBus myUIScopedEventBus;
@author Petter Holmström (petter@vaadin.com)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|