In-memory event store
What is it?
Section titled “What is it?”A simple in-memory implementation of the EventStore interface.
When to use?
Section titled “When to use?”Mainly for testing purposes or for integration tests that doesn’t require a durable event store.
Dependencies
Section titled “Dependencies”<dependency> <groupId>org.occurrent</groupId> <artifactId>occurrent-eventstore-inmemory</artifactId> <version>0.34.0</version></dependency>compile 'org.occurrent:occurrent-eventstore-inmemory:0.34.0'libraryDependencies += "org.occurrent" % "occurrent-eventstore-inmemory" % "0.34.0"@Grab(group='org.occurrent', module='occurrent-eventstore-inmemory', version='0.34.0')[org.occurrent/occurrent-eventstore-inmemory "0.34.0"]'org.occurrent:occurrent-eventstore-inmemory:jar:0.34.0'<dependency org="org.occurrent" name="occurrent-eventstore-inmemory" rev="0.34.0" />Getting Started
Section titled “Getting Started”Once you’ve imported the dependencies you create a new instance of org.occurrent.eventstore.inmemory.InMemoryEventStore. For example:
InMemoryEventStore eventStore = new InMemoryEventStore();val eventStore = 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();
// WriteeventStore.write("streamId", List.of(event));
// ReadEventStream<CloudEvent> eventStream = eventStore.read("streamId");
// Read filtered events when the store supports itif (eventStore instanceof ReadEventStreamWithFilter filteredEventStore) { EventStream<CloudEvent> filteredEventStream = filteredEventStore.read( "streamId", StreamReadFilter.type("com.example.NameDefined") );}val 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\" }".toByteArray()) .build()
// WriteeventStore.write("streamId", listOf(event))
// Readval eventStream : EventStream<CloudEvent> = eventStore.read("streamId")
// Read filtered events when the store supports itval filteredEventStore = eventStore as? ReadEventStreamWithFilterval filteredEventStream = filteredEventStore?.read( "streamId", StreamReadFilter.type(NameDefined::class.java.name))