scala 如何等待 Akka 演员系统终止?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12436397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to wait for Akka actor system to terminate?
提问by Rogach
I need to start Akka (2.0) actor system, send in some messages, then wait for it to do heavy lifting. After that, I need to do something unrelated to those actors.
我需要启动 Akka (2.0) 演员系统,发送一些消息,然后等待它完成繁重的工作。在那之后,我需要做一些与那些演员无关的事情。
I tried to wait for all actors to stop with following code:
我试图等待所有演员停止使用以下代码:
val system = new ActorSystem("parallelRunners")
val master = system.actorOf(Props[Master])
master ! Start
system.awaitTermination // <-- hangs here
All actors kill themselves via self ! PoisonPill. What am I doing wrong?
所有演员都通过self ! PoisonPill. 我究竟做错了什么?
回答by Michael Larsen
In Akka 2.4.1 for scala 2.11 it appears to be different again.
在 Scala 2.11 的 Akka 2.4.1 中,它似乎再次不同。
system.awaitTermination()is deprecated and the docs instruct us to use Await.result(system.whenTerminated, timeout)instead.
system.awaitTermination()已弃用,文档指示我们Await.result(system.whenTerminated, timeout)改用。
As 203said, system.terminateis still the way to terminate the system.
正如203所说,system.terminate仍然是终止系统的方式。
Here is some example code I used:
这是我使用的一些示例代码:
val actorSystem = ActorSystem("ActorSystem")
val myActors = actorSystem.actorOf(Props[MyActor].withRouter(RoundRobinPool(10)), "MyActors")
rainbows.foreach(rainbow => myActors ! rainbow)
Await.ready(actorSystem.whenTerminated, Duration(1, TimeUnit.MINUTES))
Then in the MyActor class I have the line context.system.terminate()
然后在 MyActor 类中,我有一行 context.system.terminate()
回答by 203
As of Akka 2.4, you should use system.awaitTermination()which returns a Future[Terminated]you can wait for.
从 Akka 2.4 开始,您应该使用system.awaitTermination()which 返回Future[Terminated]您可以等待的。
In order to terminate the system, you should use ActorSystem.terminate(e.g. context.system.terminate()from within an actor when it is finished.
为了终止系统,您应该使用ActorSystem.terminate(例如context.system.terminate(),当它完成时从一个actor 中使用。
Source: Release Notes
来源:发行说明
回答by Sebastian Kruse
Just a side note for those running into this question for its title: Apparently, Akka 2.5 does not support actorSystem.awaitTerminationanymore. Reason being why might be Akka's philosophy of avoiding any blocking calls. Instead, actorSystem.registerOnTermination(...)can be used as a non-blocking way to do actions while an ActorSystemis being shutdown.
对于那些因其标题而遇到这个问题的人来说,只是一个旁注:显然,Akka 2.5 不再支持actorSystem.awaitTermination了。原因可能是 Akka 避免任何阻塞调用的哲学。相反,actorSystem.registerOnTermination(...)可以用作ActorSystem在关闭时执行操作的非阻塞方式。
Nonetheless, you can still wait for your actor system to complete via a Futureprovided by ActorSystem.whenTerminated:
尽管如此,您仍然可以通过以下Future提供的方式等待您的演员系统完成ActorSystem.whenTerminated:
val system = new ActorSystem("parallelRunners")
val master = system.actorOf(Props[Master])
master ! Start
import scala.concurrent.Await
import scala.concurrent.duration._
Await.ready(system.whenTerminated, 365.days)
回答by Rogach
I found the solution - just call system.shutdown from the master actor:
我找到了解决方案 - 只需从主角色调用 system.shutdown :
context.system.shutdown
回答by Ben Schmidt
You can also kill the ActorSystemfrom the main thread by using system.shutdown. But that operation is asynchronous. You only need to use system.awaitTerminationif you need to block the main thread until it completes. If your parallelRunners return something useful for the rest of the program, then it would probably be easiest to not block and continue your program.
您还可以ActorSystem使用system.shutdown. 但该操作是异步的。仅system.awaitTermination当需要阻塞主线程直到完成时才需要使用。如果您的 parallelRunner 返回对程序的其余部分有用的东西,那么不阻塞并继续您的程序可能是最简单的。
回答by matanster
in Akka 2.3.9, it seems that shutting down an actor system and waiting for it to shut down is a two step process:
在 Akka 2.3.9 中,关闭 actor 系统并等待它关闭似乎是一个两步过程:
- Initiate the shutdown:
actorSystem.shutdown - Wait for its termination in a blocking manner:
actorSystem.awaitTermination
- 启动关机:
actorSystem.shutdown - 以阻塞方式等待其终止:
actorSystem.awaitTermination
As an alternative to step (2), possibly (haven't tested these alternatives) you may alternatively poll on isTerminatedor use registerOnTerminationfor running some code when it's terminated. So it is worth going through the comments of akka.actor.ActorSystem to fathom and choose between these methods for your implementation.
作为步骤 (2) 的替代方案,可能(尚未测试这些替代方案)您可以选择轮询isTerminated或registerOnTermination用于在终止时运行某些代码。因此,值得通过 akka.actor.ActorSystem 的评论来理解和选择这些方法以用于您的实现。
Perhaps I'm missing other options about the API (?) as Futurereturn values could have been nicer.
也许我错过了关于 API (?) 的其他选项,因为Future返回值本来可以更好。
回答by Osskar Werrewka
How about:
怎么样:
import scala.concurrent.Await
import scala.concurrent.duration._
Await.ready(system.terminate(), 5.seconds)
terminate returns a Future:
终止返回一个未来:
def terminate(): Future[Terminated]
and you can just await completion of this future.
你可以等待这个未来的完成。

