Creating a dynamically resizable pool of actors
There are situations when we want a variable number of actors under the router to handle the increasing and decreasing number of requests. The following is a recipe for such situations:
Getting ready
To step through this recipe, we need to import the Hello-Akka
project. All prerequisites are the same as before.
How to do it...
- Create a Scala file,
ResizablePool.scala
, in the packagecom.packt.chapter3.
- Add the following import to the top of the file:
import akka.actor.{Props, Actor, ActorSystem} import akka.routing.{RoundRobinPool, DefaultResizer}
- Create a case object as the message to be sent to the actor:
case object Load
- Define a dummy actor as follows:
class LoadActor extends Actor { def receive = { case Load => println("Handing loads of requests") } }
- Create a test application as follows:
object ResizablePool extends App { val system = ActorSystem...