Protected
Protected

ActorRegistry (Scala)


Module stability: SOLID


ActorRegistry: Finding Actors


Actors can be looked up using the 'se.scalablesolutions.akka.actor.ActorRegistry' object. Through this registry you can look up actors by:
  • uuid string – this uses the ‘uuid’ field in the Actor class, returns all actor instances with that uuid
  • id string – this uses the ‘id’ field in the Actor class, which can be set by the user (default is the class name), returns instances of a specific Actor
  • specific actor class - returns a 'List[Actor]' with all actors of this exact class
  • parameterized type - returns a 'List[Actor]' with all actors that are a subtype of this specific type

Actors are automatically registered in the ActorRegistry when they are started and removed when they are stopped. But you can explicitly register and unregister ActorRef's if you need to using the 'register' and 'unregister' methods.

Here is a summary of the API for finding actors:

def actors: List[ActorRef]
def actorFor(uuid: String): Option[ActorRef]
def actorsFor(id : String): List[ActorRef]
def actorsFor[T <: Actor](implicit manifest: Manifest[T]): List[ActorRef]
def actorsFor[T <: Actor](clazz: Class[T]): List[ActorRef]

Examples of how to use them:

val actor =  ActorRegistry.actorFor(uuid)

val actors = ActorRegistry.actorsFor(classOf[...])

val actors =  ActorRegistry.actorsFor(id)

val actors = ActorRegistry.actorsFor[MyActorType]

The ActorRegistry also has a 'shutdownAll' and 'foreach' method:

def foreach(f: (ActorRef) => Unit)
def shutdownAll

If you want to know when a new Actor is added or to or removed from the registry, you can use the subscription API. You can register an Actor that should be notified when an event happens in the ActorRegistry:

def addRegistrationListener(listener: ActorRef)
def removeRegistrationListener(listener: ActorRef)

The messages sent to this Actor are:

case class ActorRegistered(actor: ActorRef)
case class ActorUnregistered(actor: ActorRef)

So your listener Actor needs to be able to handle these two messages.
Home
close
Loading...
Home Turn Off "Getting Started"
close
Loading...