Akka context become recursive function

家住魔仙堡 提交于 2021-02-07 07:28:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!