Interface EventBus
- All Known Implementing Classes:
LocalEventBus
public interface EventBus
Simple event bus concept that supports both synchronous and asynchronous subscribers that are registered and listening for events published
Usage example:
Example of registering the
Usage 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:
-
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/consumer
-
Method Details
-
publish
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
Add an asynchronous subscriber/consumer- Parameters:
subscriber- the subscriber to add- Returns:
- this bus instance
-
removeAsyncSubscriber
Remove an asynchronous subscriber/consumer- Parameters:
subscriber- the subscriber to remove- Returns:
- this bus instance
-
addSyncSubscriber
Add a synchronous subscriber/consumer- Parameters:
subscriber- the subscriber to add- Returns:
- this bus instance
-
removeSyncSubscriber
Remove a synchronous subscriber/consumer- Parameters:
subscriber- the subscriber to remove- Returns:
- this bus instance
-
hasSyncSubscriber
-
hasAsyncSubscriber
-