Question

we have a lot of actors that get created as

class BankActor extends Actor{
   def receive ={
      case CreateCustomer(message) => context.actorOf(Props[CustomerActor]) ! message
  }
}

And CustomerActor creates other actors in a similar manner. Reason for creating actors in such a way is potentially there will be hundreds(or even thousands) of CreateCustomer messages that BankActor will be receiving in them. I thought creating them on the fly is a better way (given Actor is a low memory footprint). I didn't think having a "pool" of CustomerActor was a right thing to do because biz req is clear there will be lots and lots of "CreateCustomer" messages. can you share your thoughts on this? Now going back to question about stopping "CustomerActor" : where should I be doing context.stop(self) inside "CustomerActor"'s "receive" method, should it be the last thing in every "case" block in there? What's the best practice around this?

Was it helpful?

Solution

Avoid creating top-level actors unless you have a good reason to. (Use context.actorOf)

Send the newly created actor a PoisonPill after the "message" if you don't want to encode the shutdown within the created actor.

class BankActor extends Actor{
   def receive = {
      case CreateCustomer(message) =>
        val customer = context.actorOf(Props[CustomerActor])
        customer ! message
        customer ! PoisonPill // message ordering is preserved on a per-sender basis (for all normal mailboxes)
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top