Class LocalEventBus
- java.lang.Object
-
- dk.cloudcreate.essentials.reactive.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:
If you wish to colocate multiple related Event handling methods inside the same class and use it together with theLocalEventBus localEventBus = new LocalEventBus("TestBus", 3, (failingSubscriber, event, exception) -> log.error("....")); localEventBus.addAsyncSubscriber(orderEvent -> { ... }); localEventBus.addSyncSubscriber(orderEvent -> { ... }); localEventBus.publish(new OrderCreatedEvent());LocalEventBusthen you can extend theAnnotatedEventHandlerclass:
{@code public class OrderEventsHandler extends AnnotatedEventHandler {- See Also:
AnnotatedEventHandler
-
-
Constructor Summary
Constructors Constructor Description LocalEventBus(String busName)Create aLocalEventBuswith the given name, using system available processors of parallel asynchronous processing threadsLocalEventBus(String busName, int parallelThreads)Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threadsLocalEventBus(String busName, int parallelThreads, OnErrorHandler onErrorHandler)Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threadsLocalEventBus(String busName, int parallelThreads, Optional<OnErrorHandler> optionalOnErrorHandler)Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threadsLocalEventBus(String busName, OnErrorHandler onErrorHandler)Create aLocalEventBuswith the given name, using system available processors of parallel asynchronous processing threadsLocalEventBus(String busName, Optional<OnErrorHandler> optionalOnErrorHandler)Create aLocalEventBuswith the given name, using system available processors of parallel asynchronous processing threadsLocalEventBus(String busName, reactor.core.scheduler.Scheduler asyncSubscribersScheduler, OnErrorHandler onErrorHandler)Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threadsLocalEventBus(String busName, reactor.core.scheduler.Scheduler asyncSubscribersScheduler, Optional<OnErrorHandler> optionalOnErrorHandler)Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threads
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EventBusaddAsyncSubscriber(EventHandler subscriber)Add an asynchronous subscriber/consumerEventBusaddSyncSubscriber(EventHandler subscriber)Add a synchronous subscriber/consumerbooleanhasAsyncSubscriber(EventHandler subscriber)booleanhasSyncSubscriber(EventHandler subscriber)EventBuspublish(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.EventBusremoveAsyncSubscriber(EventHandler subscriber)Remove an asynchronous subscriber/consumerEventBusremoveSyncSubscriber(EventHandler subscriber)Remove a synchronous subscriber/consumerStringtoString()
-
-
-
Constructor Detail
-
LocalEventBus
public LocalEventBus(String busName, OnErrorHandler onErrorHandler)
Create aLocalEventBuswith the given name, using system available processors of parallel asynchronous processing threads- Parameters:
busName- the name of the busonErrorHandler- the error handler which will be called if any subscriber/consumer fails to handle an event
-
LocalEventBus
public LocalEventBus(String busName)
Create aLocalEventBuswith 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 aLocalEventBuswith the given name, using system available processors of parallel asynchronous processing threads- Parameters:
busName- the name of the busoptionalOnErrorHandler- optional error handler which will be called if any subscriber/consumer fails to handle an event
IfOptional.empty(), a default error logging handler is used
-
LocalEventBus
public LocalEventBus(String busName, int parallelThreads, Optional<OnErrorHandler> optionalOnErrorHandler)
Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threads- Parameters:
busName- the name of the busparallelThreads- the number of parallel asynchronous processing threadsoptionalOnErrorHandler- optional error handler which will be called if any subscriber/consumer fails to handle an event
IfOptional.empty(), a default error logging handler is used
-
LocalEventBus
public LocalEventBus(String busName, int parallelThreads, OnErrorHandler onErrorHandler)
Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threads- Parameters:
busName- the name of the busparallelThreads- the number of parallel asynchronous processing threadsonErrorHandler- the error handler which will be called if any subscriber/consumer fails to handle an event
-
LocalEventBus
public LocalEventBus(String busName, int parallelThreads)
Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threads- Parameters:
busName- the name of the busparallelThreads- the number of parallel asynchronous processing threads
-
LocalEventBus
public LocalEventBus(String busName, reactor.core.scheduler.Scheduler asyncSubscribersScheduler, Optional<OnErrorHandler> optionalOnErrorHandler)
Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threads- Parameters:
busName- the name of the busasyncSubscribersScheduler- 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
IfOptional.empty(), a default error logging handler is used
-
LocalEventBus
public LocalEventBus(String busName, reactor.core.scheduler.Scheduler asyncSubscribersScheduler, OnErrorHandler onErrorHandler)
Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threads- Parameters:
busName- the name of the busasyncSubscribersScheduler- 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:EventBusPublish 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)
-
addAsyncSubscriber
public EventBus addAsyncSubscriber(EventHandler subscriber)
Description copied from interface:EventBusAdd an asynchronous subscriber/consumer- Specified by:
addAsyncSubscriberin interfaceEventBus- Parameters:
subscriber- the subscriber to add- Returns:
- this bus instance
-
removeAsyncSubscriber
public EventBus removeAsyncSubscriber(EventHandler subscriber)
Description copied from interface:EventBusRemove an asynchronous subscriber/consumer- Specified by:
removeAsyncSubscriberin interfaceEventBus- Parameters:
subscriber- the subscriber to remove- Returns:
- this bus instance
-
addSyncSubscriber
public EventBus addSyncSubscriber(EventHandler subscriber)
Description copied from interface:EventBusAdd a synchronous subscriber/consumer- Specified by:
addSyncSubscriberin interfaceEventBus- Parameters:
subscriber- the subscriber to add- Returns:
- this bus instance
-
removeSyncSubscriber
public EventBus removeSyncSubscriber(EventHandler subscriber)
Description copied from interface:EventBusRemove a synchronous subscriber/consumer- Specified by:
removeSyncSubscriberin interfaceEventBus- Parameters:
subscriber- the subscriber to remove- Returns:
- this bus instance
-
hasSyncSubscriber
public boolean hasSyncSubscriber(EventHandler subscriber)
- Specified by:
hasSyncSubscriberin interfaceEventBus
-
hasAsyncSubscriber
public boolean hasAsyncSubscriber(EventHandler subscriber)
- Specified by:
hasAsyncSubscriberin interfaceEventBus
-
-