How to log all incoming messages from Akka (Java)

走远了吗. 提交于 2019-12-13 13:26:07

问题


In Scala you can wrap the receive function with a LoggingReceive. How do you achieve the same from the Java API?

def receive = {
  LoggingReceive {
    case x ⇒ // do something
  }
}

回答1:


The Scala API has the LoggingReceive decorator because a partial function literal makes it awkward to express something to be done in all cases (like this logging).

In Java you don’t have this problem because your onReceive method is always called, and you can put a logging statement at the top to see all messages which are received by the actor. As an added bonus you get to decide at which level to log them ;-)

If you want to make your logging conditional on the config setting akka.actor.debug.receive (just as for Scala), then you can say for example

if (getContext().system().settings().AddLoggingReceive())
  log.debug("received message of type {}", msg.getClass());


来源:https://stackoverflow.com/questions/21000843/how-to-log-all-incoming-messages-from-akka-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!