1
votes

I am new to Akka and I am implementing something for fine. What I have now is a supervisor that have a child actor that does the actual work. If the work is done, the child will send the supervisor a case object Finished which changes the state of the supervisor.

However, in this sense, I can also send the supervisor in the master thread Finished, and then the action will be completely out of order.

Is there a way for the supervisor to react differently depending on who sends he message? Or better, it is possible to enforce the supervisor to only receive Finished if it is sent by its child?

1
You can find some solutions but, you probably shouldn't. Actors should not know anything outside their scope. You should encapsulate all behaviour and only react to messages, what about using different kinds of Finished messages? Just saying for example Finished for worker actors, Finish for supervisor actorHüseyin Zengin

1 Answers

2
votes

Yes, quite easily.

Maintain set of children on supervisor actor:

 var children = Set[ActorRef]()

Wherever supervisor receives message, check if sender belongs to your set of children:

 def receive = {
    case Finished(_) if children.contains(sender) => ???
 }

That's it!

There is one thing to consider though, if you are using Finished for logically independent cases, think of creating different message types for each particular case.