2012-02-10 14:13:40 +01:00
|
|
|
|
/**
|
2016-02-23 12:58:39 +01:00
|
|
|
|
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
|
2012-02-10 14:13:40 +01:00
|
|
|
|
*/
|
2012-05-22 11:37:09 +02:00
|
|
|
|
package docs.routing
|
2012-02-10 14:13:40 +01:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
|
import akka.testkit._
|
2012-08-31 13:56:37 +02:00
|
|
|
|
import akka.actor.{ ActorRef, Props, Actor }
|
2013-09-19 08:00:05 +02:00
|
|
|
|
import akka.actor.Terminated
|
|
|
|
|
|
import akka.routing.FromConfig
|
|
|
|
|
|
import akka.routing.RoundRobinPool
|
|
|
|
|
|
import akka.routing.RandomPool
|
|
|
|
|
|
import akka.routing.RoundRobinGroup
|
|
|
|
|
|
import akka.routing.SmallestMailboxPool
|
|
|
|
|
|
import akka.routing.BroadcastPool
|
|
|
|
|
|
import akka.routing.BroadcastGroup
|
|
|
|
|
|
import akka.routing.ConsistentHashingGroup
|
|
|
|
|
|
import akka.routing.ConsistentHashingPool
|
|
|
|
|
|
import akka.routing.DefaultResizer
|
|
|
|
|
|
import akka.routing.ScatterGatherFirstCompletedGroup
|
|
|
|
|
|
import akka.routing.RandomGroup
|
|
|
|
|
|
import akka.routing.ScatterGatherFirstCompletedPool
|
2014-01-10 17:14:10 +01:00
|
|
|
|
import akka.routing.BalancingPool
|
2014-06-05 21:56:05 +02:00
|
|
|
|
import akka.routing.TailChoppingGroup
|
|
|
|
|
|
import akka.routing.TailChoppingPool
|
2012-02-10 14:13:40 +01:00
|
|
|
|
|
|
|
|
|
|
object RouterDocSpec {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
val config = """
|
|
|
|
|
|
#//#config-round-robin-pool
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/router1 {
|
|
|
|
|
|
router = round-robin-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-round-robin-pool
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-round-robin-group
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/router3 {
|
|
|
|
|
|
router = round-robin-group
|
|
|
|
|
|
routees.paths = ["/user/workers/w1", "/user/workers/w2", "/user/workers/w3"]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-round-robin-group
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-random-pool
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/router5 {
|
|
|
|
|
|
router = random-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-random-pool
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-random-group
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/router7 {
|
|
|
|
|
|
router = random-group
|
|
|
|
|
|
routees.paths = ["/user/workers/w1", "/user/workers/w2", "/user/workers/w3"]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-random-group
|
2014-01-10 17:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
#//#config-balancing-pool
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/router9 {
|
|
|
|
|
|
router = balancing-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-balancing-pool
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-balancing-pool2
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/router9b {
|
|
|
|
|
|
router = balancing-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
pool-dispatcher {
|
|
|
|
|
|
attempt-teamwork = off
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-balancing-pool2
|
2015-06-03 13:14:57 +02:00
|
|
|
|
|
|
|
|
|
|
#//#config-balancing-pool3
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/router10b {
|
|
|
|
|
|
router = balancing-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
pool-dispatcher {
|
|
|
|
|
|
executor = "thread-pool-executor"
|
|
|
|
|
|
|
|
|
|
|
|
# allocate exactly 5 threads for this pool
|
|
|
|
|
|
thread-pool-executor {
|
|
|
|
|
|
core-pool-size-min = 5
|
|
|
|
|
|
core-pool-size-max = 5
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-balancing-pool3
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
#//#config-smallest-mailbox-pool
|
|
|
|
|
|
akka.actor.deployment {
|
2014-01-10 17:14:10 +01:00
|
|
|
|
/parent/router11 {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
router = smallest-mailbox-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-smallest-mailbox-pool
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-broadcast-pool
|
|
|
|
|
|
akka.actor.deployment {
|
2014-01-10 17:14:10 +01:00
|
|
|
|
/parent/router13 {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
router = broadcast-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-broadcast-pool
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-broadcast-group
|
|
|
|
|
|
akka.actor.deployment {
|
2014-01-10 17:14:10 +01:00
|
|
|
|
/parent/router15 {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
router = broadcast-group
|
|
|
|
|
|
routees.paths = ["/user/workers/w1", "/user/workers/w2", "/user/workers/w3"]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-broadcast-group
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-scatter-gather-pool
|
|
|
|
|
|
akka.actor.deployment {
|
2014-01-10 17:14:10 +01:00
|
|
|
|
/parent/router17 {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
router = scatter-gather-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
within = 10 seconds
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-scatter-gather-pool
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-scatter-gather-group
|
|
|
|
|
|
akka.actor.deployment {
|
2014-01-10 17:14:10 +01:00
|
|
|
|
/parent/router19 {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
router = scatter-gather-group
|
|
|
|
|
|
routees.paths = ["/user/workers/w1", "/user/workers/w2", "/user/workers/w3"]
|
|
|
|
|
|
within = 10 seconds
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-scatter-gather-group
|
2014-06-05 21:56:05 +02:00
|
|
|
|
|
|
|
|
|
|
#//#config-tail-chopping-pool
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/router21 {
|
|
|
|
|
|
router = tail-chopping-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
within = 10 seconds
|
|
|
|
|
|
tail-chopping-router.interval = 20 milliseconds
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-tail-chopping-pool
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-tail-chopping-group
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/router23 {
|
|
|
|
|
|
router = tail-chopping-group
|
|
|
|
|
|
routees.paths = ["/user/workers/w1", "/user/workers/w2", "/user/workers/w3"]
|
|
|
|
|
|
within = 10 seconds
|
|
|
|
|
|
tail-chopping-router.interval = 20 milliseconds
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-tail-chopping-group
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
#//#config-consistent-hashing-pool
|
|
|
|
|
|
akka.actor.deployment {
|
2014-06-05 21:56:05 +02:00
|
|
|
|
/parent/router25 {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
router = consistent-hashing-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
virtual-nodes-factor = 10
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-consistent-hashing-pool
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-consistent-hashing-group
|
|
|
|
|
|
akka.actor.deployment {
|
2014-06-05 21:56:05 +02:00
|
|
|
|
/parent/router27 {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
router = consistent-hashing-group
|
|
|
|
|
|
routees.paths = ["/user/workers/w1", "/user/workers/w2", "/user/workers/w3"]
|
|
|
|
|
|
virtual-nodes-factor = 10
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-consistent-hashing-group
|
|
|
|
|
|
|
2014-02-11 14:48:24 +01:00
|
|
|
|
#//#config-remote-round-robin-pool
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/remotePool {
|
|
|
|
|
|
router = round-robin-pool
|
|
|
|
|
|
nr-of-instances = 10
|
|
|
|
|
|
target.nodes = ["akka.tcp://app@10.0.0.2:2552", "akka://app@10.0.0.3:2552"]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-remote-round-robin-pool
|
|
|
|
|
|
|
|
|
|
|
|
#//#config-remote-round-robin-group
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/remoteGroup {
|
|
|
|
|
|
router = round-robin-group
|
|
|
|
|
|
routees.paths = [
|
|
|
|
|
|
"akka.tcp://app@10.0.0.1:2552/user/workers/w1",
|
|
|
|
|
|
"akka.tcp://app@10.0.0.2:2552/user/workers/w1",
|
|
|
|
|
|
"akka.tcp://app@10.0.0.3:2552/user/workers/w1"]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-remote-round-robin-group
|
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
#//#config-resize-pool
|
|
|
|
|
|
akka.actor.deployment {
|
2014-06-05 21:56:05 +02:00
|
|
|
|
/parent/router29 {
|
2013-09-19 08:00:05 +02:00
|
|
|
|
router = round-robin-pool
|
|
|
|
|
|
resizer {
|
|
|
|
|
|
lower-bound = 2
|
|
|
|
|
|
upper-bound = 15
|
|
|
|
|
|
messages-per-resize = 100
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-resize-pool
|
|
|
|
|
|
|
2015-08-21 21:03:57 -04:00
|
|
|
|
#//#config-optimal-size-exploring-resize-pool
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/parent/router31 {
|
|
|
|
|
|
router = round-robin-pool
|
|
|
|
|
|
optimal-size-exploring-resizer {
|
|
|
|
|
|
enabled = on
|
|
|
|
|
|
action-interval = 5s
|
|
|
|
|
|
downsize-after-underutilized-for = 72h
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-optimal-size-exploring-resize-pool
|
|
|
|
|
|
|
2013-10-16 13:02:35 +02:00
|
|
|
|
#//#config-pool-dispatcher
|
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
|
/poolWithDispatcher {
|
|
|
|
|
|
router = random-pool
|
|
|
|
|
|
nr-of-instances = 5
|
|
|
|
|
|
pool-dispatcher {
|
2014-01-10 17:14:10 +01:00
|
|
|
|
fork-join-executor.parallelism-min = 5
|
|
|
|
|
|
fork-join-executor.parallelism-max = 5
|
2013-10-16 13:02:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#//#config-pool-dispatcher
|
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
router-dispatcher {}
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
2014-03-07 13:20:01 +01:00
|
|
|
|
final case class Work(payload: String)
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
//#router-in-actor
|
2015-06-03 13:14:57 +02:00
|
|
|
|
import akka.routing.{ ActorRefRoutee, RoundRobinRoutingLogic, Router }
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
class Master extends Actor {
|
|
|
|
|
|
var router = {
|
|
|
|
|
|
val routees = Vector.fill(5) {
|
|
|
|
|
|
val r = context.actorOf(Props[Worker])
|
|
|
|
|
|
context watch r
|
|
|
|
|
|
ActorRefRoutee(r)
|
|
|
|
|
|
}
|
|
|
|
|
|
Router(RoundRobinRoutingLogic(), routees)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2013-12-03 16:34:26 +01:00
|
|
|
|
case w: Work =>
|
2014-01-16 15:16:35 +01:00
|
|
|
|
router.route(w, sender())
|
2013-12-03 16:34:26 +01:00
|
|
|
|
case Terminated(a) =>
|
2013-09-19 08:00:05 +02:00
|
|
|
|
router = router.removeRoutee(a)
|
|
|
|
|
|
val r = context.actorOf(Props[Worker])
|
|
|
|
|
|
context watch r
|
|
|
|
|
|
router = router.addRoutee(r)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//#router-in-actor
|
|
|
|
|
|
|
|
|
|
|
|
class Worker extends Actor {
|
|
|
|
|
|
def receive = {
|
2013-12-03 16:34:26 +01:00
|
|
|
|
case _ =>
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#create-worker-actors
|
|
|
|
|
|
class Workers extends Actor {
|
|
|
|
|
|
context.actorOf(Props[Worker], name = "w1")
|
|
|
|
|
|
context.actorOf(Props[Worker], name = "w2")
|
|
|
|
|
|
context.actorOf(Props[Worker], name = "w3")
|
|
|
|
|
|
// ...
|
|
|
|
|
|
//#create-worker-actors
|
|
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2013-12-03 16:34:26 +01:00
|
|
|
|
case _ =>
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Parent extends Actor {
|
|
|
|
|
|
|
|
|
|
|
|
//#paths
|
|
|
|
|
|
val paths = List("/user/workers/w1", "/user/workers/w2", "/user/workers/w3")
|
|
|
|
|
|
//#paths
|
|
|
|
|
|
|
|
|
|
|
|
//#round-robin-pool-1
|
|
|
|
|
|
val router1: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(Props[Worker]), "router1")
|
|
|
|
|
|
//#round-robin-pool-1
|
|
|
|
|
|
|
|
|
|
|
|
//#round-robin-pool-2
|
|
|
|
|
|
val router2: ActorRef =
|
|
|
|
|
|
context.actorOf(RoundRobinPool(5).props(Props[Worker]), "router2")
|
|
|
|
|
|
//#round-robin-pool-2
|
|
|
|
|
|
|
|
|
|
|
|
//#round-robin-group-1
|
|
|
|
|
|
val router3: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(), "router3")
|
|
|
|
|
|
//#round-robin-group-1
|
|
|
|
|
|
|
|
|
|
|
|
//#round-robin-group-2
|
|
|
|
|
|
val router4: ActorRef =
|
|
|
|
|
|
context.actorOf(RoundRobinGroup(paths).props(), "router4")
|
|
|
|
|
|
//#round-robin-group-2
|
|
|
|
|
|
|
|
|
|
|
|
//#random-pool-1
|
|
|
|
|
|
val router5: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(Props[Worker]), "router5")
|
|
|
|
|
|
//#random-pool-1
|
|
|
|
|
|
|
|
|
|
|
|
//#random-pool-2
|
|
|
|
|
|
val router6: ActorRef =
|
|
|
|
|
|
context.actorOf(RandomPool(5).props(Props[Worker]), "router6")
|
|
|
|
|
|
//#random-pool-2
|
|
|
|
|
|
|
|
|
|
|
|
//#random-group-1
|
|
|
|
|
|
val router7: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(), "router7")
|
|
|
|
|
|
//#random-group-1
|
|
|
|
|
|
|
|
|
|
|
|
//#random-group-2
|
|
|
|
|
|
val router8: ActorRef =
|
|
|
|
|
|
context.actorOf(RandomGroup(paths).props(), "router8")
|
|
|
|
|
|
//#random-group-2
|
|
|
|
|
|
|
2014-01-10 17:14:10 +01:00
|
|
|
|
//#balancing-pool-1
|
2013-09-19 08:00:05 +02:00
|
|
|
|
val router9: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(Props[Worker]), "router9")
|
2014-01-10 17:14:10 +01:00
|
|
|
|
//#balancing-pool-1
|
|
|
|
|
|
|
|
|
|
|
|
//#balancing-pool-2
|
|
|
|
|
|
val router10: ActorRef =
|
|
|
|
|
|
context.actorOf(BalancingPool(5).props(Props[Worker]), "router10")
|
2015-06-03 13:14:57 +02:00
|
|
|
|
//#balancing-pool-2
|
|
|
|
|
|
|
|
|
|
|
|
// #balancing-pool-3
|
|
|
|
|
|
val router10b: ActorRef =
|
|
|
|
|
|
context.actorOf(BalancingPool(20).props(Props[Worker]), "router10b")
|
|
|
|
|
|
//#balancing-pool-3
|
|
|
|
|
|
import scala.collection.JavaConversions._
|
|
|
|
|
|
for (i <- 1 to 100) router10b ! i
|
|
|
|
|
|
val threads10b = Thread.getAllStackTraces.keySet.filter { _.getName contains "router10b" }
|
|
|
|
|
|
val threads10bNr = threads10b.size
|
|
|
|
|
|
require(threads10bNr == 5, s"Expected 5 threads for router10b, had $threads10bNr! Got: ${threads10b.map(_.getName)}")
|
2014-01-10 17:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
//#smallest-mailbox-pool-1
|
|
|
|
|
|
val router11: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(Props[Worker]), "router11")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#smallest-mailbox-pool-1
|
|
|
|
|
|
|
|
|
|
|
|
//#smallest-mailbox-pool-2
|
2014-01-10 17:14:10 +01:00
|
|
|
|
val router12: ActorRef =
|
|
|
|
|
|
context.actorOf(SmallestMailboxPool(5).props(Props[Worker]), "router12")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#smallest-mailbox-pool-2
|
|
|
|
|
|
|
|
|
|
|
|
//#broadcast-pool-1
|
2014-01-10 17:14:10 +01:00
|
|
|
|
val router13: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(Props[Worker]), "router13")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#broadcast-pool-1
|
|
|
|
|
|
|
|
|
|
|
|
//#broadcast-pool-2
|
2014-01-10 17:14:10 +01:00
|
|
|
|
val router14: ActorRef =
|
|
|
|
|
|
context.actorOf(BroadcastPool(5).props(Props[Worker]), "router14")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#broadcast-pool-2
|
|
|
|
|
|
|
|
|
|
|
|
//#broadcast-group-1
|
2014-01-10 17:14:10 +01:00
|
|
|
|
val router15: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(), "router15")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#broadcast-group-1
|
|
|
|
|
|
|
|
|
|
|
|
//#broadcast-group-2
|
2014-01-10 17:14:10 +01:00
|
|
|
|
val router16: ActorRef =
|
|
|
|
|
|
context.actorOf(BroadcastGroup(paths).props(), "router16")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#broadcast-group-2
|
|
|
|
|
|
|
|
|
|
|
|
//#scatter-gather-pool-1
|
2014-01-10 17:14:10 +01:00
|
|
|
|
val router17: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(Props[Worker]), "router17")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#scatter-gather-pool-1
|
|
|
|
|
|
|
|
|
|
|
|
//#scatter-gather-pool-2
|
2014-01-10 17:14:10 +01:00
|
|
|
|
val router18: ActorRef =
|
2013-09-19 08:00:05 +02:00
|
|
|
|
context.actorOf(ScatterGatherFirstCompletedPool(5, within = 10.seconds).
|
2014-01-10 17:14:10 +01:00
|
|
|
|
props(Props[Worker]), "router18")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#scatter-gather-pool-2
|
|
|
|
|
|
|
|
|
|
|
|
//#scatter-gather-group-1
|
2014-01-10 17:14:10 +01:00
|
|
|
|
val router19: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(), "router19")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#scatter-gather-group-1
|
|
|
|
|
|
|
|
|
|
|
|
//#scatter-gather-group-2
|
2014-01-10 17:14:10 +01:00
|
|
|
|
val router20: ActorRef =
|
2013-09-19 08:00:05 +02:00
|
|
|
|
context.actorOf(ScatterGatherFirstCompletedGroup(paths,
|
2014-01-10 17:14:10 +01:00
|
|
|
|
within = 10.seconds).props(), "router20")
|
2014-06-05 21:56:05 +02:00
|
|
|
|
//#scatter-gather-group-2
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
2014-06-05 21:56:05 +02:00
|
|
|
|
//#tail-chopping-pool-1
|
2014-01-10 17:14:10 +01:00
|
|
|
|
val router21: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(Props[Worker]), "router21")
|
2014-06-05 21:56:05 +02:00
|
|
|
|
//#tail-chopping-pool-1
|
|
|
|
|
|
|
|
|
|
|
|
//#tail-chopping-pool-2
|
|
|
|
|
|
val router22: ActorRef =
|
|
|
|
|
|
context.actorOf(TailChoppingPool(5, within = 10.seconds, interval = 20.millis).
|
|
|
|
|
|
props(Props[Worker]), "router22")
|
|
|
|
|
|
//#tail-chopping-pool-2
|
|
|
|
|
|
|
|
|
|
|
|
//#tail-chopping-group-1
|
|
|
|
|
|
val router23: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(), "router23")
|
|
|
|
|
|
//#tail-chopping-group-1
|
|
|
|
|
|
|
|
|
|
|
|
//#tail-chopping-group-2
|
|
|
|
|
|
val router24: ActorRef =
|
|
|
|
|
|
context.actorOf(TailChoppingGroup(paths,
|
|
|
|
|
|
within = 10.seconds, interval = 20.millis).props(), "router24")
|
|
|
|
|
|
//#tail-chopping-group-2
|
|
|
|
|
|
|
|
|
|
|
|
//#consistent-hashing-pool-1
|
|
|
|
|
|
val router25: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(Props[Worker]), "router25")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#consistent-hashing-pool-1
|
|
|
|
|
|
|
|
|
|
|
|
//#consistent-hashing-pool-2
|
2014-06-05 21:56:05 +02:00
|
|
|
|
val router26: ActorRef =
|
2013-09-19 08:00:05 +02:00
|
|
|
|
context.actorOf(ConsistentHashingPool(5).props(Props[Worker]),
|
2014-06-05 21:56:05 +02:00
|
|
|
|
"router26")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#consistent-hashing-pool-2
|
|
|
|
|
|
|
|
|
|
|
|
//#consistent-hashing-group-1
|
2014-06-05 21:56:05 +02:00
|
|
|
|
val router27: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(), "router27")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#consistent-hashing-group-1
|
|
|
|
|
|
|
|
|
|
|
|
//#consistent-hashing-group-2
|
2014-06-05 21:56:05 +02:00
|
|
|
|
val router28: ActorRef =
|
|
|
|
|
|
context.actorOf(ConsistentHashingGroup(paths).props(), "router28")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#consistent-hashing-group-2
|
|
|
|
|
|
|
|
|
|
|
|
//#resize-pool-1
|
2014-06-05 21:56:05 +02:00
|
|
|
|
val router29: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(Props[Worker]), "router29")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#resize-pool-1
|
|
|
|
|
|
|
|
|
|
|
|
//#resize-pool-2
|
|
|
|
|
|
val resizer = DefaultResizer(lowerBound = 2, upperBound = 15)
|
2014-06-05 21:56:05 +02:00
|
|
|
|
val router30: ActorRef =
|
2013-09-19 08:00:05 +02:00
|
|
|
|
context.actorOf(RoundRobinPool(5, Some(resizer)).props(Props[Worker]),
|
2014-06-05 21:56:05 +02:00
|
|
|
|
"router30")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#resize-pool-2
|
|
|
|
|
|
|
2015-08-21 21:03:57 -04:00
|
|
|
|
//#optimal-size-exploring-resize-pool
|
|
|
|
|
|
val router31: ActorRef =
|
|
|
|
|
|
context.actorOf(FromConfig.props(Props[Worker]), "router31")
|
|
|
|
|
|
//#optimal-size-exploring-resize-pool
|
|
|
|
|
|
|
2012-02-10 14:13:40 +01:00
|
|
|
|
def receive = {
|
2013-12-03 16:34:26 +01:00
|
|
|
|
case _ =>
|
2012-02-10 14:13:40 +01:00
|
|
|
|
}
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Echo extends Actor {
|
|
|
|
|
|
def receive = {
|
2014-01-16 15:16:35 +01:00
|
|
|
|
case m => sender() ! m
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
2012-02-10 14:13:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
class RouterDocSpec extends AkkaSpec(RouterDocSpec.config) with ImplicitSender {
|
2012-02-10 14:13:40 +01:00
|
|
|
|
|
|
|
|
|
|
import RouterDocSpec._
|
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#create-workers
|
|
|
|
|
|
system.actorOf(Props[Workers], "workers")
|
|
|
|
|
|
//#create-workers
|
|
|
|
|
|
|
|
|
|
|
|
//#create-parent
|
|
|
|
|
|
system.actorOf(Props[Parent], "parent")
|
|
|
|
|
|
//#create-parent
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate dispatcher" in {
|
|
|
|
|
|
//#dispatchers
|
|
|
|
|
|
val router: ActorRef = system.actorOf(
|
2013-10-16 13:02:35 +02:00
|
|
|
|
// “head” router actor will run on "router-dispatcher" dispatcher
|
|
|
|
|
|
// Worker routees will run on "pool-dispatcher" dispatcher
|
|
|
|
|
|
RandomPool(5, routerDispatcher = "router-dispatcher").props(Props[Worker]),
|
|
|
|
|
|
name = "poolWithDispatcher")
|
2013-09-19 08:00:05 +02:00
|
|
|
|
//#dispatchers
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate broadcast" in {
|
|
|
|
|
|
val router = system.actorOf(RoundRobinPool(nrOfInstances = 5).props(Props[Echo]))
|
|
|
|
|
|
//#broadcastDavyJonesWarning
|
|
|
|
|
|
import akka.routing.Broadcast
|
|
|
|
|
|
router ! Broadcast("Watch out for Davy Jones' locker")
|
|
|
|
|
|
//#broadcastDavyJonesWarning
|
2013-12-17 14:25:56 +01:00
|
|
|
|
receiveN(5, 5.seconds.dilated) should have length (5)
|
2013-09-19 08:00:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate PoisonPill" in {
|
|
|
|
|
|
val router = watch(system.actorOf(RoundRobinPool(nrOfInstances = 5).props(Props[Echo])))
|
|
|
|
|
|
//#poisonPill
|
|
|
|
|
|
import akka.actor.PoisonPill
|
|
|
|
|
|
router ! PoisonPill
|
|
|
|
|
|
//#poisonPill
|
|
|
|
|
|
expectTerminated(router)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate broadcast of PoisonPill" in {
|
|
|
|
|
|
val router = watch(system.actorOf(RoundRobinPool(nrOfInstances = 5).props(Props[Echo])))
|
|
|
|
|
|
//#broadcastPoisonPill
|
|
|
|
|
|
import akka.actor.PoisonPill
|
|
|
|
|
|
import akka.routing.Broadcast
|
|
|
|
|
|
router ! Broadcast(PoisonPill)
|
|
|
|
|
|
//#broadcastPoisonPill
|
|
|
|
|
|
expectTerminated(router)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate Kill" in {
|
|
|
|
|
|
val router = watch(system.actorOf(RoundRobinPool(nrOfInstances = 5).props(Props[Echo])))
|
|
|
|
|
|
//#kill
|
|
|
|
|
|
import akka.actor.Kill
|
|
|
|
|
|
router ! Kill
|
|
|
|
|
|
//#kill
|
|
|
|
|
|
expectTerminated(router)
|
|
|
|
|
|
}
|
2012-02-10 14:13:40 +01:00
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
|
"demonstrate broadcast of Kill" in {
|
|
|
|
|
|
val router = watch(system.actorOf(RoundRobinPool(nrOfInstances = 5).props(Props[Echo])))
|
|
|
|
|
|
//#broadcastKill
|
|
|
|
|
|
import akka.actor.Kill
|
|
|
|
|
|
import akka.routing.Broadcast
|
|
|
|
|
|
router ! Broadcast(Kill)
|
|
|
|
|
|
//#broadcastKill
|
|
|
|
|
|
expectTerminated(router)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate remote deploy" in {
|
|
|
|
|
|
//#remoteRoutees
|
|
|
|
|
|
import akka.actor.{ Address, AddressFromURIString }
|
|
|
|
|
|
import akka.remote.routing.RemoteRouterConfig
|
|
|
|
|
|
val addresses = Seq(
|
2013-10-16 13:02:35 +02:00
|
|
|
|
Address("akka.tcp", "remotesys", "otherhost", 1234),
|
|
|
|
|
|
AddressFromURIString("akka.tcp://othersys@anotherhost:1234"))
|
2013-09-19 08:00:05 +02:00
|
|
|
|
val routerRemote = system.actorOf(
|
|
|
|
|
|
RemoteRouterConfig(RoundRobinPool(5), addresses).props(Props[Echo]))
|
|
|
|
|
|
//#remoteRoutees
|
|
|
|
|
|
}
|
2012-10-01 20:35:19 +02:00
|
|
|
|
}
|