liveBook · Manning

Command

As explained in Akka in Action, Second Edition

A command is a protocol message type representing a request that can be sent to an actor.

A command is a protocol message type representing a request that can be sent to an actor. The actor’s command type defines the messages its protocol exposes. section 3.1.7

Commands establish a typed boundary between actors:

  • Define commands in a sealed Command trait, commonly with case classes carrying request data. section 3.1.7
  • Keep responses separate: messages of type Response do not belong to the actor’s command type and cannot be sent to it. section 3.1.7
  • In event sourcing, treat a command as the intention to change state; validate it before producing and storing an event. section 9.2

Commands in a persistent container

A persistent container can accept AddCargo(cargo) and GetCargos(replyTo) commands. Its commandHandler receives the current State and a Command, then creates an event and new state; an eventHandler applies stored events to rebuild State after restart. section 9.2.1

object Worker {
  
  sealed trait Command                                         #1
  final case class Parse(replyTo: ActorRef[Worker.Response],   #1
text: String)                                              #1
      extends Command                                          #1
 
  sealed trait Response                                        #2
  final case class Done(text: String) extends Response         #2
  ...
}

Shows a typed Worker protocol with Parse as a command and Done as a response, including an explicit reply target. section 3.1.7

Figure 9.5 From command to a stored event
CH09_F05_Abraham2.

Visualizes the transition from a validated command to a stored event. section 9.2

How Akka in Action, Second Edition covers it

How deep it goes
Explains command traits, response separation, validation, event generation, and command-handler inputs without detailing message transport.
Examples
Uses Worker.Parse, Container.AddCargo, and Container.GetCargos to demonstrate protocol messages and persistence commands.
What it assumes
Assumes typed actor protocols and state concepts, while introducing command and response separation through concrete Scala message definitions.
What you can do after
Apply it via Akka actors and Akka Persistence to define requests, validate state changes, and produce stored events.
How it connects
Connects commands with actors, responses, event sourcing, events, state, command handlers, event handlers, 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.