Design pattern: how to implement a chain of participants processing a single logical message

I'm starting to look at Akka, and I'm wondering how to properly implement the participants pipeline, working together to process the message. Let me give an example to illustrate my question:

  • I have a message that is a “decision” to be made by collaborating participants.

  • I want to create some "voters"; each voter expresses his opinion regarding the decision to be made (based on his own strategy and rules).

  • I could implement different strategies to summarize the opinions of different agents, but imagine that I want to make a POSITIVE decision if and only if ALL of the agents in the chain make a positive decision.

How do I implement this in akka? Can I implement the first actor who represents the whole chain and receives the message "SOLUTION"? Will this actor have many children, because there are voters in the chain? How will the father interact with the children and who will control the flow of events? Will there be the same DECISION message coming from one voter to another? Or maybe a series of interactions between a parent and one child?

What are the recommended patterns for such cases?

Thanks so much for your feedback!

Olivie

+4
1

, , , :

trait DecisionActor extends Actor

class AbsoluteAgreementActor extends DecisionActor {
  val numberOfVoters = 100
  def receive = {
    case d:Decision = {
      computeDecision(d) pipeTo sender
    }
  }

  def computeDecision(d) = {
    val votes = for {
      i <- 0 until numberOfVoters
      //Naming it for re-usual in case you don't delete it
      voter = context.actorOf(Props[VoterActor], s"Voter $i")           
    } yield {
      voter ? d
    }
    Future.sequence(votes).foldLeft(True)(&&)
  }
}

. /, , . , . . , DecisionActor.

+2

All Articles