Upgraded routing documentation to Akka 2.0. See #1063
This commit is contained in:
parent
73b79d6e3e
commit
41ce42c8f7
8 changed files with 495 additions and 189 deletions
|
|
@ -0,0 +1,21 @@
|
|||
package akka.docs.routing
|
||||
|
||||
import akka.routing.{ BasicNoBackoffFilter, SmallestMailboxSelector, DefaultActorPool }
|
||||
import akka.actor.{ ActorRef, Props, Actor }
|
||||
|
||||
//#testPool
|
||||
class TestPool extends Actor with DefaultActorPool with SmallestMailboxSelector with BasicNoBackoffFilter {
|
||||
|
||||
def capacity(delegates: Seq[ActorRef]) = 5
|
||||
protected def receive = _route
|
||||
def rampupRate = 0.1
|
||||
def selectionCount = 1
|
||||
def partialFill = true
|
||||
|
||||
def instance(defaults: Props) = context.actorOf(defaults.withCreator(new Actor {
|
||||
def receive = {
|
||||
case _ ⇒ // do something
|
||||
}
|
||||
}))
|
||||
}
|
||||
//#testPool
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package akka.docs.routing
|
||||
|
||||
import akka.actor.ActorRef
|
||||
|
||||
//#boundedCapacitor
|
||||
trait BoundedCapacitor {
|
||||
def lowerBound: Int
|
||||
def upperBound: Int
|
||||
|
||||
def capacity(delegates: Seq[ActorRef]): Int = {
|
||||
val current = delegates length
|
||||
var delta = _eval(delegates)
|
||||
val proposed = current + delta
|
||||
|
||||
if (proposed < lowerBound) delta += (lowerBound - proposed)
|
||||
else if (proposed > upperBound) delta -= (proposed - upperBound)
|
||||
|
||||
delta
|
||||
}
|
||||
|
||||
protected def _eval(delegates: Seq[ActorRef]): Int
|
||||
}
|
||||
//#boundedCapacitor
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package akka.docs.routing
|
||||
|
||||
import akka.routing.ActorPool
|
||||
import akka.actor.ActorRef
|
||||
|
||||
//#capacityStrategy
|
||||
trait CapacityStrategy {
|
||||
import ActorPool._
|
||||
|
||||
def pressure(delegates: Seq[ActorRef]): Int
|
||||
def filter(pressure: Int, capacity: Int): Int
|
||||
|
||||
protected def _eval(delegates: Seq[ActorRef]): Int =
|
||||
filter(pressure(delegates), delegates.size)
|
||||
}
|
||||
//#capacityStrategy
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
package akka.docs.routing
|
||||
|
||||
import akka.routing.{ ScatterGatherFirstCompletedRouter, BroadcastRouter, RandomRouter, RoundRobinRouter }
|
||||
import annotation.tailrec
|
||||
import akka.actor.{ Props, Actor }
|
||||
import akka.util.duration._
|
||||
import akka.dispatch.Await
|
||||
|
||||
case class FibonacciNumber(nbr: Int)
|
||||
|
||||
//#printlnActor
|
||||
class PrintlnActor extends Actor {
|
||||
def receive = {
|
||||
case msg ⇒
|
||||
println("Received message '%s' in actor %s".format(msg, self.path.name))
|
||||
}
|
||||
}
|
||||
|
||||
//#printlnActor
|
||||
|
||||
//#fibonacciActor
|
||||
class FibonacciActor extends Actor {
|
||||
def receive = {
|
||||
case FibonacciNumber(nbr) ⇒ sender.tell(fibonacci(nbr))
|
||||
}
|
||||
|
||||
private def fibonacci(n: Int): Int = {
|
||||
@tailrec
|
||||
def fib(n: Int, b: Int, a: Int): Int = n match {
|
||||
case 0 ⇒ a
|
||||
case _ ⇒ fib(n - 1, a + b, b)
|
||||
}
|
||||
|
||||
fib(n, 1, 0)
|
||||
}
|
||||
}
|
||||
|
||||
//#fibonacciActor
|
||||
|
||||
//#parentActor
|
||||
class ParentActor extends Actor {
|
||||
def receive = {
|
||||
case "rrr" ⇒
|
||||
//#roundRobinRouter
|
||||
val roundRobinRouter =
|
||||
context.actorOf(Props[PrintlnActor].withRouter(RoundRobinRouter()), "router")
|
||||
1 to 10 foreach {
|
||||
i ⇒ roundRobinRouter ! i
|
||||
}
|
||||
//#roundRobinRouter
|
||||
case "rr" ⇒
|
||||
//#randomRouter
|
||||
val randomRouter =
|
||||
context.actorOf(Props[PrintlnActor].withRouter(RandomRouter()), "router")
|
||||
1 to 10 foreach {
|
||||
i ⇒ randomRouter ! i
|
||||
}
|
||||
//#randomRouter
|
||||
case "br" ⇒
|
||||
//#broadcastRouter
|
||||
val broadcastRouter =
|
||||
context.actorOf(Props[PrintlnActor].withRouter(BroadcastRouter()), "router")
|
||||
broadcastRouter ! "this is a broadcast message"
|
||||
//#broadcastRouter
|
||||
case "sgfcr" ⇒
|
||||
//#scatterGatherFirstCompletedRouter
|
||||
val scatterGatherFirstCompletedRouter = context.actorOf(
|
||||
Props[FibonacciActor].withRouter(ScatterGatherFirstCompletedRouter()),
|
||||
"router")
|
||||
implicit val timeout = context.system.settings.ActorTimeout
|
||||
val futureResult = scatterGatherFirstCompletedRouter ? FibonacciNumber(10)
|
||||
val result = Await.result(futureResult, timeout.duration)
|
||||
//#scatterGatherFirstCompletedRouter
|
||||
println("The result of calculating Fibonacci for 10 is %d".format(result))
|
||||
}
|
||||
}
|
||||
|
||||
//#parentActor
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package akka.docs.routing
|
||||
|
||||
import com.typesafe.config.{ ConfigFactory, Config }
|
||||
import akka.actor.{ Actor, Props, ActorSystem }
|
||||
import akka.routing.RoundRobinRouter
|
||||
|
||||
case class Message(nbr: Int)
|
||||
|
||||
class ExampleActor extends Actor {
|
||||
def receive = {
|
||||
case Message(nbr) ⇒ println("Received %s in router %s".format(nbr, self.path.name))
|
||||
}
|
||||
}
|
||||
|
||||
object RouterWithConfigExample extends App {
|
||||
val system = ActorSystem("Example")
|
||||
//#configurableRouting
|
||||
val router = system.actorOf(Props[PrintlnActor].withRouter(RoundRobinRouter()),
|
||||
"exampleActor")
|
||||
//#configurableRouting
|
||||
1 to 10 foreach { i ⇒ router ! Message(i) }
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package akka.docs.routing
|
||||
|
||||
import akka.routing.RoundRobinRouter
|
||||
import akka.actor.{ ActorRef, Props, Actor, ActorSystem }
|
||||
|
||||
case class Message1(nbr: Int)
|
||||
|
||||
class ExampleActor1 extends Actor {
|
||||
def receive = {
|
||||
case Message1(nbr) ⇒ println("Received %s in router %s".format(nbr, self.path.name))
|
||||
}
|
||||
}
|
||||
|
||||
object RoutingProgrammaticallyExample extends App {
|
||||
val system = ActorSystem("RPE")
|
||||
//#programmaticRoutingNrOfInstances
|
||||
val router1 = system.actorOf(Props[ExampleActor1].withRouter(
|
||||
RoundRobinRouter(nrOfInstances = 5)))
|
||||
//#programmaticRoutingNrOfInstances
|
||||
1 to 6 foreach { i ⇒ router1 ! Message1(i) }
|
||||
|
||||
//#programmaticRoutingRoutees
|
||||
val actor1 = system.actorOf(Props[ExampleActor1])
|
||||
val actor2 = system.actorOf(Props[ExampleActor1])
|
||||
val actor3 = system.actorOf(Props[ExampleActor1])
|
||||
val routees = Vector[ActorRef](actor1, actor2, actor3)
|
||||
val router2 = system.actorOf(Props[ExampleActor1].withRouter(
|
||||
RoundRobinRouter(targets = routees)))
|
||||
//#programmaticRoutingRoutees
|
||||
1 to 6 foreach { i ⇒ router2 ! Message1(i) }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue