2
我有一個執着的演員。當它第一次啓動時(數據庫爲空),我堅持一些初始數據。但是這個州並沒有像我期望的那樣得到更新。它只有在處理完第一條消息後纔會更新。我怎樣才能讓演員在狀態更新後開始處理消息?阿卡堅持恢復完成更新狀態後第一條消息
演員代碼
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
要考慮Full code