In this recipe, you will learn how a parent sends messages to its child, and collects responses from them.
To step through this recipe, we need to import the Hello-Akka
project in the IDE.
- Create a file,
SendMesagesToChilds.scala
, in packagecom.packt.chapter2
. - Add the following imports to the top of the file:
import akka.actor.{ Props, ActorSystem, Actor, ActorRef }
- Create messages to be sent to the actors as follows:
case class DoubleValue(x: Int) case object CreateChild case object Send case class Response(x: Int)
- Define a child actor. It doubles the value sent to it.
class DoubleActor extends Actor { def receive = { case DoubleValue(number) => println(s"${self.path.name} Got the number $number") sender ! Response(number * 2) } }
- Define a parent actor. It creates child actors in its context, sends messages...