Reputation: 358
I am using the ask pattern with a timeout to send a message to my actor, which might take a long time to process it. I would like to stop processing the message if the timeout expires. But, it seems the actor keeps processing the message, and I receive a dead letter.
How can I stop the actor from processing the message when the timeout expires?
Upvotes: 0
Views: 811
Reputation: 35443
The short answer is that you can't. Once it starts processing it, sending Kill or Poison Pill or stopping it will only prevent it from processing future messages. The long answer is that maybe you can hack around this if the actor happened to be in a loop of some sort and was checking some mutable (and potentially globally available) signal for stopping looping, but there are so many red flags with that approach that you should forget I even mentioned it. And if the long call happened to be a blocking io call to something like a DB, forget about it; you're stuck until that call completes (or times out if you are lucky).
The actor itself is not aware that someone called it via ask
and there is a Future
upstream that might time out. It has no concept of this and thus can not react accordingly when that Future
times out.
Upvotes: 3