liveBook · Manning

EventSourcedBehavior

As explained in Akka in Action, Second Edition

EventSourcedBehavior is an Akka Persistence behavior that processes commands, persists events, and rebuilds state by replaying those events.

EventSourcedBehavior is an Akka Persistence behavior for processing commands, persisting events, and rebuilding state by replaying those events. section 9.2.1

Construct an EventSourcedBehavior with four parameters:

  • A unique PersistenceId identifies the persistent actor; Akka Sharding can add entity-type information and prevent identity collisions. section 9.2.1
  • State holds the actor's current state, such as a list of cargo items. section 9.2.1
  • The command handler receives State and Command, then produces an Effect containing events and the resulting state transition. section 9.2.1
  • The event handler applies each Event to State during normal processing and event replay. section 9.2.1

Persistent container

A container actor models CargoAdded events and State(cargos: List[Cargo] = Nil). Commands such as AddCargo and GetCargos provide the write and read interface; persisting an event can be followed by a side effect with Effect.persist(...).thenRun(...). section 9.2.1

How Akka in Action, Second Edition covers it

How deep it goes
Explains the four constructor parameters, identity collisions, state handling, command effects, and event handling through a container example.
Examples
Uses a cargo container with AddCargo, GetCargos, CargoAdded, and a list-based State.
What it assumes
Assumes familiarity with Scala actor behaviors and introduces the persistence-specific commands, events, state, effects, and handlers.
What you can do after
Build it from scratch as a Scala persistent actor with defined commands, events, state, handlers, and a unique PersistenceId.
How it connects
Connects actors, Akka Persistence, event sourcing, commands, events, state, and Akka Sharding.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.