【Akka】Actor引用

Actor系统的实体

在Actor系统中,actor之间具有树形的监管结构,并且actor可以跨多个网络节点进行透明通信。
对于一个Actor而言,其源码中存在ActorActorContextActorRef等多个概念,它们都是为了描述Actor对象而进行的不同层面的抽象。
我们先给出一个官方的示例图,再对各个概念进行解释。

上图很清晰的展示了一个actor在源码层面的不同抽象,和不同actor之间的父子关系:
Actor类的一个成员context是ActorContext类型,ActorContext存储了Actor类的上下文,包括self、sender。
ActorContext还混入了ActorRefFactory特质,其中实现了actorOf方法用来创建子actor。
这是Actor中context的源码:

trait Actor {
  /**
   * Stores the context for this actor, including self, and sender.
   * It is implicit to support operations such as `forward`.
   *
   * WARNING: Only valid within the Actor itself, so do not close over it and
   * publish it to other threads!
   *
   * [[akka.actor.ActorContext]] is the Scala API. `getContext` returns a
   * [[akka.actor.UntypedActorContext]], which is the Java API of the actor
   * context.
   */
  implicit val context: ActorContext = {
    val contextStack = ActorCell.contextStack.get
    if ((contextStack.isEmpty) || (contextStack.head eq null))
      throw ActorInitializationException(
        s"You cannot create an instance of [${getClass.getName}] explicitly using the constructor (new). " +
          "You have to use one of the 'actorOf' factory methods to create a new actor. See the documentation.")
    val c = contextStack.head
    ActorCell.contextStack.set(null :: contextStack)
    c
  }

ActorCell的self成员是ActorRef类型,ActorRef是一个actor的不可变,可序列化的句柄(handle),它可能不在本地或同一个ActorSystem中,它是实现网络空间位置透明性的关键设计。
这是ActorContext中self的源码:

trait ActorContext extends
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值