Events from Apache Pekko Persistence

A typical source for Projections is events stored with @apidoc[EventSourcedBehavior$] in @extref:Apache Pekko Persistence. Events can be @extref:tagged and then consumed with the @extref:eventsByTag query.

Apache Pekko Projections has integration with eventsByTag, which is described here.

Dependencies

To use the Event Sourced module of Apache Pekko Projections add the following dependency in your project:

@@dependency [sbt,Maven,Gradle] { group=org.apache.pekko artifact=pekko-projection-eventsourced_$scala.binary.version$ version=$project.version$ }

Apache Pekko Projections require Pekko $pekko.version$ or later, see @ref:Pekko version.

@@project-info{ projectId=“eventsourced” }

Transitive dependencies

The table below shows pekko-projection-eventsourced's direct dependencies and the second tab shows all libraries it depends on transitively.

@@dependencies{ projectId=“eventsourced” }

SourceProvider for eventsByTag

A @apidoc[SourceProvider] defines the source of the event envelopes that the Projection will process. A SourceProvider for the eventsByTag query can be defined with the @apidoc[EventSourcedProvider$] like this:

Scala : @@snip EventSourcedDocExample.scala { #eventsByTagSourceProvider }

Java : @@snip EventSourcedDocExample.java { #eventsByTagSourceProvider }

This example is using the Cassandra plugin for Apache Pekko Persistence, but same code can be used for other Pekko Persistence plugins by replacing the CassandraReadJournal.Identifier. For example the JDBC plugin can be used. You will use the same plugin as you have configured for the write side that is used by the EventSourcedBehavior.

This source is consuming all events from the ShoppingCart EventSourcedBehavior that are tagged with "cart-1".

The tags are assigned as described in @ref:Tagging Events in EventSourcedBehavior.

The @scala[EventEnvelope[ShoppingCart.Event]]@java[EventEnvelope<ShoppingCart.Event>] is what the Projection handler will process. It contains the Event and additional meta data, such as the offset that will be stored by the Projection. See @apidoc[pekko.projection.eventsourced.EventEnvelope] for full details of what the envelope contains.

SourceProvider for eventsBySlices

A @apidoc[SourceProvider] defines the source of the event envelopes that the Projection will process. A SourceProvider for the eventsBySlices query can be defined with the @apidoc[EventSourcedProvider$] like this:

Scala : @@snip EventSourcedDocExample.scala { #eventsBySlicesSourceProvider }

Java : @@snip EventSourcedDocExample.java { #eventsBySlicesSourceProvider }

This example is using the @extref:R2DBC plugin for Apache Pekko Persistence. You will use the same plugin as you have configured for the write side that is used by the EventSourcedBehavior.

This source is consuming all events from the ShoppingCart EventSourcedBehavior for the given slice range. In a production application, you would need to start as many instances as the number of slice ranges. That way you consume the events from all entities.

The @scala[EventEnvelope[ShoppingCart.Event]]@java[EventEnvelope<ShoppingCart.Event>] is what the Projection handler will process. It contains the Event and additional meta data, such as the offset that will be stored by the Projection. See @apidoc[pekko.persistence.query.typed.EventEnvelope] for full details of what the envelope contains.