Lets say I have some commonly used by other actors service-layer actor. For example, an registry service that stores and retrieves domain objects:
case class DomainObject(id: UUID)
class Registry extends akka.actor.Actor {
def receive: Receive = {
case o: DomainObject => store(o) // save or update object
case id: UUID => sender ! retrieve(id) // retrieve object and send it back
}
}
I do not want to explicitly pass instance of such registry into all actors who may use it. Instead of it, I want them to be able to somehow 'locate' it.
For this I can think of two solutions:
Identify
message: each registry user actor knows registry actor name from some configuration and able to sent identification message to it. AfterAgentIdentity
message is received back we are good to go:val registryName = ... // some name val registryId = ... // some id var registry = _ def preStart() { context.actorSelection(registryName) ! Identify(registryId) } def receive: Receive = { case ActorIdentity(`registryId`, ref) => registry = ref }
I do not like this way because right after user actor initialisation there is a phase when we do not know if there is a registry in system et all and thus do not know will we ever be able to operate or not.
Akka Extensions: I can create an extension which would:
a. create instance of Registry actor in given Actor System on initialization;
b. return this actor to user who needs it via some method in Extension.
object RegistryKey extends ExtensionKey[RegistryExtension] class RegistryExtesion(system: ExtendedActorSystem) extends RegistryKey { val registry = system.actorOf(Props[Registry], "registry") }
The question is: which method is better and are Akka Extesions can be used for this at all?