Scala 工厂参与者模式
前面我读到创建root(在/user/之后)actor(system.actorOf)很昂贵Scala 工厂参与者模式,scala,akka,Scala,Akka,前面我读到创建root(在/user/之后)actor(system.actorOf)很昂贵 是否有一种创建ClientFactoryActor的通用模式,其主要职责是在请求时返回新的actor(例如,我们需要每个客户端都有新的websocket actor等) 事实上,为了错误处理的目的,您应该尝试维护参与者的层次结构(不同的监督策略) 创建actor的一种方便方法是使用一个伴随对象,该对象返回使用给定参数实例化的对所需actor的引用(singleton工厂) 一个很好的起点是页面。不要忘记
是否有一种创建ClientFactoryActor的通用模式,其主要职责是在请求时返回新的actor(例如,我们需要每个客户端都有新的websocket actor等) 事实上,为了错误处理的目的,您应该尝试维护参与者的层次结构(不同的监督策略) 创建actor的一种方便方法是使用一个伴随对象,该对象返回使用给定参数实例化的对所需actor的引用(singleton工厂)
一个很好的起点是页面。不要忘记杀死参与者以避免内存泄漏。
object DemoActor {
/**
* Create Props for an actor of this type.
*
* @param magicNumber The magic number to be passed to this actor’s constructor.
* @return a Props for creating this actor, which can then be further configured
* (e.g. calling `.withDispatcher()` on it)
*/
def props(magicNumber: Int): Props = Props(new DemoActor(magicNumber))
}
class DemoActor(magicNumber: Int) extends Actor {
def receive = {
case x: Int => sender() ! (x + magicNumber)
}
}
class SomeOtherActor extends Actor {
// Props(new DemoActor(42)) would not be safe
context.actorOf(DemoActor.props(42), "demo")
// ...
}