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

Prioritizing messages that an actor receives

There are situations when you want your actor to process some particular messages first, and then move on to others. This means you want to give priority to some messages over others.

For such scenarios, Akka has comes up with priority mailbox, which lets you prioritize messages.

Getting ready

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

How to do it...

  1. Create a Scala file named PriorityMailBox.scala in package comi.packt.chapter1.
  2. Create an actor called MyPriorityActor as follows:
        class MyPriorityActor extends Actor { 
def receive: PartialFunction[Any, Unit] = {
// Int Messages
case x: Int => println(x)
// String Messages
case x: String => println(x)
// Long messages
case x: Long => println(x)
// other messages
case x => println(x)
}
}
  1. To prioritize the messages, create a priority mailbox as follows:
        class MyPriorityActorMailbox(settings:
ActorSystem.Settings, config: Config) extends
UnboundedPriorityMailbox (
// Create a new PriorityGenerator,
lower prio means more important
PriorityGenerator {
// Int Messages
case x: Int => 1
// String Messages
case x: String => 0
// Long messages
case x: Long => 2
// other messages
case _ => 3
})
  1. Add this configuration to application.conf:
        prio-dispatcher {  
mailbox-type =
"com.packt.chapter1..MyPriorityActorMailbox"
}
  1. Create an application, PriorityMailBoxApp, as shown in the following code:
        object PriorityMailBoxApp extends App { 
val actorSystem = ActorSystem("HelloAkka")
val myPriorityActor =
actorSystem.actorOf(Props[MyPriorityActor].withDispatcher
("prio-dispatcher"))
myPriorityActor ! 6.0
myPriorityActor ! 1
myPriorityActor ! 5.0
myPriorityActor ! 3
myPriorityActor ! "Hello"
myPriorityActor ! 5
myPriorityActor ! "I am priority actor"
myPriorityActor ! "I process string messages first,then
integer, long and others"
}
  1. Run the application in IDE or from the console. The following output will be displayed:
      Hello
I process string messages first,then integer, long and others
I am priority actor
1
3
5
6.0
5.0

How it works...

In step two, we just define an actor which processes Int, Long, String, and other messages.

In step four, we configure a prio-dispatcher with this MyPriorityActorMailbox.

In step five, we create an actor which will use the prio-dispatcher.

In step six, as we can see in the output, the string messages are processed first, because they were given highest priority.

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