Defining the actor's behavior and state
In this recipe, we will define an actor, which will receive some messages, and apply its behavior to its state. After that, we will create that actor inside the actor system.
We will see what is meant by the terms behavior and the state of an actor.
Getting ready
To step through this recipe, we need to import the Hello-Akka
project in any IDE, like intelliJ Idea, and ensure that SBT is installed on our machine to build and run the Scala project from the console.
How to do it...
- Open the project
Hello-Akka
in an IDE like intelliJ Idea, and create a Scala file,BehaviorAndState.scala
, inside the package,com.packt.chapter1
. - Add an import to the top of the file:
import akka.actor.Actor
.
Inside the file, define the actor as follows:
class SummingActor extends Actor { // state inside the actor var sum = 0 // behaviour which is applied on the state override def receive: Receive = { // receives message an integer case x: Int => sum = sum + x println(s"my state as sum is $sum") // receives default message case _ => println("I don't know what are you talking about") } }
Here, we are not creating an actor, we are only defining the state and behavior.
- From the previous recipe, we know that ActorSystem is the home for actors, and we know how to create an ActorSystem. Now, we will create the actor inside it.
Add the following imports to the top of the file:
import akka.actor.Props import akka.actor.ActorSystem object BehaviourAndState extends App { val actorSystem = ActorSystem("HelloAkka") // creating an actor inside the actor system val actor = actorSystem.actorOf(Props[SummingActor]) // print actor path println(actor.path) }
- Run the preceding application from the IDE or from the console, and it will print the actor path.
You can run the application from the console using the following command:
sbt "runMain com.packt.chapter1.BehaviorAndState" akka://HelloAkka/user/$a
Here, HelloAkka
is the ActorSystem name, user
is the user guardian actor, that is, the parent actor for your actor, and $a
is the name of your actor.
You can also give a name to your actor:
val actor = actorSystem.actorOf(Props[SummingActor],
"summingactor")
If you run the application again, it will print the actor name as summingactor
.
The output will be as follows:
akka://HelloAkka/user/summingactor
How do we create an actor if it takes an argument in the constructor as shown in the following code:
class SummingActorWithConstructor(intitalSum: Int) extends Actor { // state inside the actor var sum = 0 // behaviour which is applied on the state override def receive: Receive = { // receives message an integer case x: Int => sum = intitalSum + sum + x println(s"my state as sum is $sum") // receives default message case _ => println("I don't know what are you talking about") } }
For this, we use the following code:
actorSystem.actorOf(Props(classOf[ SummingActorWithConstructor], 10), "summingactor")
How it works...
As we know from the previous recipe, the ActorSystem is a place where the actor lives.
In the preceding application, we define the actor with its state and behavior, and then create it inside Akka using the API provided by Akka.
In case of the summingactor
, the state is the variable sum
and the behavior is adding of the integer to the sum
as soon as the message arrives.
There's more...
There are some recommended practices related to creation of actors. For more details, you can check out the following link:
http://doc.akka.io/docs/akka/current/scala/actors.html.