Hi Iain,

11 apr 2014 kl. 12:37 skrev Iain Hull <[email protected]>:

> 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?

Yes, that is safe.

> 
> 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) =>

One problem is that this grabs the handling of all Terminated messages which 
get this far; you could add a guard `if watches contains child`, but if 
different layers of your composed actor all watch a target then only one of 
them gets the Terminated message.

>       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"))

This means that this actor can be remote-crashed by sending Create twice (using 
InvalidActorNameException), but that might be your intention.

>     case Recreate(value) =>
>       fatedOpt match { 
>         case Some(fated) =>
>           monitorChild(fated, Create(value))
>           context.system.stop(fated)

I would just use `context.stop(fated)`.

>           // fated ! PoisonPill
>         case None =>
>           fatedOpt = Some(context.actorOf(pheonixProps(value), "pheonix"))
>       }
>   }
> }
> 
> Thanks for your help,

You’re welcome!

Roland

> 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.



Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


-- 
>>>>>>>>>>      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.

Reply via email to