java.lang.Object
dk.cloudcreate.essentials.reactive.LocalEventBus
All Implemented Interfaces:
EventBus, dk.cloudcreate.essentials.shared.Lifecycle

public final class LocalEventBus extends Object implements EventBus
Simple event bus that supports both synchronous and asynchronous subscribers that are registered and listening for events published within the local the JVM
You can have multiple instances of the LocalEventBus deployed with the local JVM, but usually one event bus is sufficient.

Example:

  LocalEventBus localEventBus    = new LocalEventBus("TestBus", 3, (failingSubscriber, event, exception) -> log.error("...."));

   localEventBus.addAsyncSubscriber(orderEvent -> {
             ...
         });

   localEventBus.addSyncSubscriber(orderEvent -> {
               ...
         });

   localEventBus.publish(new OrderCreatedEvent());
 
If you wish to colocate multiple related Event handling methods inside the same class and use it together with the LocalEventBus then you can extend the AnnotatedEventHandler class:

 public class OrderEventsHandler extends AnnotatedEventHandler {

     @Handler
     void handle(OrderCreated event) {
     }

     @Handler
     void handle(OrderCancelled event) {
     }
 }

Example of registering the AnnotatedEventHandler with the LocalEventBus:

 LocalEventBus localEventBus    = new LocalEventBus("TestBus", 3, (failingSubscriber, event, exception) -> log.error("...."));
 localEventBus.addAsyncSubscriber(new OrderEventsHandler(...));
 localEventBus.addSyncSubscriber(new OrderEventsHandler(...));
 
See Also:
  • Field Details

    • DEFAULT_BACKPRESSURE_BUFFER_SIZE

      public static final int DEFAULT_BACKPRESSURE_BUFFER_SIZE
      See Also:
    • DEFAULT_OVERFLOW_MAX_RETRIES

      public static final int DEFAULT_OVERFLOW_MAX_RETRIES
      See Also:
    • QUEUED_TASK_CAP_FACTOR

      public static final double QUEUED_TASK_CAP_FACTOR
      See Also:
  • Constructor Details

    • LocalEventBus

      public LocalEventBus(String busName, int parallelThreads, int backpressureBufferSize, OnErrorHandler onErrorHandler, int overflowMaxRetries, double queuedTaskCapFactor)
      Create a LocalEventBus with the given parameters. After creation the LocalEventBus is already started!
      Parameters:
      busName - the name of the bus
      parallelThreads - the number of parallel asynchronous processing threads
      backpressureBufferSize - the backpressure size for Sinks.Many's onBackpressureBuffer size
      onErrorHandler - the error handler which will be called if any asynchronous subscriber/consumer fails to handle an event
      overflowMaxRetries - the maximum number of retries for events that overflow the Flux
      queuedTaskCapFactor - the factor to calculate queued task capacity from the backpressureBufferSize
    • LocalEventBus

      public LocalEventBus(String busName)
      Create a LocalEventBus with the given name and default settings.
      Parameters:
      busName - the name of the bus
    • LocalEventBus

      public LocalEventBus(String busName, OnErrorHandler onErrorHandler)
      Create a LocalEventBus with the given name and error handler.
      Parameters:
      busName - the name of the bus
      onErrorHandler - the error handler which will be called if any asynchronous subscriber/consumer fails to handle an event
    • LocalEventBus

      public LocalEventBus(String busName, int parallelThreads, OnErrorHandler onErrorHandler)
      Create a LocalEventBus with the given name, number of parallel threads, and error handler.
      Parameters:
      busName - the name of the bus
      parallelThreads - the number of parallel asynchronous processing threads
      onErrorHandler - the error handler which will be called if any asynchronous subscriber/consumer fails to handle an event
    • LocalEventBus

      public LocalEventBus(String busName, int parallelThreads, int backpressureBufferSize)
      Create a LocalEventBus with the given name, number of parallel threads, and backpressure buffer size.
      Parameters:
      busName - the name of the bus
      parallelThreads - the number of parallel asynchronous processing threads
      backpressureBufferSize - The backpressure size for Sinks.Many's onBackpressureBuffer size
  • Method Details

    • builder

      public static LocalEventBus.Builder builder()
    • getName

      public String getName()
    • publish

      public EventBus publish(Object event)
      Description copied from interface: EventBus
      Publish the event to all subscribers/consumer
      First we call all asynchronous subscribers, after which we will call all synchronous subscribers on the calling thread (i.e. on the same thread that the publish method is called on)
      Specified by:
      publish in interface EventBus
      Parameters:
      event - the event to publish
      Returns:
      this bus instance
    • addAsyncSubscriber

      public EventBus addAsyncSubscriber(EventHandler subscriber)
      Description copied from interface: EventBus
      Add an asynchronous subscriber/consumer
      Specified by:
      addAsyncSubscriber in interface EventBus
      Parameters:
      subscriber - the subscriber to add
      Returns:
      this bus instance
    • removeAsyncSubscriber

      public EventBus removeAsyncSubscriber(EventHandler subscriber)
      Description copied from interface: EventBus
      Remove an asynchronous subscriber/consumer
      Specified by:
      removeAsyncSubscriber in interface EventBus
      Parameters:
      subscriber - the subscriber to remove
      Returns:
      this bus instance
    • addSyncSubscriber

      public EventBus addSyncSubscriber(EventHandler subscriber)
      Description copied from interface: EventBus
      Add a synchronous subscriber/consumer
      Specified by:
      addSyncSubscriber in interface EventBus
      Parameters:
      subscriber - the subscriber to add
      Returns:
      this bus instance
    • removeSyncSubscriber

      public EventBus removeSyncSubscriber(EventHandler subscriber)
      Description copied from interface: EventBus
      Remove a synchronous subscriber/consumer
      Specified by:
      removeSyncSubscriber in interface EventBus
      Parameters:
      subscriber - the subscriber to remove
      Returns:
      this bus instance
    • hasSyncSubscriber

      public boolean hasSyncSubscriber(EventHandler subscriber)
      Specified by:
      hasSyncSubscriber in interface EventBus
    • hasAsyncSubscriber

      public boolean hasAsyncSubscriber(EventHandler subscriber)
      Specified by:
      hasAsyncSubscriber in interface EventBus
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • start

      public void start()
      Specified by:
      start in interface dk.cloudcreate.essentials.shared.Lifecycle
    • stop

      public void stop()
      Specified by:
      stop in interface dk.cloudcreate.essentials.shared.Lifecycle
    • isStarted

      public boolean isStarted()
      Specified by:
      isStarted in interface dk.cloudcreate.essentials.shared.Lifecycle