Class VaadinServiceEventBus

java.lang.Object
com.vaadin.flow.server.VaadinServiceEventBus
All Implemented Interfaces:
Serializable

public class VaadinServiceEventBus extends Object implements Serializable
An event bus for 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 Details

    • VaadinServiceEventBus

      public VaadinServiceEventBus(VaadinService service)
      Creates an event bus for the given service.
      Parameters:
      service - the service owning this event bus, not null
  • Method Details

    • getService

      public VaadinService 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, not null
      listener - the listener to call when an event of the given type is fired, not null
      Returns:
      a handle that can be used for removing the listener
    • hasListener

      public boolean hasListener(Class<? extends EventObject> eventType)
      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, not null
      Returns:
      true if at least one listener is registered for the event type, false otherwise
    • getListeners

      public <E extends EventObject> Collection<SerializableConsumer<E>> getListeners(Class<E> eventType)
      Gets the listeners registered for the given event type, in registration order.
      Type Parameters:
      E - the event type
      Parameters:
      eventType - the event type, not null
      Returns:
      an unmodifiable collection of listeners, empty if none are registered
    • fireEvent

      public void fireEvent(EventObject event)
      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, not null
    • 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, not null
      errorHandler - invoked with the event and the exception when a listener throws, not null
    • fireEventInReverseOrder

      public void fireEventInReverseOrder(EventObject event)
      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, not null
    • 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, not null
      errorHandler - invoked with the event and the exception when a listener throws, not null