Akka并发编程——4、Actor模型(四)

xiaoxiao2021-02-28  104

摘要: 本节主要内容: 1. 停止Actor 

(1)通过ActorSystem.shutdown方法停止该system下所有Actor的运行

(2)通过context.stop方法停止Actor的运行

(3)通过akka.actor.PoisonPill消息停止Actor的运行

本节主要内容:      停止Actor

停止Actor

(1)通过ActorSystem.shutdown方法停止所有 Actor的运行

/* *停止Actor:ActorSystem.shutdown方法 */ object Example_10 extends App{ import akka.actor.Actor import akka.actor.ActorSystem import akka.actor.Props class FirstActor extends Actor with ActorLogging{ var child:ActorRef = context.actorOf(Props[MyActor], name = "myActor") def receive = { //向MyActor发送消息 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 //创建FirstActor对象 val firstactor = system.actorOf(Props[FirstActor], name = "firstActor") systemLog.info("准备向firstactor发送消息") //向firstactor发送消息 firstactor!"test" firstactor! 123 //关闭ActorSystem,停止所有Acotr运行 system.shutdown() } 代码运行结果:

(2)通过context.stop方法停止Actor的运行

/* *停止Actor:context.stop方法 */ object Example_11 extends App{ import akka.actor.Actor import akka.actor.ActorSystem import akka.actor.Props class FirstActor extends Actor with ActorLogging{ var child:ActorRef = context.actorOf(Props[MyActor], name = "myActor") def receive = { case "stop"=>context.stop(child) // 当FirstActor接受到消息stop时,停掉其子Actor:child。 case x =>{ //向MyActor发送消息 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 //创建FirstActor对象 val firstactor = system.actorOf(Props[FirstActor], name = "firstActor") systemLog.info("准备向firstactor发送消息") //向firstactor发送消息 firstactor!"test" firstactor! 123 firstactor!"stop" } 代码运行结果:

注意程序中并没有使用system.shutdown方法,因此不会使得整个程序全部停止,如下图所示 

(3)通过akka.actor.PoisonPill消息停止Actor的运行

/* *停止Actor:使用akka.actor.PoisonPill */ object Example_12 extends App{ import akka.actor.Actor import akka.actor.ActorSystem import akka.actor.Props import akka.actor.PoisonPill class FirstActor extends Actor with ActorLogging{ var child:ActorRef = context.actorOf(Props[MyActor], name = "myActor") def receive = { //向child发送PoisonPill停止其运行 case "stop"=>child!PoisonPill case x =>{ //向MyActor发送消息 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 //创建FirstActor对象 val firstactor = system.actorOf(Props[FirstActor], name = "firstActor") systemLog.info("准备向firstactor发送消息") //向firstactor发送消息 firstactor!"test" firstactor! 123 firstactor!"stop" } 代码运行结果:

注意,其与第(2)中方法不同在于:不像第(2)种方法使用context.stop()。而是直接向子Actor发送表示中断的消息。(

akka.actor.PoisonPill

此外,还有一种gracefulStop方法可以停止Actor的运行,这部分内容等了解完Future类的作用原理之后再来讨论

转载请注明原文地址: https://www.6miu.com/read-70380.html

最新回复(0)