Added functionality for a router client to retrieve the current routees of that router. See #1495

This commit is contained in:
Henrik Engstrom 2011-12-19 13:47:49 +01:00
parent 419b69438f
commit 3ff779cabd
2 changed files with 43 additions and 5 deletions

View file

@ -59,6 +59,34 @@ class RoutingSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
expectMsg(Terminated(router))
}
"be able to send their routees" in {
val doneLatch = new CountDownLatch(1)
class TheActor extends Actor {
val routee1 = context.actorOf(Props[TestActor], "routee1")
val routee2 = context.actorOf(Props[TestActor], "routee2")
val routee3 = context.actorOf(Props[TestActor], "routee3")
val router = context.actorOf(Props[TestActor].withRouter(ScatterGatherFirstCompletedRouter(routees = List(routee1, routee2, routee3))))
// Stop one of the routees - which should exclude it from the router's list of active routees
routee3 ! PoisonPill
def receive = {
case RouterRoutees(iterable)
iterable.exists(_ == "routee1") must be(true)
iterable.exists(_ == "routee2") must be(true)
iterable.exists(_ == "routee3") must be(false)
doneLatch.countDown()
case "doIt"
router ! CurrentRoutees
}
}
val theActor = system.actorOf(Props(new TheActor), "theActor")
theActor ! "doIt"
doneLatch.await(1, TimeUnit.SECONDS) must be(true)
}
}
"no router" must {

View file

@ -4,12 +4,8 @@
package akka.routing
import akka.actor._
import akka.japi.Creator
import java.lang.reflect.InvocationTargetException
import akka.config.ConfigurationException
import java.util.concurrent.atomic.AtomicInteger
import akka.util.{ ReflectiveAccess, Timeout }
import akka.AkkaException
import akka.util.Timeout
import scala.collection.JavaConversions._
import java.util.concurrent.TimeUnit
@ -33,6 +29,9 @@ private[akka] class RoutedActorRef(_system: ActorSystemImpl, _props: Props, _sup
def applyRoute(sender: ActorRef, message: Any): Iterable[Destination] = message match {
case _: AutoReceivedMessage Nil
case Terminated(_) Nil
case CurrentRoutees
sender ! RouterRoutees(_routees map (_.path.name))
Nil
case _
if (route.isDefinedAt(sender, message)) route(sender, message)
else Nil
@ -148,6 +147,17 @@ trait Router extends Actor {
*/
case class Broadcast(message: Any)
/**
* Sending this message to a router will make it send back its currently used routees.
* The routees will be sent as a RouterRoutees message to the "requester".
*/
case object CurrentRoutees
/**
* Message used to carry information about what routees the router is currently using.
*/
case class RouterRoutees(routees: Iterable[String])
/**
* For every message sent to a router, its route determines a set of destinations,
* where for each recipient a different sender may be specified; typically the