Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it safe to close over the actor context in akka scheduler

Tags:

scala

akka

I know that it's not safe to close over the sender method call or the internal state of the actor in a Future or a scheduler, but what about the actor context? what are in an ActorContext? Is it safe to close over the actor context in a scheduler or a future callback, like this? :

def receive: Receive = {
  case Msg => 
    system.scheduler.scheduleOnce(1 second) {
       context.actorOf[ChildActor]
    }
}
like image 517
xiefei Avatar asked Dec 08 '14 15:12

xiefei


People also ask

Are Akka actors thread safe?

Actors are 'Treadsafe'. The Actor System (AKKA), provides each actor with its own 'light-weight thread'. Meaning that this is not a tread, but the AKKA system will give the impression that an Actor is always running in it's own thread to the developer.

Can an Akka actor stop other actors?

In Akka, you can stop Actors by invoking the stop() method of either ActorContext or ActorSystem class. ActorContext is used to stop child actor and ActorSystem is used to stop top level Actor. The actual termination of the actor is performed asynchronously.

Is Akka scalable?

Akka is a very scalable piece of software, not only in the context of performance but also in the size of applications it is useful for. The core of Akka, akka-actor, is very small and easily dropped into an existing project where you need asynchronicity and lockless concurrency without hassle.

Which is one is the method in Akka actor life cycle?

Akka Actor life cycle methods This is overridable method, so you can override preStart() method to provide specific implementation for an Actor. It is invoked right after the starting of Actor and when an Actor is first created. In case of restart, it is called by postRestart() method.


1 Answers

No, it is not safe to close over an actor context. From akka source:

/**
   * 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 = { ...
like image 74
jxstanford Avatar answered Nov 13 '22 21:11

jxstanford