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.
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.
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.
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.
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)
}
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")
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 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.