Я хочу контролировать дочерних акторов, используя context.watch(childActor)
API
Я вижу, что, когда у дочернего актера есть неперехваченное исключение, действительно, родительское сообщение вызывается сообщением "Termination".
Но поскольку я создаю много дочерних актеров, каждый из которых имеет разные контексты и сообщения, я не знаю, какой именно дочерний актер (и окружающий контекст) действительно потерпел неудачу.
Например:
class ParentActor extends Actor {
override def receive = {
case "delegateWorkToChildrenActors" => {
(0 to 100) foreach {
i =>
val child = context.actorOf(Props(new ChildActor(i, "child-actor")
context.watch(child)
val message = SomeSortOfComplexMessageWithManyParameters(...)
val res = child ? message
res.onComplete {
x: Try[Any] => x match {
case Failure(exception: Throwable) => // can only get timeout exception here
case Success(value) => // continue as usual
}
}
}
}
case m @ Terminated(actor) => {
// a child actor was terminated but what is its context ? i.e. which message did it try to handle ?
}
}
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 1, withinTimeRange = 1 minute) {
case m @ _ => {
Stop // Assume that the actor was stopped or crashed etc.
}
}
}
class ChildActor(number : Int) extends Actor {
override def receive: Receive = {
case SomeSortOfComplexMessageWithManyParameters => {
// might of got some sort of exception here...
// the parent which is monitoring it received the "Terminate" message
}
}
}
Как я могу получить сообщение, над которым работал дочерний актер?