如何记录来自Akka的所有传入消息(Java)

编程入门 行业动态 更新时间:2024-10-28 16:19:17
本文介绍了如何记录来自Akka的所有传入消息(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Scala中,您可以使用LoggingReceive包装接收函数。您如何通过Java API实现相同的目的?

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 } }

推荐答案

Scala API具有 LoggingReceive 装饰器,因为部分函数字面量使其难以表达在所有情况下(例如此日志记录)要做的事情。

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).

在Java中,您不会遇到此问题,因为您的 onReceive 方法总是被调用,并且您可以放置​​一条日志记录语句在顶部可以查看参与者收到的所有消息。作为额外的奖励,您可以决定将日志记录在哪个级别上;-)

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 ;-)

如果您希望以配置​​设置为条件进行记录akka.actor.debug.receive (与Scala一样),例如,您可以说

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());

更多推荐

如何记录来自Akka的所有传入消息(Java)

本文发布于:2023-11-25 22:18:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631575.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:消息   Akka   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!