首页 > 解决方案 > 如何检查akka actor中的存储大小

问题描述

我已经使用 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")
  }

标签: scalaakkaactorakka-actor

解决方案


In akka, Stash is internally implemented as StashBuffer. A StashBuffer is a non thread safe mutable message buffer that can be used to buffer messages inside actors and then unstash them. The buffer can hold at most the given capacity number of messages.

The StashBuffer is a trait

trait StashBuffer[T] extends AnyRef

which contains the size method

abstract def size: Int

which denotes the number the number of elements in the message buffer. Please refer https://doc.akka.io/api/akka/current/akka/actor/typed/scaladsl/StashBuffer.html


推荐阅读