问题
I have an actor in which I mutate the state using context.become: Here is the snippet:
def stateMachine(state: State): Receive = {
case a => {
... do something
context.become(stateMachine(newState))
}
case b => {
... do something
sender ! state
}
case c => {
... do something
context.become(stateMachine(newState))
}
}
My IntelliJ says that my stateMachine(...) function is recursive. Is this a problem? Should I be concerned? Is there something fundamentally wrong with my approach in the above example?
回答1:
The approach you are using is fine, it is a common way to implement state inside an Actor without using var. The default version of context.become
does not maintain a stack, it just replaces the existing functionality with the new one. The is called "HotSwap". To maintain a stack, you would have to add discardOld = false
.
http://doc.akka.io/docs/akka/snapshot/scala/actors.html#become-unbecome
http://doc.akka.io/docs/akka/1.3.1/scala/actors.html#actor-hotswap
来源:https://stackoverflow.com/questions/32122077/akka-context-become-recursive-function