From e20866c9827e7790f25dd1b1e2e8b10b5c3e8c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bone=CC=81r?= Date: Tue, 11 Oct 2011 11:55:59 +0200 Subject: [PATCH] Moved method for creating a RoutedActorRef from 'Routing.actorOf' to 'Actor.actorOf' --- .../test/scala/akka/routing/RoutingSpec.scala | 42 +++++++++---------- .../src/main/java/akka/actor/Actors.java | 8 ---- .../src/main/scala/akka/actor/Actor.scala | 5 +++ .../scala/akka/actor/ActorRefProvider.scala | 22 +++++++++- .../src/main/scala/akka/routing/Routing.scala | 18 -------- akka-docs/intro/code/tutorials/first/Pi.scala | 2 +- .../akka/remote/RemoteActorRefProvider.scala | 2 +- .../java/akka/tutorial/first/java/Pi.java | 3 +- .../src/main/scala/Pi.scala | 2 +- .../java/akka/tutorial/java/second/Pi.java | 3 +- .../src/main/scala/Pi.scala | 2 +- 11 files changed, 54 insertions(+), 55 deletions(-) diff --git a/akka-actor-tests/src/test/scala/akka/routing/RoutingSpec.scala b/akka-actor-tests/src/test/scala/akka/routing/RoutingSpec.scala index f7edfe78ea..d8859deb5b 100644 --- a/akka-actor-tests/src/test/scala/akka/routing/RoutingSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/routing/RoutingSpec.scala @@ -31,14 +31,14 @@ class RoutingSpec extends WordSpec with MustMatchers { val actor1 = Actor.actorOf[TestActor] val props = RoutedProps().withDirectRouter.withLocalConnections(List(actor1)) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") actor.isShutdown must be(false) } "throw ConfigurationException at construction when no connections" in { try { val props = RoutedProps().withDirectRouter - Routing.actorOf(props, "foo") + Actor.actorOf(props, "foo") fail() } catch { case e: ConfigurationException ⇒ @@ -57,7 +57,7 @@ class RoutingSpec extends WordSpec with MustMatchers { }) val props = RoutedProps().withDirectRouter.withLocalConnections(List(connection1)) - val routedActor = Routing.actorOf(props, "foo") + val routedActor = Actor.actorOf(props, "foo") routedActor ! "hello" routedActor ! "end" @@ -78,7 +78,7 @@ class RoutingSpec extends WordSpec with MustMatchers { }) val props = RoutedProps().withDirectRouter.withLocalConnections(List(connection1)) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") actor ! Broadcast(1) actor ! "end" @@ -95,14 +95,14 @@ class RoutingSpec extends WordSpec with MustMatchers { val actor1 = Actor.actorOf[TestActor] val props = RoutedProps().withRoundRobinRouter.withLocalConnections(List(actor1)) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") actor.isShutdown must be(false) } "throw ConfigurationException at construction when no connections" in { try { val props = RoutedProps().withRoundRobinRouter - Routing.actorOf(props, "foo") + Actor.actorOf(props, "foo") fail() } catch { case e: ConfigurationException ⇒ @@ -135,7 +135,7 @@ class RoutingSpec extends WordSpec with MustMatchers { //create the routed actor. val props = RoutedProps().withRoundRobinRouter.withLocalConnections(connections) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") //send messages to the actor. for (i ← 0 until iterationCount) { @@ -174,7 +174,7 @@ class RoutingSpec extends WordSpec with MustMatchers { }) val props = RoutedProps().withRoundRobinRouter.withLocalConnections(List(connection1, connection2)) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") actor ! Broadcast(1) actor ! Broadcast("end") @@ -197,7 +197,7 @@ class RoutingSpec extends WordSpec with MustMatchers { }) val props = RoutedProps().withRoundRobinRouter.withLocalConnections(List(connection1)) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") try { actor ? Broadcast(1) @@ -219,14 +219,14 @@ class RoutingSpec extends WordSpec with MustMatchers { val actor1 = Actor.actorOf[TestActor] val props = RoutedProps().withRandomRouter.withLocalConnections(List(actor1)) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") actor.isShutdown must be(false) } "throw ConfigurationException at construction when no connections" in { try { val props = RoutedProps().withRandomRouter - Routing.actorOf(props, "foo") + Actor.actorOf(props, "foo") fail() } catch { case e: ConfigurationException ⇒ @@ -257,7 +257,7 @@ class RoutingSpec extends WordSpec with MustMatchers { }) val props = RoutedProps().withRandomRouter.withLocalConnections(List(connection1, connection2)) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") actor ! Broadcast(1) actor ! Broadcast("end") @@ -280,7 +280,7 @@ class RoutingSpec extends WordSpec with MustMatchers { }) val props = RoutedProps().withRandomRouter.withLocalConnections(List(connection1)) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") try { actor ? Broadcast(1) @@ -305,7 +305,7 @@ class RoutingSpec extends WordSpec with MustMatchers { .withLocalConnections(List(newActor(0, Some(shutdownLatch)), newActor(1, Some(shutdownLatch)))) .withRouter(() ⇒ new ScatterGatherFirstCompletedRouter()) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") actor ! Broadcast(Stop(Some(0))) @@ -322,7 +322,7 @@ class RoutingSpec extends WordSpec with MustMatchers { .withLocalConnections(List(newActor(0, Some(shutdownLatch)), newActor(1, Some(shutdownLatch)))) .withRouter(() ⇒ new ScatterGatherFirstCompletedRouter()) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") actor ! Broadcast(Stop()) @@ -340,7 +340,7 @@ class RoutingSpec extends WordSpec with MustMatchers { .withLocalConnections(List(newActor(0), newActor(1))) .withRouter(() ⇒ new ScatterGatherFirstCompletedRouter()) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") (actor ? Broadcast("Hi!")).get.asInstanceOf[Int] must be(0) @@ -351,7 +351,7 @@ class RoutingSpec extends WordSpec with MustMatchers { .withLocalConnections(List(newActor(0), newActor(1))) .withRouter(() ⇒ new ScatterGatherFirstCompletedRouter()) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") (actor ? Broadcast(0)).get.asInstanceOf[Int] must be(1) } @@ -360,7 +360,7 @@ class RoutingSpec extends WordSpec with MustMatchers { val props = RoutedProps() .withLocalConnections(List(newActor(0))) .withRouter(() ⇒ new ScatterGatherFirstCompletedRouter()) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") actor.isShutdown must be(false) @@ -372,7 +372,7 @@ class RoutingSpec extends WordSpec with MustMatchers { .withRouter(() ⇒ new ScatterGatherFirstCompletedRouter()) try { - Routing.actorOf(props, "foo") + Actor.actorOf(props, "foo") fail() } catch { case e: ConfigurationException ⇒ @@ -402,7 +402,7 @@ class RoutingSpec extends WordSpec with MustMatchers { .withLocalConnections(connections) .withRouter(() ⇒ new ScatterGatherFirstCompletedRouter()) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") for (i ← 0 until iterationCount) { for (k ← 0 until connectionCount) { @@ -443,7 +443,7 @@ class RoutingSpec extends WordSpec with MustMatchers { .withLocalConnections(List(connection1, connection2)) .withRouter(() ⇒ new ScatterGatherFirstCompletedRouter()) - val actor = Routing.actorOf(props, "foo") + val actor = Actor.actorOf(props, "foo") actor ! Broadcast(1) actor ! Broadcast("end") diff --git a/akka-actor/src/main/java/akka/actor/Actors.java b/akka-actor/src/main/java/akka/actor/Actors.java index 88e3cc86fa..ea0c44e549 100644 --- a/akka-actor/src/main/java/akka/actor/Actors.java +++ b/akka-actor/src/main/java/akka/actor/Actors.java @@ -25,14 +25,6 @@ public class Actors { return Actor$.MODULE$.provider(); } - /** - * - * @return The actor registry - */ - public static ActorRefProviders registry() { - return Actor$.MODULE$.provider(); - } - /** * * @return diff --git a/akka-actor/src/main/scala/akka/actor/Actor.scala b/akka-actor/src/main/scala/akka/actor/Actor.scala index 51a9acb982..d7745e3c77 100644 --- a/akka-actor/src/main/scala/akka/actor/Actor.scala +++ b/akka-actor/src/main/scala/akka/actor/Actor.scala @@ -335,6 +335,11 @@ object Actor { */ def actorOf(props: Props, address: String): ActorRef = provider.actorOf(props, address) + /** + * Creates (or fetches) a routed actor reference, configured by the 'props: RoutedProps' configuration. + */ + def actorOf(props: RoutedProps, address: String = newUuid().toString): ActorRef = provider.actorOf(props, address) + /** * Use to spawn out a block of code in an event-driven actor. Will shut actor down when * the block has been executed. diff --git a/akka-actor/src/main/scala/akka/actor/ActorRefProvider.scala b/akka-actor/src/main/scala/akka/actor/ActorRefProvider.scala index 6be3618b17..5bcb2132c3 100644 --- a/akka-actor/src/main/scala/akka/actor/ActorRefProvider.scala +++ b/akka-actor/src/main/scala/akka/actor/ActorRefProvider.scala @@ -5,8 +5,10 @@ package akka.actor import akka.event.EventHandler -import akka.AkkaException +import akka.config.ConfigurationException +import akka.util.ReflectiveAccess import akka.routing._ +import akka.AkkaException /** * Interface for all ActorRef providers to implement. @@ -90,6 +92,22 @@ private[akka] class ActorRefProviders( actorFor(address, providersAsList) } + /** + * Creates (or fetches) a routed actor reference, configured by the 'props: RoutedProps' configuration. + */ + def actorOf(props: RoutedProps, address: String = newUuid().toString): ActorRef = { + //TODO Implement support for configuring by deployment ID etc + //TODO If address matches an already created actor (Ahead-of-time deployed) return that actor + //TODO If address exists in config, it will override the specified Props (should we attempt to merge?) + //TODO If the actor deployed uses a different config, then ignore or throw exception? + if (props.connectionManager.size == 0) throw new ConfigurationException("RoutedProps used for creating actor [" + address + "] has zero connections configured; can't create a router") + val clusteringEnabled = ReflectiveAccess.ClusterModule.isEnabled + val localOnly = props.localOnly + + if (clusteringEnabled && !props.localOnly) ReflectiveAccess.ClusterModule.newClusteredActorRef(props) + else new RoutedActorRef(props, address) + } + /** * Returns true if the actor was in the provider's cache and evicted successfully, else false. */ @@ -173,7 +191,7 @@ class LocalActorRefProvider extends ActorRefProvider { Vector.fill(nrOfInstances.factor)(new LocalActorRef(props, new UUID().toString, systemService)) else Nil - Some(Routing.actorOf(RoutedProps( + Some(Actor.actorOf(RoutedProps( routerFactory = routerFactory, connectionManager = new LocalConnectionManager(connections)))) diff --git a/akka-actor/src/main/scala/akka/routing/Routing.scala b/akka-actor/src/main/scala/akka/routing/Routing.scala index 0e085d284f..a4f5fd4d39 100644 --- a/akka-actor/src/main/scala/akka/routing/Routing.scala +++ b/akka-actor/src/main/scala/akka/routing/Routing.scala @@ -72,24 +72,6 @@ object Routing { */ case class Broadcast(message: Any) extends RoutingMessage - /** - * Creates (or fetches) a routed actor reference, configured by the 'props: RoutedProps' configuration. - * - * FIXME: will very likely be moved to the ActorRefProvider. - */ - def actorOf(props: RoutedProps, address: String = newUuid().toString): ActorRef = { - //TODO Implement support for configuring by deployment ID etc - //TODO If address matches an already created actor (Ahead-of-time deployed) return that actor - //TODO If address exists in config, it will override the specified Props (should we attempt to merge?) - //TODO If the actor deployed uses a different config, then ignore or throw exception? - if (props.connectionManager.size == 0) throw new ConfigurationException("RoutedProps used for creating actor [" + address + "] has zero connections configured; can't create a router") - val clusteringEnabled = ReflectiveAccess.ClusterModule.isEnabled - val localOnly = props.localOnly - - if (clusteringEnabled && !props.localOnly) ReflectiveAccess.ClusterModule.newClusteredActorRef(props) - else new RoutedActorRef(props, address) - } - def createCustomRouter(implClass: String): Router = { ReflectiveAccess.createInstance( implClass, diff --git a/akka-docs/intro/code/tutorials/first/Pi.scala b/akka-docs/intro/code/tutorials/first/Pi.scala index b75813841b..6fa132c2b7 100644 --- a/akka-docs/intro/code/tutorials/first/Pi.scala +++ b/akka-docs/intro/code/tutorials/first/Pi.scala @@ -69,7 +69,7 @@ object Pi extends App { val workers = Vector.fill(nrOfWorkers)(actorOf[Worker]) // wrap them with a load-balancing router - val router = Routing.actorOf(RoutedProps().withRoundRobinRouter.withLocalConnections(workers), "pi") + val router = Actor.actorOf(RoutedProps().withRoundRobinRouter.withLocalConnections(workers), "pi") //#create-workers //#master-receive diff --git a/akka-remote/src/main/scala/akka/remote/RemoteActorRefProvider.scala b/akka-remote/src/main/scala/akka/remote/RemoteActorRefProvider.scala index b7cb36d4cf..55fea49df8 100644 --- a/akka-remote/src/main/scala/akka/remote/RemoteActorRefProvider.scala +++ b/akka-remote/src/main/scala/akka/remote/RemoteActorRefProvider.scala @@ -107,7 +107,7 @@ class RemoteActorRefProvider extends ActorRefProvider { connections.keys foreach { useActorOnNode(_, address, props.creator) } - Some(Routing.actorOf(RoutedProps( + Some(Actor.actorOf(RoutedProps( routerFactory = routerFactory, connectionManager = connectionManager))) } diff --git a/akka-tutorials/akka-tutorial-first/src/main/java/akka/tutorial/first/java/Pi.java b/akka-tutorials/akka-tutorial-first/src/main/java/akka/tutorial/first/java/Pi.java index 70d7b09986..d43b0cdda5 100644 --- a/akka-tutorials/akka-tutorial-first/src/main/java/akka/tutorial/first/java/Pi.java +++ b/akka-tutorials/akka-tutorial-first/src/main/java/akka/tutorial/first/java/Pi.java @@ -9,6 +9,7 @@ import static akka.actor.Actors.poisonPill; import static java.util.Arrays.asList; import akka.actor.ActorRef; +import akka.actor.Actors; import akka.actor.UntypedActor; import akka.actor.UntypedActorFactory; import akka.routing.RoutedProps; @@ -110,7 +111,7 @@ public class Pi { workers.add(worker); } - router = Routing.actorOf(new RoutedProps().withRoundRobinRouter().withLocalConnections(workers), "pi"); + router = Actors.provider().actorOf(new RoutedProps().withRoundRobinRouter().withLocalConnections(workers), "pi"); } // message handler diff --git a/akka-tutorials/akka-tutorial-first/src/main/scala/Pi.scala b/akka-tutorials/akka-tutorial-first/src/main/scala/Pi.scala index 98a3c87bd0..2ebb267fe5 100644 --- a/akka-tutorials/akka-tutorial-first/src/main/scala/Pi.scala +++ b/akka-tutorials/akka-tutorial-first/src/main/scala/Pi.scala @@ -58,7 +58,7 @@ object Pi extends App { val workers = Vector.fill(nrOfWorkers)(actorOf[Worker]) // wrap them with a load-balancing router - val router = Routing.actorOf(RoutedProps().withRoundRobinRouter.withLocalConnections(workers), "pi") + val router = Actor.actorOf(RoutedProps().withRoundRobinRouter.withLocalConnections(workers), "pi") // message handler def receive = { diff --git a/akka-tutorials/akka-tutorial-second/src/main/java/akka/tutorial/java/second/Pi.java b/akka-tutorials/akka-tutorial-second/src/main/java/akka/tutorial/java/second/Pi.java index 0c4c6dd0c6..f19ad8593b 100644 --- a/akka-tutorials/akka-tutorial-second/src/main/java/akka/tutorial/java/second/Pi.java +++ b/akka-tutorials/akka-tutorial-second/src/main/java/akka/tutorial/java/second/Pi.java @@ -14,6 +14,7 @@ import akka.routing.Routing; import akka.routing.LocalConnectionManager; import scala.Option; import akka.actor.ActorRef; +import akka.actor.Actors; import akka.actor.Channel; import akka.actor.UntypedActor; import akka.actor.UntypedActorFactory; @@ -104,7 +105,7 @@ public class Pi { workers.add(worker); } - router = Routing.actorOf(new RoutedProps().withRoundRobinRouter().withLocalConnections(workers), "pi"); + router = Actors.provider().actorOf(new RoutedProps().withRoundRobinRouter().withLocalConnections(workers), "pi"); } @Override diff --git a/akka-tutorials/akka-tutorial-second/src/main/scala/Pi.scala b/akka-tutorials/akka-tutorial-second/src/main/scala/Pi.scala index 83d0a1d2ff..f7dfedf637 100644 --- a/akka-tutorials/akka-tutorial-second/src/main/scala/Pi.scala +++ b/akka-tutorials/akka-tutorial-second/src/main/scala/Pi.scala @@ -53,7 +53,7 @@ object Pi extends App { val workers = Vector.fill(nrOfWorkers)(actorOf[Worker]) // wrap them with a load-balancing router - val router = Routing.actorOf(RoutedProps( + val router = Actor.actorOf(RoutedProps( routerFactory = () ⇒ new RoundRobinRouter, connectionManager = new LocalConnectionManager(workers)), "pi")