I have created akka system. Suppose there are some actors in it. How I can print all actors from akka system with their path? (for debug purposes)
ActorRefFactory, an interface which is implemented by ActorSystem and akka. actor. ActorContext. This means actors can be created top-level in the ActorSystem or as children of an existing actor, but only from within that actor. ActorRefs can be freely shared among actors by message passing.
An actor processes messages sequentially and (in Akka, at least) presents a single-threaded illusion (that is to say that under the hood, the dispatcher may execute the actor's logic on different threads from message to message, but from the actor's perspective there's only one thread).
Class DeadLetterWhen a message is sent to an Actor that is terminated before receiving the message, it will be sent as a DeadLetter to the ActorSystem's EventStream.
Behind the scenes Akka will run sets of actors on sets of real threads, where typically many actors share one thread, and subsequent invocations of one actor may end up being processed on different threads.
This reply by Roland Kuhn suggests that this isn't a completely trivial problem, but you can get pretty close (for actors that will reply to messages in a reasonable time) using the Identify
-ActorIdentity
request-response protocol that all actors obey.
Some untested code thrown together to illustrate the idea:
import akka.actor._
def receive = {
case 'listActors =>
context.actorSelection("/user/*") ! Identify()
case path: ActorPath =>
context.actorSelection(path / "*") ! Identify()
case ActorIdentity(_, Some(ref)) =>
log.info("Got actor " + ref.path.toString)
self ! ref.path
}
ActorSystem
has private method printTree
, that you can use for debugging.
1) Private method caller (from https://gist.github.com/jorgeortiz85/908035):
class PrivateMethodCaller(x: AnyRef, methodName: String) {
def apply(_args: Any*): Any = {
val args = _args.map(_.asInstanceOf[AnyRef])
def _parents: Stream[Class[_]] = Stream(x.getClass) #::: _parents.map(_.getSuperclass)
val parents = _parents.takeWhile(_ != null).toList
val methods = parents.flatMap(_.getDeclaredMethods)
val method = methods.find(_.getName == methodName).getOrElse(throw new IllegalArgumentException("Method " + methodName + " not found"))
method.setAccessible(true)
method.invoke(x, args: _*)
}
}
class PrivateMethodExposer(x: AnyRef) {
def apply(method: scala.Symbol): PrivateMethodCaller = new PrivateMethodCaller(x, method.name)
}
2) Usage
val res = new PrivateMethodExposer(system)('printTree)()
println(res)
Will print:
-> / LocalActorRefProvider$$anon$1 class akka.actor.LocalActorRefProvider$Guardian status=0 2 children
⌊-> system LocalActorRef class akka.actor.LocalActorRefProvider$SystemGuardian status=0 3 children
| ⌊-> deadLetterListener RepointableActorRef class akka.event.DeadLetterListener status=0 no children
| ⌊-> eventStreamUnsubscriber-1 RepointableActorRef class akka.event.EventStreamUnsubscriber status=0 no children
| ⌊-> log1-Logging$DefaultLogger RepointableActorRef class akka.event.Logging$DefaultLogger status=0 no children
⌊-> user LocalActorRef class akka.actor.LocalActorRefProvider$Guardian status=0 1 children
...
Beware, it can cause OOM If you have a lot of actors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With