我有一个执着的演员。当它第一次启动(数据库为空)时,我会保留一些初始数据。但是状态并没有像我预期的那样得到更新。它仅在处理第一条消息后才更新。如何让演员在状态更新后开始处理消息?
演员代码
class TestActor extends PersistentActor {
var numberOfEvents = 0
def updateState(e: Any): Unit = {
println("updating")
numberOfEvents += 1
}
override def receiveRecover: Receive = {
case RecoveryCompleted =>
if (numberOfEvents == 0) {
println("persisting")
persist("foo")(updateState)
}
}
override def receiveCommand: Receive = {
case _ => {
println("answering")
sender ! numberOfEvents
}
}
}
测试代码
Await.result(actorRef ? "stats", Duration.Inf) shouldBe 0 // I wan't 1 here
Await.result(actorRef ? "stats", Duration.Inf) shouldBe 1
输出
persisting
answering // why this goes before updating?
updating
answering