Package com.google.common.eventbus
Class Dispatcher
java.lang.Object
com.google.common.eventbus.Dispatcher
- Direct Known Subclasses:
Dispatcher.ImmediateDispatcher
,Dispatcher.LegacyAsyncDispatcher
,Dispatcher.PerThreadQueuedDispatcher
Handler for dispatching events to subscribers, providing different event ordering guarantees that
make sense for different situations.
Note: The dispatcher is orthogonal to the subscriber's Executor
. The dispatcher
controls the order in which events are dispatched, while the executor controls how (i.e. on which
thread) the subscriber is actually called when an event is dispatched to it.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Implementation ofimmediate()
.private static final class
Implementation of alegacyAsync()
dispatcher.private static final class
Implementation of aperThreadDispatchQueue()
dispatcher. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) abstract void
dispatch
(Object event, Iterator<Subscriber> subscribers) Dispatches the givenevent
to the givensubscribers
.(package private) static Dispatcher
Returns a dispatcher that dispatches events to subscribers immediately as they're posted without using an intermediate queue to change the dispatch order.(package private) static Dispatcher
Returns a dispatcher that queues events that are posted in a single global queue.(package private) static Dispatcher
Returns a dispatcher that queues events that are posted reentrantly on a thread that is already dispatching an event, guaranteeing that all events posted on a single thread are dispatched to all subscribers in the order they are posted.
-
Constructor Details
-
Dispatcher
Dispatcher()
-
-
Method Details
-
perThreadDispatchQueue
Returns a dispatcher that queues events that are posted reentrantly on a thread that is already dispatching an event, guaranteeing that all events posted on a single thread are dispatched to all subscribers in the order they are posted.When all subscribers are dispatched to using a direct executor (which dispatches on the same thread that posts the event), this yields a breadth-first dispatch order on each thread. That is, all subscribers to a single event A will be called before any subscribers to any events B and C that are posted to the event bus by the subscribers to A.
-
legacyAsync
Returns a dispatcher that queues events that are posted in a single global queue. This behavior matches the original behavior of AsyncEventBus exactly, but is otherwise not especially useful. For async dispatch, an immediate dispatcher should generally be preferable. -
immediate
Returns a dispatcher that dispatches events to subscribers immediately as they're posted without using an intermediate queue to change the dispatch order. This is effectively a depth-first dispatch order, vs. breadth-first when using a queue. -
dispatch
Dispatches the givenevent
to the givensubscribers
.
-