1

使用 akka 演员 - 我正在向更新其状态的演员发送大量消息

for (i <-0 to 100000){
  persistentActor ! Cmd("foo"+i)
}

并像这样使用persistAsync

 val receiveCommand: Receive = {
    case Cmd(data) =>
      persistAsync(Evt(s"${data}-${numEvents}"))(updateState)
    case "snap"  => saveSnapshot(state)
    case "print" => println(state)
  }

如何验证持久性过程是否已完成以便正常关闭系统?

4

1 回答 1

2

您的演员将在快照持久后收到确认:

 var count = 0
 var shutdown = false
 def checkShutDown() = if (shutdown && count == 0) context stop self

 def receive = {
     case "snap"  => count++; saveSnapshot(state)
     case SaveSnapshotSuccess(metadata)         =>
         count --; checkShutDown()
     case SaveSnapshotFailure(metadata, reason)  =>
         count --; checkShutDown()
     case ShutDown => 
         shutdown = true; checkShutDown()
 }

最后,您可以使用The Reaper 模式在演员关闭后关闭您的系统。

于 2015-02-04T11:07:09.080 回答