Defining query-side event handlers in Java

The Eventuate client framework for Java supports a number of ways to implement event handlers. The simplest approach is to use the high-level API.

Using the high-level API

The high-level API hides a lot of the boilerplate code you would otherwise have to write. When using this API you define one or more event handler Spring/Micronaut beans. Each Bean defines one or more event handler methods, one for each type of event the handler processes. The framework subscribes to the events and dispatches each event to the appropriate event handler method.

Here is an example of a query-side event handler that calls an AccountInfoUpdateService, which updates MongoDB in response to an event.

@EventSubscriber(id="querySideEventHandlers")
public class AccountQueryWorkflow {

  private AccountInfoUpdateService accountInfoUpdateService;

  public AccountQueryWorkflow(AccountInfoUpdateService accountInfoUpdateService) {
    this.accountInfoUpdateService = accountInfoUpdateService;
  }

  @EventHandlerMethod
  public void create(DispatchedEvent<AccountOpenedEvent> de) {
    AccountOpenedEvent event = de.event();
    String id = de.entityId().id();
    String eventId = de.eventId().asString();
    BigDecimal initialBalance = event.getInitialBalance();
    accountInfoUpdateService.create(id, initialBalance, eventId);
  }

Each @EventHandlerMethod method has a DispatchedEvent parameter. DispatchedEvent is a generic type that is parameterized by the event type. It contains the event, the eventId, and the ID and type of the entity that published the event. An event handler method can return either a CompletableFuture<?> or void.

Using the low-level API

Query-side event handlers can also use the low-level event handler API as described here.


Stay in touch
Copyright © 2021 Eventuate, Inc • All rights reserved.