作为静态用法

Akka static usage

我有一个带有默认配置的简单演员系统。

我有一个 class 扩展 Actor

class Test extend Actor {
  def receive: Receive = {
      case Foo(collection) => sender ! extract(collection)
  }

  private def extract(c: List[FooItem]): List[BarItem] = ???
}

这个演员有伴生对象

object Test {
  def props: Props = ???
}

像这样提取函数是否安全:

object Test {
  def props: Props = ???
  def extract(c: List[FooItem]): List[BarItem] = ???
}

并从另一个 Actor 使用?

是的,可以在 companion 上定义一个方法,然后在 actor 中导入并使用该方法 class。像这样的东西会很好用:

object Test {
  def props: Props = Props[Test]
  def extract(c: List[FooItem]): List[BarItem] = {
    . . .
  }
}

class Test extend Actor {
  import Test._
  def receive: Receive = {
    case Foo(collection) => sender ! extract(collection)
  }
}