7

如何优雅地重启整个演员系统?换句话说,停止系统中的所有参与者gracefulStop(或至少是重要的参与者!)

实际上,我想在测试期间重新启动应用程序,而无需每次启动新的单独 JVM 的开销。

我正在使用 Akka 2.0.5。

4

2 回答 2

6

Whenever you have implemented an actor system and it is lacking one function, don’t hesitate to add one more actor to take care of that: in this case create one actor whose job is it to send graceful stop requests to your important actors and await their termination. That actor can then shut down the actor system, while from the outside you awaitTermination. If you created that actor with system.actorOf(Props[Terminator], "terminator"), then you can shut it down with

system.actorFor("/user/terminator") ! MakeItStop // or whatever message you choose
try system.awaitTermination(1.minute) // or however long it may take
catch {
  case _: TimeoutException => system.shutdown() // as a last resort
}
于 2013-01-26T13:28:32.103 回答
1

如果是用于单元测试,您可以停止 ActorSystem 并启动一个新的。

val system1 = ActorSystem("system1")
system1.shutdown() // This is async and will return before the system actually stops.

val system2 = ActorSystem("system2")

我不确定如何重新启动系统(实际上,我不确定上述方法是否可行,但我认为没有理由不这样做)。

于 2013-01-23T14:12:32.683 回答