【问题标题】:how to check size of stash in akka actor如何检查akka actor中的存储大小
【发布时间】:2019-01-01 21:40:16
【问题描述】:

我已经使用 Akka actor 中的 stash 方法在 actor 中实现了 stash,但现在需要查看它的大小(即 stash 中没有消息)。有什么办法吗?

以下是方法及其文档 -

/**
   *  Adds the current message (the message that the actor received last) to the
   *  actor's stash.
   *
   *  @throws StashOverflowException in case of a stash capacity violation
   *  @throws IllegalStateException  if the same message is stashed more than once
   */
  def stash(): Unit = {
    val currMsg = actorCell.currentMessage
    if (theStash.nonEmpty && (currMsg eq theStash.last))
      throw new IllegalStateException(s"Can't stash the same message $currMsg more than once")
    if (capacity <= 0 || theStash.size < capacity) theStash :+= currMsg
    else throw new StashOverflowException(
      s"Couldn't enqueue message ${currMsg.message.getClass.getName} from ${currMsg.sender} to stash of $self")
  }

【问题讨论】:

  • 我建议你阅读this
  • 你为什么不能只计算你调用stash的次数并清除unstashAll的计数?

标签: scala akka actor akka-actor


【解决方案1】:

在 akka 中,Stash 在内部实现为 StashBuffer。 StashBuffer 是一个非线程安全的可变消息缓冲区,可用于缓冲 Actor 内部的消息,然后取消存储它们。缓冲区最多可以容纳给定容量的消息数。

StashBuffer 是一个特征

trait StashBuffer[T] extends AnyRef

其中包含size方法

abstract def size: Int

表示 number 消息缓冲区中元素的数量。请参考https://doc.akka.io/api/akka/current/akka/actor/typed/scaladsl/StashBuffer.html

【讨论】:

  • 那个参考是关于 Akka Typed 的,我不认为这个问题是关于那个版本的 Akka。
猜你喜欢
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-06
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多