2011-12-15 18:19:40 +01:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-12-15 18:19:40 +01:00
|
|
|
*/
|
2011-12-15 15:28:21 +01:00
|
|
|
package akka.docs.routing
|
|
|
|
|
|
|
|
|
|
import akka.routing.RoundRobinRouter
|
|
|
|
|
import akka.actor.{ ActorRef, Props, Actor, ActorSystem }
|
2012-01-10 15:53:27 +01:00
|
|
|
import akka.routing.DefaultResizer
|
2012-02-21 15:37:51 +01:00
|
|
|
import akka.routing.RemoteRouterConfig
|
2011-12-15 15:28:21 +01:00
|
|
|
|
|
|
|
|
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(
|
2011-12-17 16:33:29 +01:00
|
|
|
RoundRobinRouter(routees = routees)))
|
2011-12-15 15:28:21 +01:00
|
|
|
//#programmaticRoutingRoutees
|
|
|
|
|
1 to 6 foreach { i ⇒ router2 ! Message1(i) }
|
2012-01-10 15:53:27 +01:00
|
|
|
|
|
|
|
|
//#programmaticRoutingWithResizer
|
|
|
|
|
val resizer = DefaultResizer(lowerBound = 2, upperBound = 15)
|
|
|
|
|
val router3 = system.actorOf(Props[ExampleActor1].withRouter(
|
|
|
|
|
RoundRobinRouter(resizer = Some(resizer))))
|
|
|
|
|
//#programmaticRoutingWithResizer
|
|
|
|
|
1 to 6 foreach { i ⇒ router3 ! Message1(i) }
|
|
|
|
|
|
2012-02-21 15:37:51 +01:00
|
|
|
//#remoteRoutees
|
2012-02-27 10:28:20 +01:00
|
|
|
import akka.actor.{ Address, AddressFromURIString }
|
2012-02-21 15:37:51 +01:00
|
|
|
val addresses = Seq(
|
|
|
|
|
Address("akka", "remotesys", "otherhost", 1234),
|
2012-02-27 10:28:20 +01:00
|
|
|
AddressFromURIString("akka://othersys@anotherhost:1234"))
|
2012-02-21 15:37:51 +01:00
|
|
|
val routerRemote = system.actorOf(Props[ExampleActor1].withRouter(
|
|
|
|
|
RemoteRouterConfig(RoundRobinRouter(5), addresses)))
|
|
|
|
|
//#remoteRoutees
|
|
|
|
|
|
2011-12-15 15:28:21 +01:00
|
|
|
}
|