简体   繁体   中英

How to unsubscribe Akka logs in run time

I have my own Actor that subscribe to Akka logs using configuration

loggers = ["path.MyActor"]

MyActor Receiving the log events, but I would like to stop receiving them in run time. Is there way to do that? (etc by getting the event bus of Akka logs)

You could call system.eventStream to unsubscribe MyActor . For example:

import akka.event.Logging._

case object UnsubscribeFromLogging

class MyActor extends Actor {
  def receive = {
    case InitializeLogger(_)                        => sender() ! LoggerInitialized
    case Error(cause, logSource, logClass, message) => // ...
    case Warning(logSource, logClass, message)      => // ...
    case Info(logSource, logClass, message)         => // ...
    case Debug(logSource, logClass, message)        => // ...

    case UnsubscribeFromLogging => context.system.eventStream.unsubscribe(self)
  }
}

The above example uses a custom UnsubscribeFromLogging message:

val myActor: ActorRef = ??? // reference to MyActor
myActor ! UnsubscribeFromLogging

You could pass the reference to MyActor to the unsubscribe method from another actor:

val myActor: ActorRef = ??? // reference to MyActor
context.system.eventStream.unsubscribe(myActor)

Either way, you need a reference to MyActor . You can obtain this by determining MyActor 's path (for example, have MyActor print self.path ) and passing this path to context.actorSelection .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM