如何检查Akka演员的藏身大小

时间:2018-07-25 06:57:56

标签: scala akka actor akka-actor

我已经使用Akka actor中的stash方法在actor中实现了隐藏,但是现在需要查看其大小(即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")
  }

1 个答案:

答案 0 :(得分:0)

在akka中,Stash在内部实现为StashBuffer。 StashBuffer是一种非线程安全的可变消息缓冲区,可用于在actor内部缓冲消息,然后将其取消隐藏。缓冲区最多可以容纳给定容量的消息。

StashBuffer是一个特征

trait StashBuffer[T] extends AnyRef

包含size方法

abstract def size: Int

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