I have 2 actors :
class ActorA extends Actor {
def receive = ???
}
object ActorA {
val actorA = system.actorOf(Props[ActorA])
}
class ActorB extends Actor {
def receive = {
case aMessage => ActorA.actorA ! aMessage
}
}
I want the actorA to have at any time only one instance (to apply a kind of back pressure)
But with the code above, if there's an error in the ActorA actor, the actor is restarted and the ActorRef actorA is no longer correct.
What's the correct pattern to use in this case ? Must I use ActorSelection ?
Yes. .actorOf()
creates an actor
Assuming your two actors are created once in an init Class/Object/Main
val actorA = system.actorOf(Props[ActorA])
val actorB = system.actorOf(Props[ActorB])
Recover the reference to the existing ActorA
from the context
class ActorB extends Actor {
val aA = context.actorSelection( "/user/ActorA" )
def receive = {
case aMessage => aA ! aMessage
}
}
Alternatives are
ActorPath
or its reference to the constructor of ActorB