Akka并发编程——第三节:Actor模型(二)

本文详细解析了Akka Actor模型的API,包括preStart和postStop钩子方法,self和sender成员变量及方法,以及unhandled消息处理。通过示例代码展示了Actor如何启动、停止以及消息传递的过程,强调了未处理消息的处理策略在实际开发中的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本节主要内容:

  1. Actor API解析

1. Actor API解析

Actor中的主要成员变量和方法定义如下:

package akka.actor
trait Actor extends scala.AnyRef {
  type Receive = akka.actor.Actor.Receive

  //context变量暴露当前Actor的上下文信息及当前消息
  implicit val context : akka.actor.ActorContext = { /* compiled code */ }

  //self作为当前ActorRef的引用
  implicit final val self : akka.actor.ActorRef = { /* compiled code */ }

  //当前Actor接收到最后一条消息对应的消息发送者(Actor)
  final def sender() : akka.actor.ActorRef = { /* compiled code */ }

  //receive方法,抽象方法,定义Actor的行为逻辑
  def receive : akka.actor.Actor.Receive

  //内部使用API 
  protected[akka] def aroundReceive(receive : akka.actor.Actor.Receive, msg : scala.Any) : scala.Unit = { /* compiled code */ }
  protected[akka] def aroundPreStart() : scala.Unit = { /* compiled code */ }
  protected[akka] def aroundPostStop() : scala.Unit = { /* compiled code */ }
  protected[akka] def aroundPreRestart(reason : scala.Throwable, message : scala.Option[scala.Any]) : scala.Unit = { /* compiled code */ }
  protected[akka] def aroundPostRestart(reason : scala.Throwable) : scala.Unit = { /* compiled code */ }


   //监督策略,用于Actor容错处理
  def supervisorStrategy : akka.actor.SupervisorStrategy = { /* compiled code */ }

  //Hook方法,用于Actor生命周期监控 
  @scala.throws[T](classOf[scala.Exception])
  def preStart() : scala.Unit = { /* compiled code */ }
  @scala.throws[T](classOf[scala.Exception])
  def postStop() : scala.Unit = { /* compiled code */ }
  @scala.throws[T](classOf[scala.Exception])
  def preRestart(reason : scala.Throwable, message : scala.Option[scala.Any]) : scala.Unit = { /* compiled code */ }
  @scala.throws[T](classOf[scala.Exception])
  def postRestart(reason : scala.Throwable) : scala.Unit = { /* compiled code */ }


  //发送给Actor的消息,Actor没有定义相应的处理逻辑时,会调用此方法
  def unhandled(message : scala.Any) : scala.Unit = { /* compiled code */ }
}
object Actor extends scala.AnyRef {
  type Receive = scala.PartialFunction[scala.Any, scala.Unit]

  //空的行为逻辑
  @scala.SerialVersionUID(1)
  object emptyBehavior extends scala.AnyRef with akka.actor.Actor.Receive {
    def isDefinedAt(x : scala.Any) : scala.Boolean = { /* compiled code */ }
    def apply(x : scala.Any) : scala.Nothing = { /* compiled code */ }
  }
  //Sender为null
  @scala.SerialVersionUID(1)
  final val noSender : akka.actor.ActorRef = { /* compiled code */ }
}

(1) Hook方法,preStart()、postStop()方法的使用

/*
 *Actor API: Hook方法
 */
  object Example_05 extends App{
   
   
    import akka.actor.Actor
    import akka.actor.ActorSystem
    import akka.actor.Props


    class FirstActor extends Actor with ActorLogging{
   
   
      //通过context.actorOf方法创建Actor
      var child:ActorRef = _

      //Hook方法,preStart(),Actor启动之前调用,用于完成初始化工作
      override def preStart(): Unit ={
        log.info("preStart() in FirstActor")
        //通过context上下文创建Actor
        child = context.actorOf(Props[MyActor], name = "myChild")
      }
      def receive = {
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值