如何保证在ActorSystem关闭的时候,所有的ActorRef任务都能执行完?

发布于 2021-12-01 23:23:05 字数 4804 浏览 751 评论 3

做过一个实验,把要处理的任务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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩饰不了的爱 2021-12-08 07:12:33

 tell改成ask 会稍微有点问题吧。

坏尐絯 2021-12-06 14:15:52

多谢!受教了

英雄似剑 2021-12-06 14:04:31

发多少个信息你是知道的,当10个消息发完后发一个结束消息给GreetPrinter,  tell改成ask,GreetPrinter的邮箱没有改过的话,消息应该是按顺序排列的,当GreetPrinter收到最后的结束消息后,向父actor发送结束回复消息并stop自己,在main程序中收到结束回复消息后就可以关闭ActorSystem了。

 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文