如何等待Akka actor系统终止? [英] How to wait for Akka actor system to terminate?

查看:391
本文介绍了如何等待Akka actor系统终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要启动Akka(2.0)actor系统,发送一些消息,然后等待它完成繁重的工作。之后,我需要做一些与那些参与者无关的事情。

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.

我试图等待所有参与者停止执行以下代码:

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

所有演员通过自我! PoisonPill 。我在做什么错?

All actors kill themselves via self ! PoisonPill. What am I doing wrong?

推荐答案

在Akka 2.4.1中,Scala 2.11似乎又有所不同。

In Akka 2.4.1 for scala 2.11 it appears to be different again.

system.awaitTermination()已过时,文档指示我们使用 Await.result(system。

system.awaitTermination() is deprecated and the docs instruct us to use Await.result(system.whenTerminated, timeout) instead.

作为 203 说, system.terminate 仍然是终止系统的方法。

As 203 said, system.terminate is still the way to terminate the system.

这里是一些示例我使用的代码:

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))

然后在MyActor类中,有一行 context.system。 Terminate()

Then in the MyActor class I have the line context.system.terminate()

这篇关于如何等待Akka actor系统终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆