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
About the Authors
About the Reviewer
Customer Feedback

Become/unbecome behavior of an actor

In some situations, we want our actor to change its behavior based on its state. This means that there are cases where an actor receives a message, and if its state changes or transitions, it changes the way further messages should be handled.

Thus, using become/unbecome, we can hot swap the actor functionality at runtime.

Getting ready

To step through this recipe, we need to import the Hello-Akka project in the IDE-like IntelliJ Idea. Prerequisites are the same as those in previous recipes.

How to do it...

  1. Create a file named BecomeUnbecome.scala in package com.packt.chapter1.
  2. Add the following imports to the top of the file:
        import{Props, ActorSystem, Actor} 
  1. Define an actor which changes its behavior based on whether the state is true or false, as shown in the following code:
        class BecomeUnBecomeActor extends Actor { 
          def receive: Receive = { 
            case true => context.become(isStateTrue) 
            case false => context.become(isStateFalse) 
            case _ => println("don't know what you want to say !! ") 
          def isStateTrue: Receive  = { 
            case msg : String => println(s"$msg") 
            case false => context.become(isStateFalse) 
          def isStateFalse: Receive  = { 
            case msg : Int => println(s"$msg") 
            case true =>  context.become(isStateTrue) 
  1. Create a test application, BecomeUnBecomeApp, as follows:
        object BecomeUnBecomeApp extends App { 
          val actorSystem = ActorSystem("HelloAkka") 
          val becomeUnBecome = 
          becomeUnBecome ! true 
          becomeUnBecome ! "Hello how are you?" 
          becomeUnBecome ! false 
          becomeUnBecome ! 1100 
          becomeUnBecome ! true 
          becomeUnBecome ! "What do u do?" 
  1. Run the application in an IDE like IntelliJ Idea or from the console; the output will be as follows:
      Hello how are you?
      What do u do?

How it works...

In step two, we define an actor, which changes its state to handle string and integer values.

If the state is true, we set the behavior as context.become(isStateTrue), and it starts handling string messages. If the state is false, we set the behavior as context.become(isStateFalse), and it starts handling integer messages.

In step four, we create the actor and send it to see if the output matches the functionality.