问题
According to the Akka docs for PoisonPill
:
You can also send an actor the akka.actor.PoisonPill message, which will stop the actor when the message is processed. PoisonPill is enqueued as ordinary messages and will be handled after messages that were already queued in the mailbox.
Although the usefulness/utility of such a feature may be obvious to an Akka Guru, to a newcomer, this sounds completely useless/reckless/dangerous.
So I ask: What's the point of this message and when would one ever use it, for any reason?!?
回答1:
We use a pattern called disposable actors
:
- A new temporary actor is created for each application request.
- This actor may create some other actors to do some work related to the request.
- Processed result is sent back to client.
- All temporary actors related to this request are killed. That's the place where
PoisonPill
is used.
Creating an actor implies a very low overhead (about 300 bytes of RAM), so it's quite a good practise.
来源:https://stackoverflow.com/questions/30633682/use-case-for-akka-poisonpill