Class LocalEventBus
java.lang.Object
dk.cloudcreate.essentials.reactive.LocalEventBus
- All Implemented Interfaces:
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:
Example of registering the
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:
-
Constructor Summary
ConstructorsConstructorDescriptionLocalEventBus(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
Modifier and TypeMethodDescriptionaddAsyncSubscriber(EventHandler subscriber) Add an asynchronous subscriber/consumeraddSyncSubscriber(EventHandler subscriber) Add a synchronous subscriber/consumerbooleanhasAsyncSubscriber(EventHandler subscriber) booleanhasSyncSubscriber(EventHandler subscriber) 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.removeAsyncSubscriber(EventHandler subscriber) Remove an asynchronous subscriber/consumerremoveSyncSubscriber(EventHandler subscriber) Remove a synchronous subscriber/consumertoString()
-
Constructor Details
-
LocalEventBus
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 asynchronous subscriber/consumer fails to handle an event
-
LocalEventBus
Create aLocalEventBuswith the given name, using system available processors of parallel asynchronous processing threads- Parameters:
busName- the name of the bus
-
LocalEventBus
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 asynchronous 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 asynchronous subscriber/consumer fails to handle an event
IfOptional.empty(), a default error logging handler is used
-
LocalEventBus
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 asynchronous subscriber/consumer fails to handle an event
-
LocalEventBus
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 asynchronous 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 asynchronous subscriber/consumer fails to handle an event
-
-
Method Details
-
publish
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
Description copied from interface:EventBusAdd an asynchronous subscriber/consumer- Specified by:
addAsyncSubscriberin interfaceEventBus- Parameters:
subscriber- the subscriber to add- Returns:
- this bus instance
-
removeAsyncSubscriber
Description copied from interface:EventBusRemove an asynchronous subscriber/consumer- Specified by:
removeAsyncSubscriberin interfaceEventBus- Parameters:
subscriber- the subscriber to remove- Returns:
- this bus instance
-
addSyncSubscriber
Description copied from interface:EventBusAdd a synchronous subscriber/consumer- Specified by:
addSyncSubscriberin interfaceEventBus- Parameters:
subscriber- the subscriber to add- Returns:
- this bus instance
-
removeSyncSubscriber
Description copied from interface:EventBusRemove a synchronous subscriber/consumer- Specified by:
removeSyncSubscriberin interfaceEventBus- Parameters:
subscriber- the subscriber to remove- Returns:
- this bus instance
-
hasSyncSubscriber
- Specified by:
hasSyncSubscriberin interfaceEventBus
-
hasAsyncSubscriber
- Specified by:
hasAsyncSubscriberin interfaceEventBus
-
toString
-