If you really need to wait for the future to finish before processing the next message, you can try something like this:
object SimpleMessageHandler{ case class SimpleMessage() case class FinishSimpleMessage(i:Int) } class SimpleMessageHandler extends Actor with Stash{ import SimpleMessageHandler._ import context._ import akka.pattern.pipe def receive = waitingForMessage def waitingForMessage: Receive = { case SimpleMessage() => val futData:Future[Int] = ... futData.map(FinishSimpleMessage(_)) pipeTo self context.become(waitingToFinish(sender)) } def waitingToFinish(originalSender:ActorRef):Receive = { case SimpleMessage() => stash() case FinishSimpleMessage(i) =>
In this approach, we process SimpleMessage and then switch the processing logic so that all subsequent SimpleMessage are retrieved until we get the result from the future. When we get a result, failure or not, we swing all the rest of SimpleMessage , which we received in anticipation of the future, and continue our fun journey.
This actor simply switches between the two states and allows you to fully process only one SimpleMessage at a time without having to block the future.
cmbaxter
source share