akka-typed的actor從建立、啓用、狀態轉換、停用、監視等生命週期管理方式和akka-classic仍是有必定的不一樣之處。這篇咱們就介紹一下akka-typed的actor生命週期管理。app
每一種actor都是經過定義它的行爲屬性behavior造成模版,而後由對上一層的父輩actor用spawn方法產生actor實例的。產生的actor實例加入一個系統的由上至下樹形結構,直接在spawn產生本身的父輩之下。akka-typed的守護guardian-actor,即根部root-actor是經過在定義ActorSystem時指定併產生的。以下:框架
val config = ConfigFactory.load("application.conf") val man: ActorSystem[GreetStarter.Command] = ActorSystem(GreetStarter(), "greetDemo",config) man ! GreetStarter.RepeatedGreeting("Tiger",1.seconds)
在某種意義上,這個ActorSystem實例man就表明root-actor。咱們能夠向man發送消息而後由GreetStarter的behavior用本身的ActorContext進行spawn,stop,watch及分派計算任務等,其實就是一個程序的集線器:函數
object GreetStarter { import Messages._ def apply(): Behavior[SayHi] = { Behaviors.setup { ctx => val props = DispatcherSelector.fromConfig("akka.actor.default-blocking-io-dispatcher") val helloActor = ctx.spawn(HelloActor(), "hello-actor",props) val greeter = ctx.spawn(Greeter(helloActor), "greeter") ctx.watch(greeter) ctx.watchWith(helloActor,StopWorker("something happend")) Behaviors.receiveMessage { who =>
if (who.name == "stop") { ctx.stop(helloActor) ctx.stop(greeter) Behaviors.stopped } else { greeter ! who Behaviors.same } } } } }
可是,總有時候咱們須要在root-actor的ActorContext以外來進行一些製造、使用actor的操做。下面這個官方文檔上的例子是很好的示範:this
import akka.actor.typed.Behavior import akka.actor.typed.SpawnProtocol import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.LoggerOps object HelloWorldMain { def apply(): Behavior[SpawnProtocol.Command] = Behaviors.setup { context =>
// Start initial tasks // context.spawn(...)
SpawnProtocol() } } object Main extends App { implicit val system: ActorSystem[SpawnProtocol.Command] = ActorSystem(HelloWorldMain(), "hello") // needed in implicit scope for ask (?)
import akka.actor.typed.scaladsl.AskPattern._ implicit val ec: ExecutionContext = system.executionContext implicit val timeout: Timeout = Timeout(3.seconds) val greeter: Future[ActorRef[HelloWorld.Greet]] = system.ask(SpawnProtocol.Spawn(behavior = HelloWorld(), name = "greeter", props = Props.empty, _)) val greetedBehavior = Behaviors.receive[HelloWorld.Greeted] { (context, message) => context.log.info2("Greeting for {} from {}", message.whom, message.from) Behaviors.stopped } val greetedReplyTo: Future[ActorRef[HelloWorld.Greeted]] = system.ask(SpawnProtocol.Spawn(greetedBehavior, name = "", props = Props.empty, _)) for (greeterRef <- greeter; replyToRef <- greetedReplyTo) { greeterRef ! HelloWorld.Greet("Akka", replyToRef) } ... }
能夠看到全部操做都在actor框架以外進行的。這個SpawnProtocol自己就是一個actor,以下:spa
object SpawnProtocol { ... final case class Spawn[T](behavior: Behavior[T], name: String, props: Props, replyTo: ActorRef[ActorRef[T]]) extends Command ... def apply(): Behavior[Command] = Behaviors.receive { (ctx, msg) => msg match { case Spawn(bhvr, name, props, replyTo) => val ref =
if (name == null || name.equals("")) ctx.spawnAnonymous(bhvr, props) else { @tailrec def spawnWithUniqueName(c: Int): ActorRef[Any] = { val nameSuggestion = if (c == 0) name else s"$name-$c" ctx.child(nameSuggestion) match { case Some(_) => spawnWithUniqueName(c + 1) // already taken, try next
case None => ctx.spawn(bhvr, nameSuggestion, props) } } spawnWithUniqueName(0) } replyTo ! ref Behaviors.same } } }
外界經過發送Spawn消息來指定產生新的actor。scala
actor的狀態切換就是從一種behavior轉到另外一種behavior。咱們能夠自定義behavior或者用現成的Behaviors.???。若是隻是涉及內部變量變化,那麼能夠直接生成帶着變量的當前behavior,以下:rest
object HelloWorldBot { def apply(max: Int): Behavior[HelloWorld.Greeted] = { bot(0, max) } private def bot(greetingCounter: Int, max: Int): Behavior[HelloWorld.Greeted] = Behaviors.receive { (context, message) => val n = greetingCounter + 1 context.log.info2("Greeting {} for {}", n, message.whom) if (n == max) { Behaviors.stopped } else { message.from ! HelloWorld.Greet(message.whom, context.self) bot(n, max) } } }
actor停用能夠由直屬父輩actor的ActorContext.stop或者自身的Behaviors.stopped來實現。Behaviors.stopped能夠帶入一個清理函數。在actor徹底中止以前進行一些清理操做: code
object MasterControlProgram { sealed trait Command final case class SpawnJob(name: String) extends Command case object GracefulShutdown extends Command // Predefined cleanup operation
def cleanup(log: Logger): Unit = log.info("Cleaning up!") def apply(): Behavior[Command] = { Behaviors .receive[Command] { (context, message) => message match { case SpawnJob(jobName) => context.log.info("Spawning job {}!", jobName) context.spawn(Job(jobName), name = jobName) Behaviors.same case GracefulShutdown => context.log.info("Initiating graceful shutdown...") // perform graceful stop, executing cleanup before final system termination // behavior executing cleanup is passed as a parameter to Actor.stopped
Behaviors.stopped { () => cleanup(context.system.log) } } } .receiveSignal { case (context, PostStop) => context.log.info("Master Control Program stopped") Behaviors.same } } }
實際上一個actor轉入停用stop狀態能夠在另外一個做爲監視actor的receiveSignal獲取,以下:orm
object GreetStarter { import Messages._ def apply(): Behavior[SayHi] = { Behaviors.setup { ctx => val props = DispatcherSelector.fromConfig("akka.actor.default-blocking-io-dispatcher") val helloActor = ctx.spawn(HelloActor(), "hello-actor",props) val greeter = ctx.spawn(Greeter(helloActor), "greeter") ctx.watch(greeter) ctx.watchWith(helloActor,StopWorker("something happend")) Behaviors.receiveMessage { who =>
if (who.name == "stop") { ctx.stop(helloActor) ctx.stop(greeter) Behaviors.stopped } else { greeter ! who Behaviors.same } }.receiveSignal { case (context, Terminated(ref)) => context.log.info("{} stopped!", ref.path.name) Behaviors.same } } } }
下面是.receiveSignal函數及其捕獲的Signal消息:blog
trait Receive[T] extends Behavior[T] { def receiveSignal(onSignal: PartialFunction[(ActorContext[T], Signal), Behavior[T]]): Behavior[T] } trait Signal /** * Lifecycle signal that is fired upon restart of the Actor before replacing * the behavior with the fresh one (i.e. this signal is received within the * behavior that failed). */
sealed abstract class PreRestart extends Signal case object PreRestart extends PreRestart { def instance: PreRestart = this } /** * Lifecycle signal that is fired after this actor and all its child actors * (transitively) have terminated. The [[Terminated]] signal is only sent to * registered watchers after this signal has been processed. */
sealed abstract class PostStop extends Signal // comment copied onto object for better hints in IDEs /** * Lifecycle signal that is fired after this actor and all its child actors * (transitively) have terminated. The [[Terminated]] signal is only sent to * registered watchers after this signal has been processed. */
case object PostStop extends PostStop { def instance: PostStop = this } object Terminated { def apply(ref: ActorRef[Nothing]): Terminated = new Terminated(ref) def unapply(t: Terminated): Option[ActorRef[Nothing]] = Some(t.ref) }