Another way of defining how your actors behave is using a finite-state machine. This is a well-known method to create machines in digital circuits that work as well for Akka actors. An FSM can be described as a set of relations of this form:
We can interpret these relations by saying that if we are in state S and an event E comes, then we will perform actions A and change our state to S'. Akka FSM brings some APIs to easily achieve these relations by defining different behaviors using the when function. This feature can fit several use cases, such as example aggregation of values.
In this recipe, we will explore how to use Akka FSM by implementing a simple traffic light. We will also look at how we can perform action between transitions by providing a partial function to onTransition.