Class LocalEventBus<EVENT_TYPE>
- java.lang.Object
-
- dk.cloudcreate.essentials.reactive.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 Summary
Constructors Constructor Description LocalEventBus(String busName, int parallelThreads, OnErrorHandler<EVENT_TYPE> onErrorHandler)Create aLocalEventBuswith the given name, the given number of parallel asynchronous processing threadsLocalEventBus(String busName, reactor.core.scheduler.Scheduler asyncSubscribersScheduler, OnErrorHandler<EVENT_TYPE> onErrorHandler)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 LocalEventBus<EVENT_TYPE>addAsyncSubscriber(Consumer<EVENT_TYPE> subscriber)Add an asynchronous subscriber/consumerLocalEventBus<EVENT_TYPE>addSyncSubscriber(Consumer<EVENT_TYPE> subscriber)Add a synchronous subscriber/consumerLocalEventBus<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.LocalEventBus<EVENT_TYPE>removeAsyncSubscriber(Consumer<EVENT_TYPE> subscriber)Remove an asynchronous subscriber/consumerLocalEventBus<EVENT_TYPE>removeSyncSubscriber(Consumer<EVENT_TYPE> subscriber)Remove a synchronous subscriber/consumerStringtoString()
-
-
-
Constructor Detail
-
LocalEventBus
public LocalEventBus(String busName, int parallelThreads, OnErrorHandler<EVENT_TYPE> 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, reactor.core.scheduler.Scheduler asyncSubscribersScheduler, OnErrorHandler<EVENT_TYPE> 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 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
-
-