3

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.

  • What do you mean by "the value changes for each object and many objects can be created for a single actor"? – sourcedelica Nov 15 '12 at 14:32
  • @sourcedelica hashCode() for any object returns a unique value in Java. This value can be used for uniquely identifying that object. However for a single actor class, we would be creating multiple instances in the program. when we use hashCode() on these instances, we will be getting unique values each instance. Hence I couldn't use this method for identifying each actor uniquely. – Mohanasundaram Veeramuthu Nov 15 '12 at 15:10
  • Ok - so you mean identifying the type of actor, not the actor itself. Because the hashcode will identify the actor (at least in Akka). Are you using Akka? – sourcedelica Nov 15 '12 at 15:15
  • @sourcedelica No. This is a homework assignment and I was asked to use only scala or java libraries. – Mohanasundaram Veeramuthu Nov 15 '12 at 15:18
  • So you are trying to identify the type of actor not the actor itself? – sourcedelica Nov 15 '12 at 15:27

1 Answers1

5

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")
Ivan Meredith
  • 2,222
  • 14
  • 13