I have an actor who oversees another actor (who is removed, so you cannot refer to him directly). He gets a link to the actor using actorSelectionand Identify, and then looks at the resulting one ActorRef. It works.
But now I want to automatically connect the same way as when another actor completes, so I reuse the same one actorSelection(the Actor instance gets the instance in the same place), but this time the search through is Identifycompleted forever. I have no idea why this works when the actor is already created at the beginning, but no, otherwise.
Edit: What is also strange is that association errors occur before the first connection, while there is no association error when trying to reconnect, even if the remote jvm is completely completed. I just noticed that if you wait minutes or more after the failure, the association error returns and the connection ends again. Is there a way to configure this mechanism (is it a Cache mechanism?).
Is this standard behavior or is something wrong with me?
In case I messed up something with my code:
object ServerMonitor {
case object Request
case class Reply(ref: ActorRef)
}
class ServerMonitor(path: String) extends Actor with ActorLogging {
import ServerMonitor._
var server: Option[ActorRef] = None
var listeners: Set[ActorRef] = Set.empty
def receive = {
case ActorIdentity("server", Some(ref)) =>
server = Some(ref)
context.watch(ref)
listeners.foreach(_ ! Reply(ref))
listeners = Set.empty
log.info(s"connected to the server at $path")
case ActorIdentity("server", None) =>
server = None
log.warning(s"couldnt reach the server at $path")
import context.dispatcher
context.system.scheduler.scheduleOnce(1 second) {
context.actorSelection(path) ! Identify("server")
}
case Terminated(ref) =>
log.warning("server terminated")
server = None
context.actorSelection(path) ! Identify("server")
case Request =>
server.fold {
listeners += sender
} { ref =>
sender ! Reply(ref)
}
}
override def preStart() {
context.actorSelection(path) ! Identify("server")
}
}
source
share