Skip to content

Event Repository

The framework module provides helper classes, encapsulating the persistence of Java events. This relieves the user from:

The EventReader interface defines operations for reading events from the EventSourcingDB, including:

  • reading raw Events
  • reading upcasted Events
  • reading Java event objects and their optional meta-data from EventData

The ImmediateEventPublisher interface defines operations for publishing events to the EventSourcingDB, including:

  • atomically publishing one or more Java event objects and their optional meta-data
  • specifying additional preconditions which must be fulfilled when publishing

All these operations are implemented within the EventRepository class. Suitable framework exceptions are thrown in case of an error.

Configuration

An instance of EventRepository can be obtained, either by manually instantiating it or using Spring Boot autoconfiguration.

Tip

In either case it is required to make sure Jackson Databind is included as dependency, unless a custom EventDataMarshaller implementation is used.

Manual Configuration

An instance of EventRepository can be created providing the necessary configuration properties:

The following example shows, how to instantiate an EventRepository using a provided EsdbClient instance, the built-in ClassNameEventTypeResolver, the built-in JacksonEventDataMarshaller, and an empty instance of EventUpcasters (i.e. containing no upcasters):

import com.fasterxml.jackson.databind.ObjectMapper;
import com.opencqrs.esdb.client.EsdbClient;
import com.opencqrs.esdb.client.Option;
import com.opencqrs.framework.serialization.JacksonEventDataMarshaller;
import com.opencqrs.framework.types.ClassNameEventTypeResolver;
import com.opencqrs.framework.upcaster.EventUpcasters;

import java.util.Set;

public class EventRepositoryConfiguration {

    public static EventRepository eventRepository(EsdbClient esdbClient) {
        return new EventRepository(
                esdbClient,
                new EventSource("http://my-service.com"),
                new ClassNameEventTypeResolver(EventRepositoryConfiguration.class.getClassLoader()),
                new JacksonEventDataMarshaller(new ObjectMapper()),
                new EventUpcasters()
        );
    }
}

The correct configuration can be confirmed, for instance by calling readAsObject() as follows:

public static void main(String[] args){
    EsdbClient client = ...;

    EventRepositoryConfiguration
            .eventRepository(client)
            .readAsObject("/books", Set.of(new Option.Recursive()))
            .forEach(System.out::println);
}

Spring Boot Auto-Configuration

For Spring Boot applications using the framework-spring-boot-starter module and Jackson Databind EventPersistenceAutoConfiguration provides both an EventSource Spring bean configured based on the spring.application.name and a fully configured EventRepository Spring bean.

With that configuration in place the auto-configured EventRepository instance can be auto-wired within any other Spring bean, if needed. The configuration can be further customized by: