开发者社区> 问答> 正文

如何保证在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'.

 

 

展开
收起
爱吃鱼的程序员 2020-06-08 11:03:03 748 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

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

     

    多谢!受教了

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

    2020-06-08 11:03:17
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
面向失败设计 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载