生命週期管理

生命週期鉤子也作為 DSL 元素公開,下面顯示的方法的後續呼叫將替換相應鉤子的內容:

val a = actor(new Act {
  whenStarting { testActor ! "started" }
  whenStopping { testActor ! "stopped" }
})

如果 actor 的邏輯生命週期與重啟週期匹配(即在重啟之前執行停止和之後啟動時),則上述內容就足夠了。如果不需要,請使用以下兩個鉤子:

val a = actor(new Act {
  become {
    case "die" ⇒ throw new Exception
  }
  whenFailing { case m @ (cause, msg) ⇒ testActor ! m }
  whenRestarted { cause ⇒ testActor ! cause }
})