scala - Listening to a remote akka ActorSystem's log stream -


i'm trying subscribe log stream of remote akka actorsystem, write console shows running logs of remote actors.

the way can think to: create actor within logging actorsystem, have actor subscribe actorsystem.eventstream , subscribe actor using actorselection within console's actorsystem.

but seems "indirect" since log pipeline like:

logging actor --> eventstream --> actor subscribed eventstream --> local actor 

is there easier way subscribe event stream?

from simplicity viewpoint, nothing forbids subscribe remote actor event stream without additional actor. akka documentation mentions:

the event stream local facility, meaning not distribute events other nodes in clustered environment (unless subscribe remote actor stream explicitly). if need broadcast events in akka cluster, without knowing recipients explicitly (i.e. obtaining actorrefs), may want into: distributed publish subscribe in cluster.

for illustration purposes, consider following code fragment corresponds remote system, 1 want subscribe to:

  class publisheractor extends actor actorlogging { // example publisher actor generate logs     context.system.scheduler.schedule(1.second, 1.second, self, "echo")     def receive = {       case "echo" ⇒         val x = random.nextint(100)         log.info(s"i got random number: $x")     }   }    def runpublisher() = {     println("=== running publisher node ===")     val system = actorsystem("publishersystem")     val selection = system.actorselection("akka.tcp://subscribersystem@127.0.0.1:2553/user/subscriber")     selection.resolveone(10.seconds) onsuccess { // when listener actor available,       case listener ⇒ system.eventstream.subscribe(listener, classof[logevent]) // subscribe event stream     }     val publisher = system.actorof(props[publisheractor], "publisher") // example publisher   } 

and corresponding subscriber in "local" node, want show logs:

  class subscriberactor extends actor actorlogging {     log.info("subscriber listening...")     def receive = {       case msg ⇒ log.info(s"got: $msg")     }   }    def runsubscriber() = {     println("=== running subscriber node ===")     val system = actorsystem("subscribersystem")     val listener = system.actorof(props[subscriberactor], "subscriber")   } 

however, there several caveats solution, fact publisher must running before subscriber (or subscriber implement retry policy until publisher up), location hardcoded , on. if want have more robust , resilient system , it's permissible, follow advice in documentation , use distributed publisher-subscriber in clustered environment poses several advantages similar amount of boilerplate.

hope helped!


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -