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 beans. Each Spring 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 implements CompoundEventHandler {

  private AccountInfoUpdateService accountInfoUpdateService;

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

  @EventHandlerMethod
  public Observable<Object> 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);
    return Observable.just(null);
  }

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.

Using the low-level API

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


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