Akka:actor 当前邮箱大小或等待处理的消息数 [英] Akka: actor current mailbox size or number of messages awaiting processing

查看:47
本文介绍了Akka:actor 当前邮箱大小或等待处理的消息数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出等待参与者处理的待处理队列项目的数量.

我相信一定有一种方法可以从actor上下文或context.system.mailboxes等中引用

这是代码示例:

class SomeActor 扩展 Actor {覆盖定义接收 = {case ScanExisting =>{val queueSize = context.system.mailboxes... size ??}

解决方案

这种方法在 Akka 1.x 中存在,但在 Akka 2.0 中被移除.Roland Kuhn 写了一篇详细的博文(来源于此讨论在 Akka 用户列表上)解释了这一决定背后的理由.以下摘录概述了查询参与者邮箱大小的一些问题:

<块引用>
  • 从并发队列中获得一些大小的答案需要 O(n) 时间,即当你最痛苦的时候查询会很痛苦(如果持久邮箱在错误的时刻")

  • 答案不正确,即它不需要在处理此请求的开始或结束时匹配实际大小

  • 使其更正确"涉及记账,这严重限制了可扩展性

  • 即便如此,当您在代码中收到它时,这个数字可能已经完全改变了(例如,您的线程被安排了 100 毫秒,而在此期间您收到了 100.000 条新消息).

阅读整篇文章,然后重新考虑您获取演员邮箱大小的原因.如果你还想要:

<块引用>

如果你不能没有,可以很容易地编写你自己的邮箱实现,构建在 akka.dispatch 包中的特征并将簿记代码插入到 enqueue() 和 dequeue() 中.然后,您可以使用向下转换(邪恶)或在 akka.actor.Extension(推荐)中跟踪您的邮箱,以从您的演员内部访问统计信息并执行任何必要的操作.

但是等等:我有没有提到用时间戳标记延迟关键(但频率不是太高)的消息并在处理消息时对消息的年龄做出反应可能更容易?

所以,总而言之:虽然仍然有一种方法可以获取邮箱大小,但您可能永远不会真正需要它.

如果您希望演员邮箱大小用于监控目的,请查看 Lightbend 遥测Kamon.此外,Patrik Nordwall 写了一个 gist 记录超过配置限制时的邮箱大小.>

I am trying to find out number of pending queue items which awaiting to be processed by the actor.

I am sure there must be a method to which you can refer to from actor context or from context.system.mailboxes etc

Here is code example:

class SomeActor extends Actor {
  override def receive = {

    case ScanExisting => {
      val queueSize = context.system.mailboxes... size ??
}

解决方案

Such a method existed in Akka 1.x but was removed in Akka 2.0. Roland Kuhn wrote a detailed blog post (derived from this discussion on the Akka User List) explaining the rationale behind this decision. Here is an excerpt that outlines some problems with querying an actor's mailbox size:

  • it takes O(n) time to get some size answer from a concurrent queue, i.e. querying hurts when you will feel the pain the most (it might even take several seconds in case of durable mailboxes at the "wrong moment")

  • the answer is not correct, i.e. it does not need to match the real size at either the beginning or the end of processing this request

  • making it "more correct" involves book-keeping which severely limits scalability

  • and even then the number might have changed completely by the time you receive it in your code (e.g. your thread is scheduled out for 100ms and you get 100.000 new messages during that time).

Read the entire post, then reconsider your reason for obtaining the actor's mailbox size. If you still want it:

In case you cannot do without, it is quite easy to write your own mailbox implementation, building on the traits in the akka.dispatch package and inserting book-keeping code into enqueue() and dequeue(). Then you could either use down-casting (evil) or keep track of your mailboxes in an akka.actor.Extension (recommended) to access the stats from within your actor and do whatever is necessary.

But wait: did I mention that it might even be easier to tag latency-critical (but not too high frequency) messages with timestamps and react on the age of a message when processing it?

So, in summary: while there still is a way to get the mailbox size, you will probably never actually need it.

If you want the actor mailbox size for monitoring purposes, look into Lightbend Telemetry or Kamon. Also, Patrik Nordwall wrote a gist that logs the mailbox size when it exceeds a configured limit.

这篇关于Akka:actor 当前邮箱大小或等待处理的消息数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆