All Superinterfaces:
dk.cloudcreate.essentials.shared.Lifecycle
All Known Implementing Classes:
LocalEventBus

public interface EventBus extends dk.cloudcreate.essentials.shared.Lifecycle
Simple event bus concept that supports both synchronous and asynchronous subscribers that are registered and listening for events published

Usage 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:
  • Method Details

    • publish

      EventBus publish(Object event)
      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)
      Parameters:
      event - the event to publish
      Returns:
      this bus instance
    • addAsyncSubscriber

      EventBus addAsyncSubscriber(EventHandler subscriber)
      Add an asynchronous subscriber/consumer
      Parameters:
      subscriber - the subscriber to add
      Returns:
      this bus instance
    • removeAsyncSubscriber

      EventBus removeAsyncSubscriber(EventHandler subscriber)
      Remove an asynchronous subscriber/consumer
      Parameters:
      subscriber - the subscriber to remove
      Returns:
      this bus instance
    • addSyncSubscriber

      EventBus addSyncSubscriber(EventHandler subscriber)
      Add a synchronous subscriber/consumer
      Parameters:
      subscriber - the subscriber to add
      Returns:
      this bus instance
    • removeSyncSubscriber

      EventBus removeSyncSubscriber(EventHandler subscriber)
      Remove a synchronous subscriber/consumer
      Parameters:
      subscriber - the subscriber to remove
      Returns:
      this bus instance
    • hasSyncSubscriber

      boolean hasSyncSubscriber(EventHandler subscriber)
    • hasAsyncSubscriber

      boolean hasAsyncSubscriber(EventHandler subscriber)