Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Akka Cookbook

You're reading from   Akka Cookbook Recipes for concurrent, fast, and reactive applications

Arrow left icon
Product type Paperback
Published in May 2017
Publisher Packt
ISBN-13 9781785288180
Length 414 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Vivek Mishra Vivek Mishra
Author Profile Icon Vivek Mishra
Vivek Mishra
Piyush Mishra Piyush Mishra
Author Profile Icon Piyush Mishra
Piyush Mishra
Héctor Veiga Ortiz Héctor Veiga Ortiz
Author Profile Icon Héctor Veiga Ortiz
Héctor Veiga Ortiz
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Diving into Akka FREE CHAPTER 2. Supervision and Monitoring 3. Routing Messages 4. Using Futures and Agents 5. Scheduling Actors and Other Utilities 6. Akka Persistence 7. Remoting and Akka Clustering 8. Akka Streams 9. Akka HTTP 10. Understanding Various Akka patterns 11. Microservices with Lagom

Communication between actors

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.

Getting ready

To step through this recipe we will import the Hello-Akka project in our IDE and other prerequisites are same as before.

How to do it...

We will create the following two actors here:

  • QueryActor: Sends a message to RandomNumberGenerator to generate a random number
  • RandomNumberGeneratorActor: Sends the generated random number to the QueryActor

The following are the steps for creating the actors:

  1. Create a Scala file, Communication.scala, in the package com.packt.chapter1.
  2. Create an object, Messages, which will contain the messages to be sent to the actors for communicating with each other.
  1. 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)
}
  1. Define RandomNumberGeneratorActor, which generates a random number and sends it back to the sender.
  2. 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)
}
}
  1. Create a queryActor, which sends messages to RandomNumberGeneratorActor 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")
}
}
  1. 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)
}
  1. 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

How it works...

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.

There's more...

In the following recipes, we will see how actors implement the master-slave work pulling pattern.

You have been reading a chapter from
Akka Cookbook
Published in: May 2017
Publisher: Packt
ISBN-13: 9781785288180
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image