Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the name of an Akka actor from within the actor itself?

Tags:

scala

akka

So, if I have an actor, I can give it a name. But, can I access that name internally? Example:

class Actorz extends Actor with ActorLogging {    val actorName = //??What function     def receive = {      case x => log.debug(actorName+": Received Message: "+x)    } }  val actor = system.actorOf(Props[Actorz], "named") actor ! "dogs" 

Now, I can pass its name as a constructor parameter. But, that seems like unnecessary duplication if there is a way to get the name internally... as it was set when I instantiated the actor using system.actorOf. API docs didn't seem to have anything.

like image 899
Dante Romero Avatar asked Nov 10 '13 07:11

Dante Romero


People also ask

How does Akka work internally?

Akka ensures that each instance of an actor runs in its own lightweight thread and that messages are processed one at a time. In this way, each actor's state can be reliably maintained without the developer needing to explicitly worry about synchronization or race conditions.

What is alternative to Akka?

Spring, Scala, Erlang, Kafka, and Spring Boot are the most popular alternatives and competitors to Akka.

What is ActorSystem?

The ActorSystem is a root actor in actors structure. An ActorSystem is a hierarchical group of actors which share common configuration, e.g. dispatchers, deployments, remote capabilities and addresses. It is also the entry point for creating or looking up actors.


1 Answers

From an Actor you can use self to get the ActorRef.

val actorName = self.path.name 

http://doc.akka.io/api/akka/2.2.3/#akka.actor.Actor

http://doc.akka.io/api/akka/2.2.3/#akka.actor.ActorRef

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

like image 137
Chris Martin Avatar answered Oct 22 '22 22:10

Chris Martin