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]
}
}
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.
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.
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.
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.
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 = { ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With