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

时间:2015-02-04 08:15:21

标签: scala akka akka-persistence

使用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)
  }

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

1 个答案:

答案 0 :(得分: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 pattern关闭系统。