个性化阅读
专注于IT技术分析

Akka Actor生命周期解释和示例详解

本文概述

Akka为Actor提供生命周期方法。你可以重写以下方法, 并相应地提供特定的实现。

Akka Actor生命周期方法

  1. preStart()
  2. 记录停止()
  3. preRestart(原因:可抛出, 消息:选项[任何])
  4. postRestart(原因:可抛出)
Akka Actor生命周期

1)preStart()

这是可重写的方法, 因此你可以重写preStart()方法以为Actor提供特定的实现。在Actor启动之后和首次创建Actor时立即调用它。如果重新启动, 则由postRestart()方法调用。

Akka Actor preStart()方法示例

import akka.actor._
import akka.actor.TypedActor.PreStart

class LifeCyleMethodsExample extends Actor{
  def receive = {
    case msg:String => println(msg+" "+self.path.name);  // Getting name of Actor
  }
  override def preStart(){    // overriding preStart method
    println("preStart method is called");
  }
}

object ActorMain{
  def main(args:Array[String]){
    val actorSystem = ActorSystem("ActorSystem");
    val actor = actorSystem.actorOf(Props[LifeCyleMethodsExample], "RootActor");
    actor ! "Hello"
  }
}

输出

preStart method is called
Hello RootActor

2)postStop()

你也可以覆盖此方法。停止actor之后, 将调用postStop()方法。它是一种异步方法。此方法用于在停止Actor之后释放资源。它可以用于注销该Actor的注册。发送到已停止的actor的消息将被重定向到ActorSystem的deadLetters。

Akka Actor PostStop()方法示例

import akka.actor._
import akka.actor.TypedActor.PreStart

class LifeCyleMethodsExample extends Actor{
  def receive = {
    case msg:String => println(msg+" "+self.path.name);  // Getting name of Actor
  }
  override def postStop(){    // Overriding postStop method
    println("postStop method is called");
  }
}

object ActorMain{
  def main(args:Array[String]){
    val actorSystem = ActorSystem("ActorSystem");
    val actor = actorSystem.actorOf(Props[LifeCyleMethodsExample], "RootActor");
    actor ! "Hello"
    
    println("stopping Actor");
    actorSystem.stop(actor);      // Stopping Actor by passing actor reference. 
  }
}

输出

Hello RootActor
stopping Actor
postStop method is called

3)preRestart()

如果抛出异常, 则可以重新启动Actor。当actor重新启动时, 将调用preRestart()方法。调用preRestart()方法会导致重启。默认情况下, 它处置所有子Actor, 然后调用postStop()方法。

Akka Actor preRestart()方法示例

println(msg+" "+self.path.name);  // Getting name of Actor
    var a:Int =  10/0;      // ArithmethicException occurred
  }
  override def preRestart(reason:Throwable, message: Option[Any]){    // Overriding preRestart method
    println("preRestart method is called");
    println("Reason: "+reason)
  }
}

object ActorMain{
  def main(args:Array[String]){
    val actorSystem = ActorSystem("ActorSystem");
    val actor = actorSystem.actorOf(Props[LifeCyleMethodsExample], "RootActor");
    actor ! "Hello"
  }
}

输出

Hello RootActor
preRestart method is called
Reason: java.lang.ArithmeticException: / by zero

4)postRestart()

重新启动新创建的Actor后立即调用此方法。它用于允许因异常而导致Actor崩溃后重新初始化。

Akka Actor postRestart()方法示例

import akka.actor._

class LifeCyleMethodsExample extends Actor{
  def receive = {
    case msg:String => println(msg+" "+self.path.name);  // Getting name of Actor
    var a:Int =  10/0;      // ArithmethicException occurred
  }
  override def postRestart(reason:Throwable){    // Overriding preRestart method
    println("postRestart method is called");
    println("Reason: "+reason)
  }
}

object ActorMain{
  def main(args:Array[String]){
    val actorSystem = ActorSystem("ActorSystem");
    val actor = actorSystem.actorOf(Props[LifeCyleMethodsExample], "RootActor");
    actor ! "Hello"
  }
}

输出

Hello RootActor
postRestart method is called
Reason: java.lang.ArithmeticException: / by zero

Akka Complete LifeCyle示例

import akka.actor.{Actor, ActorSystem, Props}

class RootActor extends Actor{
  def receive = {
    case msg => println("Message received: "+msg);
    10/0;
  }
  override def preStart(){
    super.preStart();
    println("preStart method is called");
  }
  override def postStop(){
    super.postStop();
    println("postStop method is called");
  }
  override def preRestart(reason:Throwable, message: Option[Any]){
    super.preRestart(reason, message);
    println("preRestart method is called");
    println("Reason: "+reason);
  }
  override def postRestart(reason:Throwable){
    super.postRestart(reason);
    println("postRestart is called");
    println("Reason: "+reason);
  }
}

输出

preStart method is called
Message received: Hello
Stoping actor
postStop method is called
preRestart method is called
Reason: java.lang.ArithmeticException: / by zero
preStart method is called
postRestart is called
Reason: java.lang.ArithmeticException: / by zero
[ERROR] [01/12/2017 15:22:18.475] [ActorSystem-akka.actor.default-dispatcher-5] 
[akka://ActorSystem/user/RootActor] / by zero
java.lang.ArithmeticException: / by zero
	at RootActor$$anonfun$receive$1.applyOrElse(ActorLifeCyle.scala:6)
	at akka.actor.Actor$class.aroundReceive(Actor.scala:496)
	at RootActor.aroundReceive(ActorLifeCyle.scala:3)
	at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
	at akka.actor.ActorCell.invoke(ActorCell.scala:495)
	at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
	at akka.dispatch.Mailbox.run(Mailbox.scala:224)
	at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
	at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
	at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
	at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
	at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
	postStop method is called
赞(0)
未经允许不得转载:srcmini » Akka Actor生命周期解释和示例详解

评论 抢沙发

评论前必须登录!