How print all actors in akka system?

后端 未结 3 1465
醉梦人生
醉梦人生 2021-01-31 05:07

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)

3条回答
  •  [愿得一人]
    2021-01-31 05:45

    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.

提交回复
热议问题