When to use the Ask template in Acca

I started to learn Akka, and in many official examples I see that it is request-responseimplemented using the template tell. That is, after the employee has done his work, he sends the result as a new message back to the sender. For example, in this, the official Pi approximation guide showed how to develop an application in which a wizard sends some work to employees and then expects results as another message.

Master Code:

def receive = {
  case Calculate ⇒
    for (i ← 0 until nrOfMessages) workerRouter ! Work(i * nrOfElements, nrOfElements)
  case Result(value) ⇒
    pi += value
    nrOfResults += 1
    if (nrOfResults == nrOfMessages) {
      // Send the result to the listener
      listener ! PiApproximation(pi, duration = (System.currentTimeMillis - start).millis)
      // Stops this actor and all its supervised children
      context.stop(self)
    }
}

Work code:

 def receive = {
    case Work(start, nrOfElements) ⇒
      sender ! Result(calculatePiFor(start, nrOfElements)) // perform the work
  }

But I wonder why this example did not use a query template? What is wrong with using the query template here?

, : - ?

  • PoisonPill ?
  • - Broadcast(PoisonPill)?
  • ?
+4
1

ask API Future, . , Future , , , , .

, tell tell sender , .

, , , receive, receive, - ask.

PoisonPill, , , . " ", . , , PoisonPill , .

+6

Source: https://habr.com/ru/post/1589291/


All Articles