On Friday, 11 April 2014 10:35:46 UTC+1, Iain Hull wrote:
>
> However I will look for another approach.
>
I think I have found a clean way to inject the DeathReportActor code into a
parent actor. I have made this actor a trait and renamed it to
ChildMonitor. I would be very grateful if someone could advise if this is
a valid use of inheritance with actors? I put the traits logic into the
unhandled method as this allows subclasses to use the normal receive method
as well as context.become and context.unbecome. However I am not sure if
this is a valid approach to stacking actor behavior or does it break the
Actor's contract. Can I safely modify state and access the other Actor
methods in unhandled?
trait ChildMonitor extends Actor {
private var watches = Map[ActorRef, Any]()
def monitorChild(child: ActorRef, message: Any): Unit = {
watches += child -> message
context.watch(child)
}
override def unhandled(message: Any): Unit = message match {
case Terminated(child) =>
watches.get(child) foreach (m => self ! m)
watches -= child
case _ =>
super.unhandled(message)
}
}
class GuardianActor extends Actor with ChildMonitor {
var fatedOpt: Option[ActorRef] = None
def receive = {
case Create(value) =>
fatedOpt = Some(context.actorOf(pheonixProps(value), "pheonix"))
case Recreate(value) =>
fatedOpt match {
case Some(fated) =>
monitorChild(fated, Create(value))
context.system.stop(fated)
// fated ! PoisonPill
case None =>
fatedOpt = Some(context.actorOf(pheonixProps(value), "pheonix"))
}
}
}
Thanks for your help,
Iain.
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.