diff --git a/akka-remote/src/main/scala/remote/RemoteServer.scala b/akka-remote/src/main/scala/remote/RemoteServer.scala index 27b9af15a2..40b312f879 100644 --- a/akka-remote/src/main/scala/remote/RemoteServer.scala +++ b/akka-remote/src/main/scala/remote/RemoteServer.scala @@ -572,6 +572,29 @@ class RemoteServerHandler( server.typedActorsByUuid().get(uuid) } + private def findActorByIdOrUuid(id: String, uuid: String) : ActorRef = { + var actorRefOrNull = if (id.startsWith(UUID_PREFIX)) { + findActorByUuid(id.substring(UUID_PREFIX.length)) + } else { + findActorById(id) + } + if (actorRefOrNull eq null) { + actorRefOrNull = findActorByUuid(uuid) + } + actorRefOrNull + } + + private def findTypedActorByIdOrUuid(id: String, uuid: String) : AnyRef = { + var actorRefOrNull = if (id.startsWith(UUID_PREFIX)) { + findTypedActorByUuid(id.substring(UUID_PREFIX.length)) + } else { + findTypedActorById(id) + } + if (actorRefOrNull eq null) { + actorRefOrNull = findTypedActorByUuid(uuid) + } + actorRefOrNull + } /** * Creates a new instance of the actor with name, uuid and timeout specified as arguments. @@ -587,11 +610,7 @@ class RemoteServerHandler( val name = actorInfo.getTarget val timeout = actorInfo.getTimeout - val actorRefOrNull = if (id.startsWith(UUID_PREFIX)) { - findActorByUuid(id.substring(UUID_PREFIX.length)) - } else { - findActorById(id) - } + val actorRefOrNull = findActorByIdOrUuid(id, uuidFrom(uuid.getHigh,uuid.getLow).toString) if (actorRefOrNull eq null) { try { @@ -603,7 +622,7 @@ class RemoteServerHandler( actorRef.id = id actorRef.timeout = timeout actorRef.remoteAddress = None - server.actors.put(id, actorRef) // register by id + server.actorsByUuid.put(actorRef.uuid.toString, actorRef) // register by uuid actorRef } catch { case e => @@ -618,11 +637,7 @@ class RemoteServerHandler( val uuid = actorInfo.getUuid val id = actorInfo.getId - val typedActorOrNull = if (id.startsWith(UUID_PREFIX)) { - findTypedActorByUuid(id.substring(UUID_PREFIX.length)) - } else { - findTypedActorById(id) - } + val typedActorOrNull = findTypedActorByIdOrUuid(id, uuidFrom(uuid.getHigh,uuid.getLow).toString) if (typedActorOrNull eq null) { val typedActorInfo = actorInfo.getTypedActorInfo @@ -639,7 +654,7 @@ class RemoteServerHandler( val newInstance = TypedActor.newInstance( interfaceClass, targetClass.asInstanceOf[Class[_ <: TypedActor]], actorInfo.getTimeout).asInstanceOf[AnyRef] - server.typedActors.put(id, newInstance) // register by id + server.typedActors.put(uuidFrom(uuid.getHigh,uuid.getLow).toString, newInstance) // register by uuid newInstance } catch { case e => diff --git a/akka-remote/src/test/scala/remote/ClientInitiatedRemoteActorSpec.scala b/akka-remote/src/test/scala/remote/ClientInitiatedRemoteActorSpec.scala index 284ba0b87b..ba550dc2aa 100644 --- a/akka-remote/src/test/scala/remote/ClientInitiatedRemoteActorSpec.scala +++ b/akka-remote/src/test/scala/remote/ClientInitiatedRemoteActorSpec.scala @@ -56,6 +56,15 @@ object ClientInitiatedRemoteActorSpec { SendOneWayAndReplySenderActor.latch.countDown } } + + class MyActorCustomConstructor extends Actor { + var prefix = "default-" + var count = 0 + def receive = { + case "incrPrefix" => count += 1; prefix = "" + count + "-" + case msg: String => self.reply(prefix + msg) + } + } } class ClientInitiatedRemoteActorSpec extends JUnitSuite { @@ -150,6 +159,26 @@ class ClientInitiatedRemoteActorSpec extends JUnitSuite { assert("Expected exception; to test fault-tolerance" === e.getMessage()) } actor.stop - } + } + + @Test + def shouldRegisterActorByUuid { + val actor1 = actorOf[MyActorCustomConstructor] + actor1.makeRemote(HOSTNAME, PORT1) + actor1.start + actor1 ! "incrPrefix" + assert((actor1 !! "test").get === "1-test") + actor1 ! "incrPrefix" + assert((actor1 !! "test").get === "2-test") + + val actor2 = actorOf[MyActorCustomConstructor] + actor2.makeRemote(HOSTNAME, PORT1) + actor2.start + + assert((actor2 !! "test").get === "default-test") + + actor1.stop + actor2.stop + } }