Skip to content

In-memory event store

Plain Java · Modules: occurrent-eventstore-inmemory

A simple in-memory implementation of the EventStore interface.

Mainly for testing purposes or for integration tests that doesn’t require a durable event store.

<dependency>
<groupId>org.occurrent</groupId>
<artifactId>occurrent-eventstore-inmemory</artifactId>
<version>0.34.0</version>
</dependency>

Once you’ve imported the dependencies you create a new instance of org.occurrent.eventstore.inmemory.InMemoryEventStore. For example:

InMemoryEventStore eventStore = new InMemoryEventStore();

This implementation also supports filtered stream reads via org.occurrent.eventstore.api.blocking.ReadEventStreamWithFilter. See Stream Filtering for when and how to use it.

By default this store refuses a query that filters on the event data payload. Call withDataFieldReader(...) to give it one, see Filtering on Payload Data.

Now you can start reading and writing events to the EventStore:

CloudEvent event = CloudEventBuilder.v1()
.withId("eventId")
.withSource(URI.create("urn:mydomain"))
.withType("HelloWorld")
.withTime(LocalDateTime.now().atOffset(ZoneOffset.UTC))
.withSubject("subject")
.withDataContentType("application/json")
.withData("{ \"message\" : \"hello\" }".getBytes(StandardCharsets.UTF_8))
.build();
// Write
eventStore.write("streamId", List.of(event));
// Read
EventStream<CloudEvent> eventStream = eventStore.read("streamId");
// Read filtered events when the store supports it
if (eventStore instanceof ReadEventStreamWithFilter filteredEventStore) {
EventStream<CloudEvent> filteredEventStream = filteredEventStore.read(
"streamId",
StreamReadFilter.type("com.example.NameDefined")
);
}