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
Visualizes the transition from a validated command to a stored event. section 9.2
