做过一个实验,把要处理的任务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'.
发多少个信息你是知道的,当10个消息发完后发一个结束消息给GreetPrinter, tell改成ask,GreetPrinter的邮箱没有改过的话,消息应该是按顺序排列的,当GreetPrinter收到最后的结束消息后,向父actor发送结束回复消息并stop自己,在main程序中收到结束回复消息后就可以关闭ActorSystem了。
多谢!受教了
tell改成ask会稍微有点问题吧。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。