Class LocalEventBus

  • All Implemented Interfaces:
    EventBus

    public 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:
    {@code
     public class OrderEventsHandler extends AnnotatedEventHandler {
    See Also:
    AnnotatedEventHandler
    • Constructor Detail

      • LocalEventBus

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

        public LocalEventBus​(String busName)
        Create a LocalEventBus with the given name, using system available processors of parallel asynchronous processing threads
        Parameters:
        busName - the name of the bus
      • LocalEventBus

        public LocalEventBus​(String busName,
                             Optional<OnErrorHandler> optionalOnErrorHandler)
        Create a LocalEventBus with the given name, using system available processors of parallel asynchronous processing threads
        Parameters:
        busName - the name of the bus
        optionalOnErrorHandler - optional error handler which will be called if any subscriber/consumer fails to handle an event
        If Optional.empty(), a default error logging handler is used
      • LocalEventBus

        public LocalEventBus​(String busName,
                             int parallelThreads,
                             Optional<OnErrorHandler> optionalOnErrorHandler)
        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
        optionalOnErrorHandler - optional error handler which will be called if any subscriber/consumer fails to handle an event
        If Optional.empty(), a default error logging handler is used
      • LocalEventBus

        public LocalEventBus​(String busName,
                             int parallelThreads,
                             OnErrorHandler 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,
                             int parallelThreads)
        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
      • LocalEventBus

        public LocalEventBus​(String busName,
                             reactor.core.scheduler.Scheduler asyncSubscribersScheduler,
                             Optional<OnErrorHandler> optionalOnErrorHandler)
        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)
        optionalOnErrorHandler - optional error handler which will be called if any subscriber/consumer fails to handle an event
        If Optional.empty(), a default error logging handler is used
      • LocalEventBus

        public LocalEventBus​(String busName,
                             reactor.core.scheduler.Scheduler asyncSubscribersScheduler,
                             OnErrorHandler 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 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