0
回答
如何保证在ActorSystem关闭的时候,所有的ActorRef任务都能执行完?
终于搞明白,存储TCO原来是这样算的>>>   

做过一个实验,把要处理的任务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'.

 

 

举报
ChangeZ
发帖于1个月前 0回/23阅
顶部