国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 服务器 > Akka并发编程——第八节:Actor模型(七)

Akka并发编程——第八节:Actor模型(七)

来源:程序员人生   发布时间:2016-08-02 08:25:15 阅读次数:2345次

本节主要内容

停止运行Typed Actor

当Typed Actor不再需要时要将其停止,有3种方法停止Typed Actor的运行:
(1)通过system.shutdown()停止ActorSystem中所有的Typed Actor;
(2)调用TypedActor(system).stop(mySquarer)停止指定的Typed Actor;
(3)调用TypedActor(system).poisonPill(otherSquarer)停止指定的Typed Actor。
具体使用代码以下:

/* * 停止Typed Actor */ object Example_3 extends App { import akka.event.Logging import scala.concurrent.{ Promise, Future } import akka.actor.{ TypedActor, TypedProps } import scala.concurrent.duration._ trait Squarer { //fire-and-forget消息 def squareDontCare(i: Int): Unit //非阻塞send-request-reply消息 def square(i: Int): Future[Int] //阻塞式的send-request-reply消息 def squareNowPlease(i: Int): Option[Int] //阻塞式的send-request-reply消息 def squareNow(i: Int): Int } //混入PostStop和PreStart class SquarerImpl(val name: String) extends Squarer with PostStop with PreStart { import TypedActor.context val log = Logging(context.system,TypedActor.self.getClass()) def this() = this("SquarerImpl") def squareDontCare(i: Int): Unit = i * i def square(i: Int): Future[Int] = Promise.successful(i * i).future def squareNowPlease(i: Int): Option[Int] = Some(i * i) def squareNow(i: Int): Int = i * i def postStop(): Unit={ log.info ("TypedActor Stopped") } def preStart(): Unit={ log.info ("TypedActor Started") } } val system = ActorSystem("TypedActorSystem") val log = Logging(system, this.getClass) //使用默许构造函数创建Typed Actor val mySquarer: Squarer = TypedActor(system).typedActorOf(TypedProps[SquarerImpl](),"mySquarer") //使用非默许构造函数创建Typed Actor val otherSquarer: Squarer = TypedActor(system).typedActorOf(TypedProps(classOf[Squarer], new SquarerImpl("SquarerImpl")), "otherSquarer") //Request-reply-with-future 消息发送 val fSquare = mySquarer.square(10) val result = Await.result(fSquare, 5 second) log.info("fSquare="+result) //调用poisonPill方法停止Actor运行 TypedActor(system).poisonPill(otherSquarer) //调用stop方法停止Actor运行 TypedActor(system).stop(mySquarer) //system.shutdown() }

代码运行结果以下所示。

[INFO] [03/21/2016 22:41:51.119] [TypedActorSystem-akka.actor.default-dispatcher-2] [$Proxy0(akka://TypedActorSystem)] TypedActor Started [INFO] [03/21/2016 22:41:51.123] [TypedActorSystem-akka.actor.default-dispatcher-2] [$Proxy1(akka://TypedActorSystem)] TypedActor Started [INFO] [03/21/2016 22:41:51.124] [main] [Example12_10$(akka://TypedActorSystem)] fSquare=100 [INFO] [03/21/2016 22:41:51.131] [TypedActorSystem-akka.actor.default-dispatcher-5] [$Proxy1(akka://TypedActorSystem)] TypedActor Stopped [INFO] [03/21/2016 22:41:51.131] [TypedActorSystem-akka.actor.default-dispatcher-3] [$Proxy0(akka://TypedActorSystem)] TypedActor Stopped

代码中类SquarerImpl 混入了PreStart和PostStop两个trait:class SquarerImpl(val name: String) extends Squarer with PostStop with PreStart,这样的话在创建TypedActor之前和停止TypedActor后能够进行相应的操作,本例中主要是为监视TypedActor的创建和停止进程。代码TypedActor(system).stop(mySquarer)通过stop方法停止TypedActor,而TypedActor(system)
.poisonPill(otherSquarer)通过调用poisonPill方法停止运行TypedActor。

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生