concept entry action in category akka

This is an excerpt from Manning's book Akka in Action.
In this example we show an FSM with two states, State1 and State2. When instantiating the machine, we start in State1, transitioning from the initial state, which is shown in the diagram by the black dot. State1 has two different actions: an entry action and an exit action. (Although we won’t use the exit action in this chapter, we show it so you’ll understand how the model works.) Just as the name says, the first action is executed when the machine sets the state to State1, and the second when the machine changes from State1 to another state. In this example we have only two states, so this action is only executed when it goes to State2. In the next examples, we’ll only use the entry actions, because this is a simple FSM. Exit actions can do some cleaning or restore some state, so they don’t embody part of the logic of the machine. It can be seen more like a finally clause in a try-catch statement, which must always be executed when exiting the try block.
The actual functionality is done by the entry and exit actions, which we’ll implement now. In our FSM model, we had defined several entry actions. Just as the transitions are declared for each state, the actions are also implemented for each state. Figure 11.5 shows the initial state WaitForRequests again, to show the entry action we have to implement. The discreet structure of the implementation code, as you’ll see, also lends itself to unit testing.
Listing 11.5. Implementation of the entry actions
class Inventory(publisher:ActorRef) extends Actor with FSM[State, StateData] { startWith(WaitForRequests, new StateData(0,Seq())) when... onTransition { case _ -> WaitForRequests => { #1 if (!nextStateData.pendingRequests.isEmpty) { // go to next state self ! PendingRequests } } case _ -> WaitForPublisher => { #2 publisher ! PublisherRequest } case _ -> ProcessRequest => { #3 val request = nextStateData.pendingRequests.head reserveId += 1 request.target ! new BookReply(request.context, Right(reserveId)) self ! Done } case _ -> ProcessSoldOut => { #4 nextStateData.pendingRequests.foreach(request => { request.target ! new BookReply(request.context, Left("SoldOut")) }) self ! Done } } } #1 - Entry action to check for pending requests #2 - Entry action to send request to publisher #3 - Entry action to send a reply to sender and signal that processing is done #4 - Entry action to send an error reply to all PendingRequests and signal that processing is done !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!