Akka Cookbook
Hector Veiga Ortiz, Piyush Mishra
- 414 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Akka Cookbook
Hector Veiga Ortiz, Piyush Mishra
About This Book
Learn how to use the Akka framework to build effective applications in ScalaAbout This Book⢠Covers a discussion on Lagomâthe newest launched Akka framework that is built to create complex microservices easily⢠The recipe approach of the book allows the reader to know important and independent concepts of Scala and Akka in a seamless manner⢠Provides a comprehensive understanding of the Akka actor model and implementing it to create reactive web applicationsWho This Book Is ForIf you are a Scala developer who wants to build scalable and concurrent applications, then this book is for you. Basic knowledge of Akka will help you take advantage of this book.What You Will Learn⢠Control an actor using the ContolAware mailbox⢠Test a fault-tolerant application using the Akka test kit⢠Create a parallel application using futures and agents⢠Package and deploy Akka application inside Docker⢠Deploy remote actors programmatically on different nodes⢠Integrate Streams with Akka actors⢠Install Lagom and create a Lagom projectIn DetailAkka 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.Style and approachThis highly practical recipe-based approach will allow you to build scalable, robust, and reactive applications using the Akka framework.
Frequently asked questions
Information
Understanding Various Akka patterns
- The Master Slave work pulling pattern
- Ordered termination of actors
- Shutdown patterns in Akka
- Scheduling periodic messages to an actor
- Throttling of messages while sending them to an actor
- Balancing workload across actors
- The aggregator pattern
- The CountDownLatch pattern
- Finite-state machine
- The pausable actor pattern
- Enveloping actor
Introduction
The Master Slave work pulling pattern
Getting ready
How to do it...
- Let's start with the master actor. This actor will keep a registry of worker actors and a queue of pending tasks. Create a file named MasterWorkPulling.scala inside the com.packt.chapter10 package. The content should look like this:
package com.packt.chapter10
import akka.actor.{Actor, ActorLogging, ActorRef, Terminated}
import com.packt.chapter10.WorkerWorkPulling._
import com.packt.chapter10.MasterWorkPulling._
import scala.collection.mutable
object MasterWorkPulling {
case object JoinWorker
case object DeregisterWorker
}
- Additionally, let's define the master actor in the same MasterWorkPulling.scala file:
class MasterWorkPulling(maxQueueSize: Int) extends Actor with ActorLogging {
val workers = mutable.Map.empty[ActorRef, WorkerState]
val pendingWork = mutable.Queue.empty[Work]
def receive = {
case JoinWorker =>
workers += sender -> Idle
context.watch(sender)
log.info(s"New worker registered [$sender]")
case Terminated(actorRef) => workers -= actorRef
case DeregisterWorker => workers -= sender
case PullWork if pendingWork.nonEmpty =>
log.info(s"Idle worker asking for work. Setting [$sender]
to [Working] state")
sender ! pendingWork.dequeue
workers += sender -> Working
case PullWork =>
log.info(s"Idle worker asking for work but no work
available. Setting [$sender] to [Idle] state")
workers += sender -> Idle
case work : Work if pendingWork.size > maxQueueSize =>
log.info(s"Work received but max pending work tasks
reached. Rejecting [$work]")
sender ! RejectWork(work)
case work : Work =>
pendingWork.enqueue(work)
workers.find(_._2 == Idle) match {
case Some((worker, _)) =>
val nextWork = pendingWork...