Package de.espirit.firstspirit.agency
Interface EventBusAgent
public interface EventBusAgent
An event agent providing means for custom event buses and events.
- Since:
- 5.2.220703
-
Field Summary
Modifier and TypeFieldDescriptionstatic final SpecialistType<EventBusAgent>
Agent providing means for custom events. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Serializable>
BusIdentifier<T>createBusIdentifier
(@NotNull String identifier, @NotNull Class<T> eventType, @org.jetbrains.annotations.Nullable long... ids) Createbus
identifier object.
A bus identifier should be as local and specific as possible to avoid unnecessary event delivery to listeners not interested in the events.<T extends Serializable>
voidpost
(@NotNull BusIdentifier<T> bus, T data) Post an event on the specifiedbus
.
Event delivery is asynchronous - this method returns before the event is delivered and processed by all possibly remote listeners.
Eachbus
has its own event queue and the events will be delivered in order one event at a time from this queue to the registered listeners.<T extends Serializable>
voidregister
(@NotNull BusIdentifier<T> bus, @NotNull Consumer<T> listener) <T extends Serializable>
voidunregister
(@NotNull BusIdentifier<T> bus, @NotNull Consumer<T> listener)
-
Field Details
-
TYPE
Agent providing means for custom events.- Since:
- 5.2.220703
-
-
Method Details
-
post
Post an event on the specifiedbus
.
Event delivery is asynchronous - this method returns before the event is delivered and processed by all possibly remote listeners.
Eachbus
has its own event queue and the events will be delivered in order one event at a time from this queue to the registered listeners. Event processing in a listener should be as fast as possible to avoid delayed event processing in subsequent listeners.
The eventdata
is serialized and should be as small as possible to avoid unnecessary network traffic and overhead.- Type Parameters:
T
- The type of the events on the bus.- Parameters:
bus
- The bus to post the event on.data
- The event data to post.- Since:
- 5.2.220703
- See Also:
-
register
<T extends Serializable> void register(@NotNull @NotNull BusIdentifier<T> bus, @NotNull @NotNull Consumer<T> listener) Register an eventlistener
for a specifiedbus
.
If the listener was already registered, the listener is moved to last position of the registered listeners. A listener may be registered on multiple buses if the event type is compatible.
The listener reference will be held with aWeakReference
. You have to keep a reference to the listener instance, or it will be automatically removed and unregistered.
If the listener implementsCloseable
, itsclose()
method will be called, if the underlyingConnection
is closed. A closedConnection
no longer delivers any events to a listener.
Note: Remember to callunregister(BusIdentifier, Consumer)
if the listener is no longer used.- Type Parameters:
T
- The type of the events on the bus.- Parameters:
bus
- The bus to register the event listener for.listener
- The event listener.- Since:
- 5.2.220703
- See Also:
-
unregister
<T extends Serializable> void unregister(@NotNull @NotNull BusIdentifier<T> bus, @NotNull @NotNull Consumer<T> listener) Unregister an eventlistener
for a specifiedbus
.
If the listener was not registered this method does nothing. A listener may be registered on multiple buses if the event type is compatible.- Type Parameters:
T
- The type of the events on the bus.- Parameters:
bus
- The bus to unregister the event listener for.listener
- The event listener.- Since:
- 5.2.220703
- See Also:
-
createBusIdentifier
static <T extends Serializable> BusIdentifier<T> createBusIdentifier(@NotNull @NotNull String identifier, @NotNull @NotNull Class<T> eventType, @Nullable @org.jetbrains.annotations.Nullable long... ids) Createbus
identifier object.
A bus identifier should be as local and specific as possible to avoid unnecessary event delivery to listeners not interested in the events.- Type Parameters:
T
- The type of the events on the bus.- Parameters:
identifier
- The string identifier name of the bus.eventType
- The type of the events on the bus.ids
- Optional additional ids to identify the bus. For instance a project id if the bus is project local.- Returns:
- The created bus identifier object.
- Since:
- 5.2.220703
-