简介: 本节主要内容 停止运行Typed Actor 当Typed Actor不再需要时要将其停止,有3种方法停止Typed Actor的运行: (1)通过system.shutdown()停止ActorSystem中所有的Typed Actor; (2)调用TypedActor(system).stop(mySquarer)停止指定的Typed Actor;
本节主要内容

停止运行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。
具体使用代码如下:

  1. /*
  2. * 停止Typed Actor
  3. */
  4. object Example_3 extends App {
  5. import akka.event.Logging
  6. import scala.concurrent.{ Promise, Future }
  7. import akka.actor.{ TypedActor, TypedProps }
  8. import scala.concurrent.duration._
  9. trait Squarer {
  10. //fire-and-forget消息
  11. def squareDontCare(i: Int): Unit
  12. //非阻塞send-request-reply消息
  13. def square(i: Int): Future[Int]
  14. //阻塞式的send-request-reply消息
  15. def squareNowPlease(i: Int): Option[Int]
  16. //阻塞式的send-request-reply消息
  17. def squareNow(i: Int): Int
  18. }
  19. //混入PostStop和PreStart
  20. class SquarerImpl(val name: String) extends Squarer with PostStop with PreStart {
  21. import TypedActor.context
  22. val log = Logging(context.system,TypedActor.self.getClass())
  23. def this() = this("SquarerImpl")
  24. def squareDontCare(i: Int): Unit = i * i
  25. def square(i: Int): Future[Int] = Promise.successful(i * i).future
  26. def squareNowPlease(i: Int): Option[Int] = Some(i * i)
  27. def squareNow(i: Int): Int = i * i
  28. def postStop(): Unit={
  29. log.info ("TypedActor Stopped")
  30. }
  31. def preStart(): Unit={
  32. log.info ("TypedActor Started")
  33. }
  34. }
  35. val system = ActorSystem("TypedActorSystem")
  36. val log = Logging(system, this.getClass)
  37. //使用默认构造函数创建Typed Actor
  38. val mySquarer: Squarer =
  39. TypedActor(system).typedActorOf(TypedProps[SquarerImpl](),"mySquarer")
  40. //使用非默认构造函数创建Typed Actor
  41. val otherSquarer: Squarer =
  42. TypedActor(system).typedActorOf(TypedProps(classOf[Squarer],
  43. new SquarerImpl("SquarerImpl")), "otherSquarer")
  44. //Request-reply-with-future 消息发送
  45. val fSquare = mySquarer.square(10)
  46. val result = Await.result(fSquare, 5 second)
  47. log.info("fSquare="+result)
  48. //调用poisonPill方法停止Actor运行
  49. TypedActor(system).poisonPill(otherSquarer)
  50. //调用stop方法停止Actor运行
  51. TypedActor(system).stop(mySquarer)
  52. //system.shutdown()
  53. }

代码运行结果如下所示。

  1. [INFO] [03/21/2016 22:41:51.119] [TypedActorSystem-akka.actor.default-dispatcher-2] [$Proxy0(akka://TypedActorSystem)] TypedActor Started
  2. [INFO] [03/21/2016 22:41:51.123] [TypedActorSystem-akka.actor.default-dispatcher-2] [$Proxy1(akka://TypedActorSystem)] TypedActor Started
  3. [INFO] [03/21/2016 22:41:51.124] [main] [Example12_10$(akka://TypedActorSystem)] fSquare=100
  4. [INFO] [03/21/2016 22:41:51.131] [TypedActorSystem-akka.actor.default-dispatcher-5] [$Proxy1(akka://TypedActorSystem)] TypedActor Stopped
  5. [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。