An Event Bus dispatches incoming events to handlers, and provides ways for registering those handlers. One event can be published to more than one subscriber.
To receive events a handler object should:
- Expose a public method that accepts the type of the event as the first parameter and EventContext as the second parameter;
- Mark the method with
Register with an instance of EventBus using
Note: Since Protobuf messages are final classes, a handler method cannot accept just
Messageas the first parameter. It must be an exact type of the event that needs to be handled.
The execution of handler methods is performed by an
Executor associated with the instance of the Event Bus.
If a handler method throws an exception which is not a Business Failure, the exception is logged.
If there is no handler for the posted event, the fact is logged as warning, with no further processing.
// Initialize Event Bus. InMemoryStorageFactory storageFactory = InMemoryStorageFactory.getInstance(); EventStore eventStore = EventStore.newBuilder() .setStreamExecutor(MoreExecutors.directExecutor()) .setStorage(storageFactory.createEventStorage()) .setLogger(EventStore.log()) .build(); EventBus eventBus = EventBus.newInstance(eventStore); // Register event handlers (see `Event Handler` section on how to define them). eventBus.subscribe(new MyEventSubscriber()); // Post an event. MyEvent eventMessage = MyEvent.newBuilder().setSmth(smth).build(); Event event = Events.createEvent(eventMessage, eventContext); eventBus.post(event);
An Event Bus also allows a subscriber to catch-up from a given timestamp on a certain event.
TODO: code sample