2013-01-31 21:48:03 +13:00
|
|
|
|
/**
|
|
|
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
|
*/
|
|
|
|
|
|
package docs.routing
|
|
|
|
|
|
|
2013-03-31 03:04:12 +02:00
|
|
|
|
import akka.actor._
|
2013-01-31 21:48:03 +13:00
|
|
|
|
import akka.routing.ConsistentHashingRouter.ConsistentHashable
|
|
|
|
|
|
import akka.routing.FromConfig
|
|
|
|
|
|
import akka.routing.RoundRobinRouter
|
|
|
|
|
|
import akka.testkit._
|
|
|
|
|
|
import com.typesafe.config.ConfigFactory
|
|
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
|
|
|
|
|
|
|
object RouterViaProgramDocSpec {
|
|
|
|
|
|
case class Message1(nbr: Int)
|
|
|
|
|
|
|
|
|
|
|
|
class ExampleActor1 extends Actor {
|
|
|
|
|
|
def receive = {
|
2013-03-28 23:45:48 +01:00
|
|
|
|
case m @ Message1(nbr) ⇒ sender ! ((self, m))
|
2013-01-31 21:48:03 +13:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Echo extends Actor {
|
|
|
|
|
|
def receive = {
|
|
|
|
|
|
case m ⇒ sender ! m
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class RouterViaProgramDocSpec extends AkkaSpec with ImplicitSender {
|
|
|
|
|
|
import RouterViaProgramDocSpec._
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate routees from paths" in {
|
|
|
|
|
|
|
|
|
|
|
|
//#programmaticRoutingRouteePaths
|
|
|
|
|
|
val actor1 = system.actorOf(Props[ExampleActor1], "actor1")
|
|
|
|
|
|
val actor2 = system.actorOf(Props[ExampleActor1], "actor2")
|
|
|
|
|
|
val actor3 = system.actorOf(Props[ExampleActor1], "actor3")
|
|
|
|
|
|
val routees = Vector[String]("/user/actor1", "/user/actor2", "/user/actor3")
|
2013-03-31 03:04:12 +02:00
|
|
|
|
val router = system.actorOf(
|
|
|
|
|
|
Props().withRouter(RoundRobinRouter(routees = routees)))
|
2013-01-31 21:48:03 +13:00
|
|
|
|
//#programmaticRoutingRouteePaths
|
|
|
|
|
|
1 to 6 foreach { i ⇒ router ! Message1(i) }
|
|
|
|
|
|
val received = receiveN(6, 5.seconds.dilated)
|
|
|
|
|
|
1 to 6 foreach { i ⇒
|
|
|
|
|
|
val expectedActor = system.actorFor(routees((i - 1) % routees.length))
|
|
|
|
|
|
val expectedMsg = Message1(i)
|
|
|
|
|
|
received must contain[AnyRef]((expectedActor, expectedMsg))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate broadcast" in {
|
|
|
|
|
|
val router = system.actorOf(Props[Echo].withRouter(RoundRobinRouter(nrOfInstances = 5)))
|
|
|
|
|
|
//#broadcastDavyJonesWarning
|
|
|
|
|
|
import akka.routing.Broadcast
|
|
|
|
|
|
router ! Broadcast("Watch out for Davy Jones' locker")
|
|
|
|
|
|
//#broadcastDavyJonesWarning
|
|
|
|
|
|
receiveN(5, 5.seconds.dilated) must have length (5)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate PoisonPill" in {
|
2013-03-31 03:04:12 +02:00
|
|
|
|
val router = watch(system.actorOf(Props[Echo].withRouter(RoundRobinRouter(nrOfInstances = 5))))
|
2013-01-31 21:48:03 +13:00
|
|
|
|
//#poisonPill
|
|
|
|
|
|
import akka.actor.PoisonPill
|
|
|
|
|
|
router ! PoisonPill
|
|
|
|
|
|
//#poisonPill
|
2013-03-31 03:04:12 +02:00
|
|
|
|
expectMsgPF() { case Terminated(`router`) ⇒ () }
|
2013-01-31 21:48:03 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate broadcast of PoisonPill" in {
|
2013-03-31 03:04:12 +02:00
|
|
|
|
val router = watch(system.actorOf(Props[Echo].withRouter(RoundRobinRouter(nrOfInstances = 5))))
|
2013-01-31 21:48:03 +13:00
|
|
|
|
//#broadcastPoisonPill
|
|
|
|
|
|
import akka.actor.PoisonPill
|
|
|
|
|
|
import akka.routing.Broadcast
|
|
|
|
|
|
router ! Broadcast(PoisonPill)
|
|
|
|
|
|
//#broadcastPoisonPill
|
2013-03-31 03:04:12 +02:00
|
|
|
|
expectMsgPF() { case Terminated(`router`) ⇒ () }
|
2013-01-31 21:48:03 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate Kill" in {
|
2013-03-31 03:04:12 +02:00
|
|
|
|
val router = watch(system.actorOf(Props[Echo].withRouter(RoundRobinRouter(nrOfInstances = 5))))
|
2013-01-31 21:48:03 +13:00
|
|
|
|
//#kill
|
|
|
|
|
|
import akka.actor.Kill
|
|
|
|
|
|
router ! Kill
|
|
|
|
|
|
//#kill
|
2013-03-31 03:04:12 +02:00
|
|
|
|
expectMsgPF() { case Terminated(`router`) ⇒ () }
|
2013-01-31 21:48:03 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
"demonstrate broadcast of Kill" in {
|
2013-03-31 03:04:12 +02:00
|
|
|
|
val router = watch(system.actorOf(Props[Echo].withRouter(RoundRobinRouter(nrOfInstances = 5))))
|
2013-01-31 21:48:03 +13:00
|
|
|
|
//#broadcastKill
|
|
|
|
|
|
import akka.actor.Kill
|
|
|
|
|
|
import akka.routing.Broadcast
|
|
|
|
|
|
router ! Broadcast(Kill)
|
|
|
|
|
|
//#broadcastKill
|
2013-03-31 03:04:12 +02:00
|
|
|
|
expectMsgPF() { case Terminated(`router`) ⇒ () }
|
2013-01-31 21:48:03 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|