【发布时间】:2017-07-19 21:09:00
【问题描述】:
我正在学习如何使用 context.become 来控制我的演员的状态,我正在使用以下代码:
class MyActor extends Actor {
override def receive: Receive = {
println("Happens here")
active(Set.empty)
}
def active(isInSet: Set[String]): Receive = {
case Add(key) =>
context.become(active(isInSet+key))
case Contains(key) =>
sender() ! isInSet(key)
case ShowAll =>
println(isInSet.toSeq)
}
}
case class Add(key: String)
case class Contains(key: String)
object ShowAll
object DemoBecome extends App{
override def main(args: Array[String]): Unit = {
val system = ActorSystem("BecomeUnbecome")
val act = system.actorOf(Props(classOf[MyActor]), "demoActor")
act ! Add("1")
act ! ShowAll
act ! Add("2")
act ! ShowAll
Thread.sleep(10000)
System.exit(0)
}
当我发送第一条消息时,“接收”工作并打印消息,在第二条消息不显示后,这是我的输出:
Happens here
Set()
Vector(1)
Set(1)
Vector(1, 2)
如果我更改接收方法,为此:
def receive = {
case a: Add => println("happens here Add" )
case c: Contains => println("happens here Contains")
case ShowAll => println("happens here Show")
}
我收到这个输出:
happens here Add
happens here Show
happens here Add
happens here Show
所以我尝试跟踪“接收”被“阻止”的时刻,但我没有成功,我的疑问是:当我在我的演员中使用 context.become 时,Akka 如何以及何时处理之后的消息第一个?
【问题讨论】: