如何保证在ActorSystem关闭的时候,所有的ActorRef任务都能执行完?
做过一个实验,把要处理的任务sleep 100ms,然后循环tell10次,也就是onReceiver那里会比较耗时,然后循环tell完之后。立即关闭ActorSystem,会报错,只执行了第一次的任务,后来的几次都没执行。简单代码如下:
public static void main(String[] args) {
ActorSystem system = ActorSystem.create("actorSystem");
ActorRef actorRef = system.actorOf(Props.create(GreetPrinter.class), "GreetPrinter");
for (int i = 0; i < 10; i++) {
actorRef.tell(new Greeting("Change" + i), ActorRef.noSender());
}
system.terminate();//旧版本是shutdown()
}
//actor,work方法耗时100ms
public class GreetPrinter extends UntypedActor {
@Override
public void onReceive(Object msg) throws Exception {
if (msg instanceof Greeting) {
Greeting g = (Greeting) msg;
g.work();
}
}
}
代码如上所示,会报错日志,我以为system.terminate();会把所有创建的ActorRef任务执行完才会关闭,但是发现不是这样的。那如何保证所有任务执行完才关闭ActorSystem呢?不要说用while循环检查每个ActorRef状态,这个方法不太好。报错日志如下:
[INFO] [09/10/2017 00:59:08.419] [actorSystem-akka.actor.default-dispatcher-5] [akka://actorSystem/user/GreetPrinter] Message [Greeting] from Actor[akka://actorSystem/deadLetters] to Actor[akka://actorSystem/user/GreetPrinter#1893524971] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [09/10/2017 00:59:08.419] [actorSystem-akka.actor.default-dispatcher-5] [akka://actorSystem/user/GreetPrinter] Message [Greeting] from Actor[akka://actorSystem/deadLetters] to Actor[akka://actorSystem/user/GreetPrinter#1893524971] was not delivered. [2] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [09/10/2017 00:59:08.419] [actorSystem-akka.actor.default-dispatcher-5] [akka://actorSystem/user/GreetPrinter] Message [Greeting] from Actor[akka://actorSystem/deadLetters] to Actor[akka://actorSystem/user/GreetPrinter#1893524971] was not delivered. [3] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [09/10/2017 00:59:08.419] [actorSystem-akka.actor.default-dispatcher-5] [akka://actorSystem/user/GreetPrinter] Message [Greeting] from Actor[akka://actorSystem/deadLetters] to Actor[akka://actorSystem/user/GreetPrinter#1893524971] was not delivered. [4] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [09/10/2017 00:59:08.419] [actorSystem-akka.actor.default-dispatcher-5] [akka://actorSystem/user/GreetPrinter] Message [Greeting] from Actor[akka://actorSystem/deadLetters] to Actor[akka://actorSystem/user/GreetPrinter#1893524971] was not delivered. [5] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [09/10/2017 00:59:08.419] [actorSystem-akka.actor.default-dispatcher-4] [akka://actorSystem/user/GreetPrinter] Message [Greeting] from Actor[akka://actorSystem/deadLetters] to Actor[akka://actorSystem/user/GreetPrinter#1893524971] was not delivered. [6] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [09/10/2017 00:59:08.420] [actorSystem-akka.actor.default-dispatcher-4] [akka://actorSystem/user/GreetPrinter] Message [Greeting] from Actor[akka://actorSystem/deadLetters] to Actor[akka://actorSystem/user/GreetPrinter#1893524971] was not delivered. [7] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [09/10/2017 00:59:08.420] [actorSystem-akka.actor.default-dispatcher-4] [akka://actorSystem/user/GreetPrinter] Message [Greeting] from Actor[akka://actorSystem/deadLetters] to Actor[akka://actorSystem/user/GreetPrinter#1893524971] was not delivered. [8] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [09/10/2017 00:59:08.420] [actorSystem-akka.actor.default-dispatcher-4] [akka://actorSystem/user/GreetPrinter] Message [Greeting] from Actor[akka://actorSystem/deadLetters] to Actor[akka://actorSystem/user/GreetPrinter#1893524971] was not delivered. [9] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
tell改成ask 会稍微有点问题吧。
多谢!受教了
发多少个信息你是知道的,当10个消息发完后发一个结束消息给GreetPrinter, tell改成ask,GreetPrinter的邮箱没有改过的话,消息应该是按顺序排列的,当GreetPrinter收到最后的结束消息后,向父actor发送结束回复消息并stop自己,在main程序中收到结束回复消息后就可以关闭ActorSystem了。