In this small recipe, we will create and understand an ActorSystem. Since we are done with our initial setup of the Hello-Akka
project, we don't need to make another project for it. We need to import the SBT project in an IDE like IntelliJ Idea.
If you are using IntelliJ Idea, then you must have Scala and the SBT plugin install on it.
Note
For importing an SBT Scala project in IntelliJ Idea, visit the following website:https://www.jetbrains.com/help/idea/2016.1/getting-started-with-sbt.html?origin=old_help#import_project.
It is very easy to create an ActorSystem in Akka:
- Create a package
com.packt.chapter1
inside theHello-Akka src
folder. We will keep all the source code in this package. - Inside the package, create a file, say
HelloAkkaActorSystem.scala
, which will contain the code. - Create a small scala application object,
HelloAkkaActorSystem
, and create anActorSystem
inside it:
package com.packt.chapter1 Import akka.actor.ActorSystem /** * Created by user */ object HelloAkkaActorSystem extends App { val actorSystem = ActorSystem("HelloAkka") println(actorSystem) }
- Now, run the application in IntelliJ Idea or in the console. It will print the output as follows:
akka://HelloAkka
Downloading the Akka actor dependency
In the recipe, we create a simple Scala object creating an ActorSystem, thereafter, we run the application.
In Akka, an ActorSystem is the starting point of any Akka application that we write.
Technically, an ActorSystem is a heavyweight structure per application, which allocates n number of threads. Thus, it is recommended to create one ActorSystem per application, until we have a reason to create another one.
ActorSystem is the home for the actors in which they live, it manages the life cycle of an actor and supervises them.On creation, an ActorSystem starts three actors:
/user
- The guardian actor: All user-defined actors are created as a child of the parent actor user, that is, when you create your actor in the ActorSystem, it becomes the child of the user guardian actor, and this guardian actor supervises your actors. If the guardian actor terminates, all your actors get terminated as well./system
- The system guardian: In Akka, logging is also implemented using actors. This special guardian shut downs the logged-in actors when all normal actors have terminated. It watches the user guardian actor, and upon termination of the guardian actor, it initiates its own shutdown./
- The root guardian: The root guardian is the grandparent of all the so-called top-level actors, and supervises all the top-level actors. Its purpose is to terminate the child upon any type of exception. It sets the ActorSystem status as terminated if the guardian actor is able to terminate all the child actors successfully.
Note
For more information on ActorSystem visit: http://doc.akka.io/docs/akka/2.4.1/general/actor-systems.html.