In an Akka-based application, there are many actors and they will have some way to communicate among themselves..
In this recipe, you will learn how two actors communicate with each other. For this, we need to import the same project, Hello-Akka
, in our IDE. Prerequisites are the same as in the previous recipes.
To step through this recipe we will import the Hello-Akka
project in our IDE and other prerequisites are same as before.
We will create the following two actors here:
QueryActor
: Sends a message toRandomNumberGenerator
to generate a random numberRandomNumberGeneratorActor
: Sends the generated random number to theQueryActor
The following are the steps for creating the actors:
- Create a Scala file,
Communication.scala
, in the packagecom.packt.chapter1
. - Create an object,
Messages
, which will contain the messages to be sent to the actors for communicating with each other.
- Add import to the top of the file:
import akka.actor.ActorRef
After adding the import add the code that follows:
object Messages { case class Done(randomNumber: Int) case object GiveMeRandomNumber case class Start(actorRef: ActorRef) }
- Define
RandomNumberGeneratorActor
, which generates a random number and sends it back to the sender. - Add the two imports given next to the top of the file:
import akka.actor.Actor import scala.util.Random._
Now add the code that follows:
class RandomNumberGeneratorActor extends Actor { import Messages._ override def receive: Receive = { case GiveMeRandomNumber => println("received a message to generate a random integer") val randomNumber = nextInt sender ! Done(randomNumber) } }
- Create a
queryActor
, which sends messages toRandomNumberGeneratorActor
and receives the random number:
class QueryActor extends Actor { import Messages._ override def receive: Receive = { case Start(actorRef) => println(s"send me the next random number") actorRef ! GiveMeRandomNumber case Done(randomNumber) => println(s"received a random number $randomNumber") } }
- Create an application object,
Communication
, to see the output:
object Communication extends App { import Messages._ val actorSystem = ActorSystem("HelloAkka") val randomNumberGenerator = actorSystem.actorOf(Props[RandomNumberGeneratorActor], "randomNumberGeneratorActor") val queryActor = actorSystem.actorOf(Props[QueryActor], "queryActor") queryActor ! Start(randomNumberGenerator) }
- Now run the application in the IDE or from the console, and the output will be displayed as follows:
send me the next random number received a message to generate a random integer received a random number 841431704
In step two, we see there is message object, which contains messages to be sent to the actors. Actors will use these messages for communication.
In step three, we define RandomNumberGeneratorActor
, which receives the message GiveMeRandomNumber
, and sends it to the sender as follows:
sender ! Done(randomNumber)
In step four, we define QueryActor
, which actually sends the message to RandomNumberGenerator
, and receives the result in case of Done
.
In step five, we create a test application to see the execution flow of the whole message.