Actor Name is not Unique InvalidActorNameException

8.6k views Asked by At

My environment is eclipse, play, akka, and scala. I am getting an error when trying to create a remote master akka actor.

I am not sure why I am getting this error:

[InvalidActorNameException: actor name hello is not unique!]

When the user submits their form, calculate is called:

options => {
     this.calculate(options.numWorkers.toInt, options.numElements.toInt,          options.numMessages.toInt) 
     //Redirect(routes.Application.)
     Ok(html.form(this.optionsForm))
}

Here is the code where I create the actor

val master = RemoteSystem.system.actorOf(Props[Master], "hello")

I also only create one instance of this actor and have tried many other names such as master, Master, and master1983274612987346198356.

Master is defined as:

class Master extends Actor {

    var pi: Double = _
    var nrOfResults: Int = _
    var start: Long = _

    def receive = {
        case calculate(numWorkers, numElements, numMessages) =>{
            for (i <- 0 until numWorkers) {
                val worker = RemoteSystem.system.actorOf(Props[Worker], "Worker")
                for(j <- 0 until numMessages) 
                {
                    worker ! Work(0, numElements)      
                }
            }
        }
        case PiResult(start, numTerms, acc) => println("Pi Result: " + acc)
    }

    override def preStart() {
        start = System.currentTimeMillis
    }

    override def postStop() {
        println(
"\n\tCalculation time: \t%s millis".format(System.currentTimeMillis - start))
    }
}

And the actor system is:

object RemoteSystem {
    val system = ActorSystem(
"RemoteCreation", ConfigFactory.load.getConfig("remotecreation"))
}

Where remotecreation is defined as:

remotecreation{
    include "common"

    akka {
        actor{
            deployment{
                /Worker{
                    remote="akka://[email protected]:2552"
                }
            }
        }
    remote.netty.port = 2554
    }
}
1

There are 1 answers

1
ndeverge On BEST ANSWER

It seems in the following code that you are creating many workers with the same name "Worker":

for (i <- 0 until numWorkers) {
      val worker = RemoteSystem.system.actorOf(Props[Worker], "Worker")
      for(j <- 0 until numMessages) 
      {
         worker ! Work(0, numElements)      
      }
}

You need to move the actor creation code (see this doc):

class Master extends Actor {
    val worker = RemoteSystem.system.actorOf(Props[Worker], "Worker")
    ...
}