From 20abaaa0e4a84ce05c299bb5799363944e057278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bone=CC=81r?= Date: Wed, 6 Jul 2011 09:11:13 +0200 Subject: [PATCH] Changed semantics for 'Actor.actorOf' to be the same locally as on cluster: If an actor of the same logical address already exists in the registry then just return that, if not create a new one. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jonas Bonér --- .../src/main/scala/akka/actor/Actor.scala | 37 +++++++++++-------- .../deployment/DeploymentMultiJvmSpec.scala | 3 -- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/akka-actor/src/main/scala/akka/actor/Actor.scala b/akka-actor/src/main/scala/akka/actor/Actor.scala index 1bd4351b0d..f7644b2fb2 100644 --- a/akka-actor/src/main/scala/akka/actor/Actor.scala +++ b/akka-actor/src/main/scala/akka/actor/Actor.scala @@ -420,23 +420,28 @@ object Actor extends ListenerManagement { } private[akka] def newLocalActorRef(clazz: Class[_ <: Actor], address: String): ActorRef = { - new LocalActorRef(() ⇒ { - import ReflectiveAccess.{ createInstance, noParams, noArgs } - createInstance[Actor](clazz.asInstanceOf[Class[_]], noParams, noArgs) match { - case Right(actor) ⇒ actor - case Left(exception) ⇒ - val cause = exception match { - case i: InvocationTargetException ⇒ i.getTargetException - case _ ⇒ exception - } + registry.local.actorFor(address) match { + case Some(alreadyExistsForAddress) ⇒ // return already existing actor for this address + alreadyExistsForAddress + case None ⇒ // create (and store in registry) a new actor for this address + new LocalActorRef(() ⇒ { + import ReflectiveAccess.{ createInstance, noParams, noArgs } + createInstance[Actor](clazz.asInstanceOf[Class[_]], noParams, noArgs) match { + case Right(actor) ⇒ actor + case Left(exception) ⇒ + val cause = exception match { + case i: InvocationTargetException ⇒ i.getTargetException + case _ ⇒ exception + } - throw new ActorInitializationException( - "Could not instantiate Actor of " + clazz + - "\nMake sure Actor is NOT defined inside a class/trait," + - "\nif so put it outside the class/trait, f.e. in a companion object," + - "\nOR try to change: 'actorOf[MyActor]' to 'actorOf(new MyActor)'.", cause) - } - }, address, Transient) + throw new ActorInitializationException( + "Could not instantiate Actor of " + clazz + + "\nMake sure Actor is NOT defined inside a class/trait," + + "\nif so put it outside the class/trait, f.e. in a companion object," + + "\nOR try to change: 'actorOf[MyActor]' to 'actorOf(new MyActor)'.", cause) + } + }, address, Transient) + } } private def newClusterActorRef(factory: () ⇒ ActorRef, address: String, deploy: Deploy): ActorRef = { diff --git a/akka-cluster/src/test/scala/akka/cluster/deployment/DeploymentMultiJvmSpec.scala b/akka-cluster/src/test/scala/akka/cluster/deployment/DeploymentMultiJvmSpec.scala index 9db73c9e4f..a511681732 100644 --- a/akka-cluster/src/test/scala/akka/cluster/deployment/DeploymentMultiJvmSpec.scala +++ b/akka-cluster/src/test/scala/akka/cluster/deployment/DeploymentMultiJvmSpec.scala @@ -35,9 +35,6 @@ class DeploymentMultiJvmNode1 extends MasterClusterTestNode { barrier("perform-deployment-on-node-1", NrOfNodes) { Deployer.start() - // val deployments = Deployer.deploymentsInConfig - // deployments must not equal (Nil) - // ClusterDeployer.init(deployments) } barrier("lookup-deployment-node-2", NrOfNodes) {