Akka并发编程——第五节:Actor模型(四) 停止Actor
生活随笔
收集整理的這篇文章主要介紹了
Akka并发编程——第五节:Actor模型(四) 停止Actor
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本節(jié)主要內(nèi)容:
1. 停止Actor
1. 停止Actor
(1)通過(guò)ActorSystem.shutdown方法停止所有 Actor的運(yùn)行
/* *停止Actor:ActorSystem.shutdown方法 */ object Example_10 extends App{import akka.actor.Actorimport akka.actor.ActorSystemimport akka.actor.Propsclass FirstActor extends Actor with ActorLogging{var child:ActorRef = context.actorOf(Props[MyActor], name = "myActor")def receive = {//向MyActor發(fā)送消息case x => child ! x;log.info("received "+x)}override def postStop(): Unit = {log.info("postStop In FirstActor")}}class MyActor extends Actor with ActorLogging{def receive = {case "test" => log.info("received test");case _ => log.info("received unknown message");}override def postStop(): Unit = {log.info("postStop In MyActor")}}val system = ActorSystem("MyActorSystem")val systemLog=system.log//創(chuàng)建FirstActor對(duì)象val firstactor = system.actorOf(Props[FirstActor], name = "firstActor")systemLog.info("準(zhǔn)備向firstactor發(fā)送消息")//向firstactor發(fā)送消息firstactor!"test"firstactor! 123//關(guān)閉ActorSystem,停止所有Acotr運(yùn)行system.shutdown() }代碼運(yùn)行結(jié)果:
[INFO] [04/02/2016 21:51:34.440] [main] [ActorSystem(MyActorSystem)] 準(zhǔn)備向firstactor發(fā)送消息 [INFO] [04/02/2016 21:51:34.441] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor/myActor] received test [INFO] [04/02/2016 21:51:34.441] [MyActorSystem-akka.actor.default-dispatcher-2] [akka://MyActorSystem/user/firstActor] received test [INFO] [04/02/2016 21:51:34.441] [MyActorSystem-akka.actor.default-dispatcher-2] [akka://MyActorSystem/user/firstActor] received 123 [INFO] [04/02/2016 21:51:34.441] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor/myActor] received unknown message [INFO] [04/02/2016 21:51:34.446] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor/myActor] postStop In MyActor [INFO] [04/02/2016 21:51:34.447] [MyActorSystem-akka.actor.default-dispatcher-5] [akka://MyActorSystem/user/firstActor] postStop In FirstActor(2)通過(guò)context.stop方法停止Actor的運(yùn)行
/* *停止Actor:context.stop方法 */ object Example_11 extends App{import akka.actor.Actorimport akka.actor.ActorSystemimport akka.actor.Propsclass FirstActor extends Actor with ActorLogging{var child:ActorRef = context.actorOf(Props[MyActor], name = "myActor")def receive = {case "stop"=>context.stop(child)case x =>{//向MyActor發(fā)送消息child ! xlog.info("received "+x)}}override def postStop(): Unit = {log.info("postStop In FirstActor")}}class MyActor extends Actor with ActorLogging{def receive = {case "test" => log.info("received test");case _ => log.info("received unknown message");}override def postStop(): Unit = {log.info("postStop In MyActor")}}val system = ActorSystem("MyActorSystem")val systemLog=system.log//創(chuàng)建FirstActor對(duì)象val firstactor = system.actorOf(Props[FirstActor], name = "firstActor")systemLog.info("準(zhǔn)備向firstactor發(fā)送消息")//向firstactor發(fā)送消息firstactor!"test"firstactor! 123firstactor!"stop"}代碼運(yùn)行結(jié)果:
[INFO] [04/02/2016 22:02:48.760] [main] [ActorSystem(MyActorSystem)] 準(zhǔn)備向firstactor發(fā)送消息 [INFO] [04/02/2016 22:02:48.761] [MyActorSystem-akka.actor.default-dispatcher-5] [akka://MyActorSystem/user/firstActor/myActor] received test [INFO] [04/02/2016 22:02:48.761] [MyActorSystem-akka.actor.default-dispatcher-2] [akka://MyActorSystem/user/firstActor] received test [INFO] [04/02/2016 22:02:48.762] [MyActorSystem-akka.actor.default-dispatcher-2] [akka://MyActorSystem/user/firstActor] received 123 [INFO] [04/02/2016 22:02:48.762] [MyActorSystem-akka.actor.default-dispatcher-5] [akka://MyActorSystem/user/firstActor/myActor] received unknown message [INFO] [04/02/2016 22:02:48.763] [MyActorSystem-akka.actor.default-dispatcher-5] [akka://MyActorSystem/user/firstActor/myActor] postStop In MyActor代碼的重點(diǎn)為
class FirstActor extends Actor with ActorLogging{var child:ActorRef = context.actorOf(Props[MyActor], name = "myActor")def receive = {case "stop"=>context.stop(child)case x =>{//向MyActor發(fā)送消息child ! xlog.info("received "+x)}}override def postStop(): Unit = {log.info("postStop In FirstActor")}}中的case “stop”=>context.stop(child),直接通過(guò)context.stop方法停止Actor的運(yùn)行。注意程序中并沒(méi)有使用system.shutdown方法,因此整個(gè)程序的不會(huì)停止,如下圖所示
(3)通過(guò)akka.actor.PoisonPill消息停止Actor的運(yùn)行
/* *停止Actor:使用akka.actor.PoisonPill */ object Example_12 extends App{import akka.actor.Actorimport akka.actor.ActorSystemimport akka.actor.Propsimport akka.actor.PoisonPillclass FirstActor extends Actor with ActorLogging{var child:ActorRef = context.actorOf(Props[MyActor], name = "myActor")def receive = {//向child發(fā)送PoisonPill停止其運(yùn)行case "stop"=>child!PoisonPillcase x =>{//向MyActor發(fā)送消息child ! xlog.info("received "+x)}}override def postStop(): Unit = {log.info("postStop In FirstActor")}}class MyActor extends Actor with ActorLogging{def receive = {case "test" => log.info("received test");case _ => log.info("received unknown message");}override def postStop(): Unit = {log.info("postStop In MyActor")}}val system = ActorSystem("MyActorSystem")val systemLog=system.log//創(chuàng)建FirstActor對(duì)象val firstactor = system.actorOf(Props[FirstActor], name = "firstActor")systemLog.info("準(zhǔn)備向firstactor發(fā)送消息")//向firstactor發(fā)送消息firstactor!"test"firstactor! 123firstactor!"stop"}代碼運(yùn)行結(jié)果:
[INFO] [04/02/2016 22:12:09.947] [main] [ActorSystem(MyActorSystem)] 準(zhǔn)備向firstactor發(fā)送消息 [INFO] [04/02/2016 22:12:09.947] [MyActorSystem-akka.actor.default-dispatcher-2] [akka://MyActorSystem/user/firstActor/myActor] received test [INFO] [04/02/2016 22:12:09.947] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor] received test [INFO] [04/02/2016 22:12:09.947] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor] received 123 [INFO] [04/02/2016 22:12:09.947] [MyActorSystem-akka.actor.default-dispatcher-2] [akka://MyActorSystem/user/firstActor/myActor] received unknown message [INFO] [04/02/2016 22:12:09.951] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor/myActor] postStop In MyActor代碼與Exampel_11中的不同之處在于
//向child發(fā)送PoisonPill停止其運(yùn)行 case "stop"=>child!PoisonPill它使用不是context.stop方法,而是向MyActor發(fā)送了PoisonPill消息,其它代碼不變。
還有一種gracefulStop方法可以停止Actor的運(yùn)行,這部分內(nèi)容等了解完Future類(lèi)的作用原理之后再來(lái)討論
總結(jié)
以上是生活随笔為你收集整理的Akka并发编程——第五节:Actor模型(四) 停止Actor的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: TensorFlow学习笔记(三)模型的
- 下一篇: Akka并发编程——第六节:Actor模