EventBus

public final class EventBus extends MulticastBus<M, E, C, D> implements ContextAware

Dispatches incoming events to subscribers and provides ways for registering those subscribers.

Receiving Events

To receive event messages, a subscriber object should:

  1. Expose a public method that accepts an event message as the first parameter and an EventContext as the second (optional) parameter.
  2. Mark the method with the @Subscribe annotation.
  3. register Register} with an instance of EventBus directly or rely on message delivery from an EventDispatcher. An example of such a dispatcher is ProjectionRepository.

Note: A subscriber method cannot accept just Message as the first parameter. It must be an exact type of the event message that needs to be handled.

Posting Events

Events are posted to an EventBus using post method. Normally this is done by an AggregateRepository in the process of handling a command or by a ProcessManager.

The passed Event is stored in the EventStore associated with the EventBusbefore it is passed to subscribers.

If there are no subscribers or dispatchers for the posted event, the fact is logged as warning with no further processing.

See also

Projection

io.spine.core.Subscribe

@Subscribe

Types

Link copied to clipboard
public class Builder extends BusBuilder<B, T, E, C, D>
The Builder for EventBus.

Functions

Link copied to clipboard
public void close()
Closes the filters of this bus and unregisters all the dispatchers.
Link copied to clipboard
Obtains the instance of DeadMessageHandler for this bus.
Link copied to clipboard
protected void dispatch(EventEnvelope event)
Passes the given envelope for dispatching.
Link copied to clipboard
public final Optional<Enricher> enricher()
Obtains Enricher used by this Event Bus.
Link copied to clipboard
Returns EventStore associated with the bus.
Link copied to clipboard
public boolean isRegistered()
Determines if this instance is already registered with a Bounded Context.
Link copied to clipboard
public static EventBus.Builder newBuilder()
Creates a builder for new EventBus.
Link copied to clipboard
public final void post(Event event)
Posts the event for handling.
public final void post(Iterable<Event> events)
Posts the events for handling.
Link copied to clipboard
Obtains the view Set of events that are known to this EventBus.
Link copied to clipboard
public void registerWith(BoundedContext context)
Registers this instance as a part of the given Bounded Context.
Link copied to clipboard
protected EventDispatcherRegistry registry()
Obtains the dispatcher registry.
Link copied to clipboard
protected void store(Iterable<Event> events)
Stores the given messages into the underlying storage.
Link copied to clipboard
protected EventEnvelope toEnvelope(Event message)
Packs the given message of type T into an envelope of type E.
Link copied to clipboard
Obtains the instance of EnvelopeValidator for this bus.

Inherited functions

Link copied to clipboard
protected int callDispatchers(E messageEnvelope)
Call the dispatchers for the messageEnvelope.
Link copied to clipboard
public void checkNotRegistered()
Verifies that this instance is NOT registered yet.
Link copied to clipboard
public void checkOpen()
Ensures that the object is open.
Link copied to clipboard
public void checkRegistered()
Verifies that this instance is already registered.
Link copied to clipboard
public abstract void close()
Link copied to clipboard
public boolean hasFilter(BusFilter<E> filter)
Link copied to clipboard
public boolean hasListener(Listener<E> listener)
Link copied to clipboard
public final boolean isOpen()
Tells if this bus can accept messages for posting.
Link copied to clipboard
protected final void onDispatched(SignalId signal)
Called after the dispatching of the signal to all of the target dispatchers has been completed.
Link copied to clipboard
protected final void onDispatchingStarted(SignalId signal)
Called before the dispatching of the signal with the passed ID is started.
Link copied to clipboard
public final void post(T message, StreamObserver<Ack> observer)
Posts the message to the bus.
public final void post(Iterable<T> messages, StreamObserver<Ack> observer)
Posts the given messages to the bus.
Link copied to clipboard
protected StreamObserver<Ack> prepareObserver(Iterable<T> messages, StreamObserver<Ack> source)
Prepares the given StreamObserver to post messages into this bus.
Link copied to clipboard
public void register(D dispatcher)
Registers the passed dispatcher.
Link copied to clipboard
A callback for derived classes to modify the order of filters used by the bus.
Link copied to clipboard
public void unregister(D dispatcher)
Unregisters dispatching for message classes of the passed dispatcher.