Skip to content
laforge49 edited this page Jul 23, 2011 · 18 revisions

Instantiating an Actor from a FactoryId is an important service, and can be quite helpful when deserializing a persistent actor or when processing a remote request. The instantiate service is invoked by passing an Instanciate message to systemServices.

case class Instantiate(factoryId: FactoryId, mailbox: Mailbox)

systemServices(Instantiate(factoryId, mailbox) {rsp => ...}

Actor instantiation is always done synchronously by the SafeInstantiate class.

class SafeInstantiate(factoryRegistryComponentFactory: FactoryRegistryComponentFactory)
  extends Safe {
  def func(msg: AnyRef, rf: Any => Unit)(implicit sender: ActiveActor) {
    val factoryId = msg.asInstanceOf[Instantiate].factoryId
    val mailbox = msg.asInstanceOf[Instantiate].mailbox
    val factory = factoryRegistryComponentFactory.getFactory(factoryId)
    val actor = factory.newActor(mailbox)
    rf(actor)
  }
}

SafeInstantiate is bound to the FactoryRegistryComponent.

class FactoryRegistryComponent(actor: Actor, componentFactory: FactoryRegistryComponentFactory)
  extends Component(actor, componentFactory) {
  bindSafe(classOf[Instantiate], new SafeInstantiate(componentFactory))
}

The FactoryRegistryComponent is, in turn, created by the FactoryRegistryComponentFactory, which handles the registration of factories.

class FactoryRegistryComponentFactory extends ComponentFactory {
  private val factories = new java.util.TreeMap[String, Factory]

  def registerFactory(factory: Factory) {factories.put(factory.id.value, factory)}

  def getFactory(id: FactoryId) = factories.get(id.value)

  override def instantiate(actor: Actor) = new FactoryRegistryComponent(actor, this)
}

Clone this wiki locally