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
Conferences
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

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 akka.actor.{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 =
actorSystem.actorOf(Props[BecomeUnBecomeActor])
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?
1100
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.

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