Search code examples
scalaactoridentify

Uniquely identifying actors in scala


I'm building a simple logging class in scala which would write all log information to a file. This is part of a homework assignment. Hence I cannot use the already available loggers in java or scala or akka libraries. Can any of you please tell how to uniquely identify actors in scala i.e., is there a resource ID or any other ID for each actor. If so, how can it be accessed?

I tried using hashCode() on the actor objects. But it does not give the expected result, as the value changes for each object and many objects can be created for a single actor.


Solution

  • If you are using akka actors you can get the name of the actor by looking at self.path (self is an ActorRef)

    http://doc.akka.io/api/akka/2.0.4/#akka.actor.ActorPath

    EDIT:

    If you are using scala actors then you could do something like...

    class MyActor(name: String) extends Actor { 
      def act() {
        receive {
          case _ => println("Message on actor: " + name)
        }
      } 
    }
    val actor1 = new MyActor("actor1")
    val actor2 = new MyActor("actor2")