Akka 2.x requires many commands to reference an ActorSystem
. So, to create an instance of an actor MyActor
you might say:
val system = ActorSystem() val myActor = system.actorOf(Props[MyActor])
Because of the frequent need for an ActorSystem
: many code examples omit the creation from the code and assume that the reader knows where a system
variable has come from.
If your code produces actors in different places, you could duplicate this code, possibly creating additional ActorSystem
instances, or you could try to share the same ActorSystem
instance by referring to some global or by passing the ActorSystem
around.
The Akka documentation provides a general overview of systems of actors under the heading 'Actor Systems', and there is documentation of the ActorSystem
class. But neither of these help a great deal in explaining why a user of Akka can't just rely on Akka to manage this under-the-hood.
What are the implications of sharing the same ActorSystem
object or creating a new one each time?
What are the best practices here? Passing around an ActorSystem
all the time seems surprisingly heavy-handed.
Some examples give the ActorSystem
a name: ActorSystem("MySystem")
others just call ActorSystem()
. What difference does this make, and what if you use the same name twice?
Does akka-testkit
require that you share a common ActorSystem
with the one you pass to the TestKit
constructor?
Companion object ActorSystem An actor system is a hierarchical group of actors which share common configuration, e.g. dispatchers, deployments, remote capabilities and addresses. It is also the entry point for creating or looking up actors. There are several possibilities for creating actors (see akka.
2) postStop() This method is used to release resources after stopping the Actor. It may be used for deregistering this Actor. Messages sent to a stopped actor will be redirected to the deadLetters of the ActorSystem.
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.
Akka “Typed Actors”, now replaced by Akka Typed, were an implementation of the Active Objects pattern. Essentially turning method invocations into asynchronous dispatch instead of synchronous that has been the default way since Smalltalk came out.
Creating an ActorSystem is very expensive, so you want to avoid creating a new one each time you need it. Also your actors should run in the same ActorSystem, unless there is a good reason for them not to. The name of the ActorSystem is also part the the path to the actors that run in it. E.g. if you create an actor in a system named MySystem
it will have a path like akka://MySystem/user/$a
. If you are in an actor context, you always have a reference to the ActorSystem. In an Actor you can call context.system
. I don't know what akka-testkit expects, but you could take a look at the akka tests.
So to sum it up, you should always use the same system, unless there is a good reason not to do so.
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