Class VaadinServiceEventBus
- All Implemented Interfaces:
Serializable
VaadinService.
Anything that wants to notify listeners registered on a service can define an
event type and fire it through this bus, instead of the service having to
grow a listener collection and a fireXyz method for every new event
type:
service.getEventBus().addListener(MyEvent.class, event -> doSomething()); service.getEventBus().fireEvent(new MyEvent(service));
The API mirrors ComponentEventBus, but
unlike that one this bus is safe to use from several threads at once, which a
service-wide bus has to be: listeners can be added and removed while events
are being fired from other request threads.
Events are dispatched by their exact runtime type; a listener registered for a supertype is not notified of subtype events.
- Since:
- 25.3
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionVaadinServiceEventBus(VaadinService service) Creates an event bus for the given service. -
Method Summary
Modifier and TypeMethodDescription<E extends EventObject>
RegistrationaddListener(Class<E> eventType, SerializableConsumer<E> listener) Adds a listener for the given event type.<E extends EventObject>
voidfireEvent(E event, SerializableBiConsumer<? super E, Exception> errorHandler) Fires an event to the listeners registered for its exact type, in registration order, handing a listener that threw to the given error handler instead of logging it.voidfireEvent(EventObject event) Fires an event to the listeners registered for its exact type, in registration order.<E extends EventObject>
voidfireEventInReverseOrder(E event, SerializableBiConsumer<? super E, Exception> errorHandler) Fires an event to the listeners registered for its exact type in reverse registration order, handing a listener that threw to the given error handler instead of logging it.voidFires an event to the listeners registered for its exact type in reverse registration order.<E extends EventObject>
Collection<SerializableConsumer<E>> getListeners(Class<E> eventType) Gets the listeners registered for the given event type, in registration order.Gets the service this event bus belongs to.booleanhasListener(Class<? extends EventObject> eventType) Checks whether at least one listener is registered for the given event type.
-
Constructor Details
-
VaadinServiceEventBus
Creates an event bus for the given service.- Parameters:
service- the service owning this event bus, notnull
-
-
Method Details
-
getService
Gets the service this event bus belongs to.- Returns:
- the service, not
null
-
addListener
public <E extends EventObject> Registration addListener(Class<E> eventType, SerializableConsumer<E> listener) Adds a listener for the given event type.Listeners are notified in registration order. The same listener can be added several times, in which case it is notified once per registration.
- Type Parameters:
E- the event type- Parameters:
eventType- the type of event to listen to, notnulllistener- the listener to call when an event of the given type is fired, notnull- Returns:
- a handle that can be used for removing the listener
-
hasListener
Checks whether at least one listener is registered for the given event type.Callers on hot code paths can use this to skip building an event that nobody would receive.
- Parameters:
eventType- the event type to check, notnull- Returns:
trueif at least one listener is registered for the event type,falseotherwise
-
getListeners
Gets the listeners registered for the given event type, in registration order.- Type Parameters:
E- the event type- Parameters:
eventType- the event type, notnull- Returns:
- an unmodifiable collection of listeners, empty if none are registered
-
fireEvent
Fires an event to the listeners registered for its exact type, in registration order.An exception thrown by a listener is logged and the remaining listeners are notified regardless, so that one misbehaving listener can neither disrupt the caller nor hide the event from the other listeners. Use
fireEvent(EventObject, SerializableBiConsumer)to handle those exceptions differently.- Parameters:
event- the event to fire, notnull
-
fireEvent
public <E extends EventObject> void fireEvent(E event, SerializableBiConsumer<? super E, Exception> errorHandler) Fires an event to the listeners registered for its exact type, in registration order, handing a listener that threw to the given error handler instead of logging it.The listeners that have not been notified yet are notified next unless the error handler itself throws, in which case that exception is propagated to the caller and the remaining listeners are skipped.
- Type Parameters:
E- the event type- Parameters:
event- the event to fire, notnullerrorHandler- invoked with the event and the exception when a listener throws, notnull
-
fireEventInReverseOrder
Fires an event to the listeners registered for its exact type in reverse registration order.This is meant for the "closing" half of a pair of events, so that listeners nest: the listener registered first is notified last, in the same way that it was notified first of the "opening" event.
- Parameters:
event- the event to fire, notnull
-
fireEventInReverseOrder
public <E extends EventObject> void fireEventInReverseOrder(E event, SerializableBiConsumer<? super E, Exception> errorHandler) Fires an event to the listeners registered for its exact type in reverse registration order, handing a listener that threw to the given error handler instead of logging it.The listeners that have not been notified yet are notified next unless the error handler itself throws, in which case that exception is propagated to the caller and the remaining listeners are skipped.
- Type Parameters:
E- the event type- Parameters:
event- the event to fire, notnullerrorHandler- invoked with the event and the exception when a listener throws, notnull
-