Removed exposure of RoutedActorRef in RouterConfig. See #1618

This commit is contained in:
Patrik Nordwall 2012-01-11 11:30:32 +01:00
parent b43a8440a1
commit 409cbaf8e2
3 changed files with 24 additions and 22 deletions

View file

@ -413,12 +413,12 @@ class RoutingSpec extends AkkaSpec(RoutingSpec.config) with DefaultTimeout with
"custom router" must { "custom router" must {
"be started when constructed" in { "be started when constructed" in {
val routedActor = system.actorOf(Props[TestActor].withRouter(VoteCountRouter)) val routedActor = system.actorOf(Props[TestActor].withRouter(VoteCountRouter()))
routedActor.isTerminated must be(false) routedActor.isTerminated must be(false)
} }
"count votes as intended - not as in Florida" in { "count votes as intended - not as in Florida" in {
val routedActor = system.actorOf(Props().withRouter(VoteCountRouter)) val routedActor = system.actorOf(Props().withRouter(VoteCountRouter()))
routedActor ! DemocratVote routedActor ! DemocratVote
routedActor ! DemocratVote routedActor ! DemocratVote
routedActor ! RepublicanVote routedActor ! RepublicanVote
@ -462,12 +462,10 @@ class RoutingSpec extends AkkaSpec(RoutingSpec.config) with DefaultTimeout with
//#crActors //#crActors
//#crRouter //#crRouter
object VoteCountRouter extends RouterConfig { case class VoteCountRouter() extends RouterConfig {
//#crRoute //#crRoute
def createRoute(props: Props, def createRoute(routeeProps: Props, actorContext: ActorContext): Route = {
actorContext: ActorContext,
ref: RoutedActorRef): Route = {
val democratActor = actorContext.actorOf(Props(new DemocratActor()), "d") val democratActor = actorContext.actorOf(Props(new DemocratActor()), "d")
val republicanActor = actorContext.actorOf(Props(new RepublicanActor()), "r") val republicanActor = actorContext.actorOf(Props(new RepublicanActor()), "r")
val routees = Vector[ActorRef](democratActor, republicanActor) val routees = Vector[ActorRef](democratActor, republicanActor)

View file

@ -42,7 +42,7 @@ private[akka] class RoutedActorRef(_system: ActorSystemImpl, _props: Props, _sup
abandonedRoutees foreach underlying.unwatch abandonedRoutees foreach underlying.unwatch
} }
val route = _props.routerConfig.createRoute(routeeProps, actorContext, this) val route = _props.routerConfig.createRoute(routeeProps, actorContext)
// initial resize, before message send // initial resize, before message send
resize() resize()
@ -111,14 +111,14 @@ private[akka] class RoutedActorRef(_system: ActorSystemImpl, _props: Props, _sup
* do the locking yourself! * do the locking yourself!
* *
* '''Caution:''' Please note that the [[akka.routing.Router]] which needs to * '''Caution:''' Please note that the [[akka.routing.Router]] which needs to
* be returned by `apply()` should not send a message to itself in its * be returned by `createActor()` should not send a message to itself in its
* constructor or `preStart()` or publish its self reference from there: if * constructor or `preStart()` or publish its self reference from there: if
* someone tries sending a message to that reference before the constructor of * someone tries sending a message to that reference before the constructor of
* RoutedActorRef has returned, there will be a `NullPointerException`! * RoutedActorRef has returned, there will be a `NullPointerException`!
*/ */
trait RouterConfig { trait RouterConfig {
def createRoute(props: Props, actorContext: ActorContext, ref: RoutedActorRef): Route def createRoute(routeeProps: Props, actorContext: ActorContext): Route
def createActor(): Router = new Router {} def createActor(): Router = new Router {}
@ -171,15 +171,15 @@ trait RouterConfig {
* @see akka.routing.RouterConfig * @see akka.routing.RouterConfig
*/ */
abstract class CustomRouterConfig extends RouterConfig { abstract class CustomRouterConfig extends RouterConfig {
override def createRoute(props: Props, context: ActorContext, ref: RoutedActorRef): Route = { override def createRoute(props: Props, context: ActorContext): Route = {
val customRoute = createCustomRoute(props, context, ref) val customRoute = createCustomRoute(props, context)
{ {
case (sender, message) customRoute.destinationsFor(sender, message) case (sender, message) customRoute.destinationsFor(sender, message)
} }
} }
def createCustomRoute(props: Props, context: ActorContext, ref: RoutedActorRef): CustomRoute def createCustomRoute(props: Props, context: ActorContext): CustomRoute
protected def registerRoutees(context: ActorContext, routees: java.util.List[ActorRef]): Unit = { protected def registerRoutees(context: ActorContext, routees: java.util.List[ActorRef]): Unit = {
import scala.collection.JavaConverters._ import scala.collection.JavaConverters._
@ -251,23 +251,23 @@ case class Destination(sender: ActorRef, recipient: ActorRef)
* Oxymoron style. * Oxymoron style.
*/ */
case object NoRouter extends RouterConfig { case object NoRouter extends RouterConfig {
def createRoute(props: Props, actorContext: ActorContext, ref: RoutedActorRef): Route = null def createRoute(props: Props, actorContext: ActorContext): Route = null
} }
/** /**
* Router configuration which has no default, i.e. external configuration is required. * Router configuration which has no default, i.e. external configuration is required.
*/ */
case object FromConfig extends RouterConfig { case object FromConfig extends RouterConfig {
def createRoute(props: Props, actorContext: ActorContext, ref: RoutedActorRef): Route = def createRoute(props: Props, actorContext: ActorContext): Route =
throw new ConfigurationException("router " + ref + " needs external configuration from file (e.g. application.conf)") throw new ConfigurationException("router " + actorContext.self + " needs external configuration from file (e.g. application.conf)")
} }
/** /**
* Java API: Router configuration which has no default, i.e. external configuration is required. * Java API: Router configuration which has no default, i.e. external configuration is required.
*/ */
case class FromConfig() extends RouterConfig { case class FromConfig() extends RouterConfig {
def createRoute(props: Props, actorContext: ActorContext, ref: RoutedActorRef): Route = def createRoute(props: Props, actorContext: ActorContext): Route =
throw new ConfigurationException("router " + ref + " needs external configuration from file (e.g. application.conf)") throw new ConfigurationException("router " + actorContext.self + " needs external configuration from file (e.g. application.conf)")
} }
object RoundRobinRouter { object RoundRobinRouter {
@ -324,9 +324,10 @@ trait RoundRobinLike { this: RouterConfig ⇒
def routees: Iterable[String] def routees: Iterable[String]
def createRoute(props: Props, context: ActorContext, ref: RoutedActorRef): Route = { def createRoute(props: Props, context: ActorContext): Route = {
createAndRegisterRoutees(props, context, nrOfInstances, routees) createAndRegisterRoutees(props, context, nrOfInstances, routees)
val ref = context.self.asInstanceOf[RoutedActorRef]
val next = new AtomicLong(0) val next = new AtomicLong(0)
def getNext(): ActorRef = { def getNext(): ActorRef = {
@ -404,7 +405,8 @@ trait RandomLike { this: RouterConfig ⇒
override def initialValue = SecureRandom.getInstance("SHA1PRNG") override def initialValue = SecureRandom.getInstance("SHA1PRNG")
} }
def createRoute(props: Props, context: ActorContext, ref: RoutedActorRef): Route = { def createRoute(props: Props, context: ActorContext): Route = {
val ref = context.self.asInstanceOf[RoutedActorRef]
createAndRegisterRoutees(props, context, nrOfInstances, routees) createAndRegisterRoutees(props, context, nrOfInstances, routees)
def getNext(): ActorRef = { def getNext(): ActorRef = {
@ -476,7 +478,8 @@ trait BroadcastLike { this: RouterConfig ⇒
def routees: Iterable[String] def routees: Iterable[String]
def createRoute(props: Props, context: ActorContext, ref: RoutedActorRef): Route = { def createRoute(props: Props, context: ActorContext): Route = {
val ref = context.self.asInstanceOf[RoutedActorRef]
createAndRegisterRoutees(props, context, nrOfInstances, routees) createAndRegisterRoutees(props, context, nrOfInstances, routees)
{ {
@ -545,7 +548,8 @@ trait ScatterGatherFirstCompletedLike { this: RouterConfig ⇒
def within: Duration def within: Duration
def createRoute(props: Props, context: ActorContext, ref: RoutedActorRef): Route = { def createRoute(props: Props, context: ActorContext): Route = {
val ref = context.self.asInstanceOf[RoutedActorRef]
createAndRegisterRoutees(props, context, nrOfInstances, routees) createAndRegisterRoutees(props, context, nrOfInstances, routees)
{ {

View file

@ -107,7 +107,7 @@ public class CustomRouterDocTestBase {
//#crRoute //#crRoute
@Override @Override
public CustomRoute createCustomRoute(Props props, ActorContext context, RoutedActorRef ref) { public CustomRoute createCustomRoute(Props props, ActorContext context) {
final ActorRef democratActor = context.actorOf(new Props(DemocratActor.class), "d"); final ActorRef democratActor = context.actorOf(new Props(DemocratActor.class), "d");
final ActorRef republicanActor = context.actorOf(new Props(RepublicanActor.class), "r"); final ActorRef republicanActor = context.actorOf(new Props(RepublicanActor.class), "r");
List<ActorRef> routees = Arrays.asList(new ActorRef[] { democratActor, republicanActor }); List<ActorRef> routees = Arrays.asList(new ActorRef[] { democratActor, republicanActor });