是否可以安全地关闭akka调度程序中的actor上下文

时间:2014-12-08 15:18:48

标签: scala akka

我知道在Future或调度程序中关闭sender方法调用或actor的内部状态是不安全的,但是actor的上下文呢? ActorContext中的内容是什么?在调度程序或将来的回调中关闭actor上下文是否安全? :

def receive: Receive = {
  case Msg => 
    system.scheduler.scheduleOnce(1 second) {
       context.actorOf[ChildActor]
    }
}

1 个答案:

答案 0 :(得分:8)

不,关闭一个actor上下文是不安全的。来自akka来源:

/**
   * 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 = { ...