Book Image

Akka Cookbook

By : Vivek Mishra, Héctor Veiga Ortiz
Book Image

Akka Cookbook

By: Vivek Mishra, Héctor Veiga Ortiz

Overview of this book

Akka is an open source toolkit that simplifies the construction of distributed and concurrent applications on the JVM. This book will teach you how to develop reactive applications in Scala using the Akka framework. This book will show you how to build concurrent, scalable, and reactive applications in Akka. You will see how to create high performance applications, extend applications, build microservices with Lagom, and more. We will explore Akka's actor model and show you how to incorporate concurrency into your applications. The book puts a special emphasis on performance improvement and how to make an application available for users. We also make a special mention of message routing and construction. By the end of this book, you will be able to create a high-performing Scala application using the Akka framework.
Table of Contents (18 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

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.