Class LocalEventBus<EVENT_TYPE>

  • Type Parameters:
    EVENT_TYPE - the event type being published by the event bus

    public class LocalEventBus<EVENT_TYPE>
    extends Object
    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<OrderEvent> localEventBus    = new LocalEventBus<>("TestBus", 3, (failingSubscriber, event, exception) -> log.error("...."));
    
       localEventBus.addAsyncSubscriber(orderEvent -> {
                 ...
             });
    
       localEventBus.addSyncSubscriber(orderEvent -> {
                   ...
             });
    
         localEventBus.publish(new OrderCreatedEvent());
     
    • Constructor Detail

      • LocalEventBus

        public LocalEventBus​(String busName,
                             int parallelThreads,
                             OnErrorHandler<EVENT_TYPE> onErrorHandler)
        Create a LocalEventBus with the given name, the given number of parallel asynchronous processing threads
        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 subscriber/consumer fails to handle an event
      • LocalEventBus

        public LocalEventBus​(String busName,
                             reactor.core.scheduler.Scheduler asyncSubscribersScheduler,
                             OnErrorHandler<EVENT_TYPE> onErrorHandler)
        Create a LocalEventBus with the given name, the given number of parallel asynchronous processing threads
        Parameters:
        busName - the name of the bus
        asyncSubscribersScheduler - the asynchronous event scheduler (for the asynchronous consumers/subscribers)
        onErrorHandler - the error handler which will be called if any subscriber/consumer fails to handle an event
    • Method Detail

      • publish

        public LocalEventBus<EVENT_TYPE> publish​(EVENT_TYPE 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

        public LocalEventBus<EVENT_TYPE> addAsyncSubscriber​(Consumer<EVENT_TYPE> subscriber)
        Add an asynchronous subscriber/consumer
        Parameters:
        subscriber - the subscriber to add
        Returns:
        this bus instance
      • removeAsyncSubscriber

        public LocalEventBus<EVENT_TYPE> removeAsyncSubscriber​(Consumer<EVENT_TYPE> subscriber)
        Remove an asynchronous subscriber/consumer
        Parameters:
        subscriber - the subscriber to remove
        Returns:
        this bus instance
      • addSyncSubscriber

        public LocalEventBus<EVENT_TYPE> addSyncSubscriber​(Consumer<EVENT_TYPE> subscriber)
        Add a synchronous subscriber/consumer
        Parameters:
        subscriber - the subscriber to add
        Returns:
        this bus instance
      • removeSyncSubscriber

        public LocalEventBus<EVENT_TYPE> removeSyncSubscriber​(Consumer<EVENT_TYPE> subscriber)
        Remove a synchronous subscriber/consumer
        Parameters:
        subscriber - the subscriber to remove
        Returns:
        this bus instance