Best way to handle asynchronous calls inside receive method of Akka actor Best way to handle asynchronous calls inside receive method of Akka actor multithreading multithreading

Best way to handle asynchronous calls inside receive method of Akka actor


This is a great example of when you should have an actor create another actor to handle an interaction. Basically the flow is like this and you can use FSM (finite state machine) too if it makes it easier for you.

  1. Parent actor receives message
  2. Parent actor creates child transaction actor
  3. Parent sends the child the original message
  4. Child calls async method and stores the Future
  5. Child uses become() to change its behavior to wait for completion of the future. There are a number of ways to do this, including FSM.
  6. Child schedules periodic messages to itself to check the Future
  7. The Child's new behavior checks the Future if it is complete on each receive
  8. When the Future is complete, child can reply with the result to the parent or to the original sender (if the parent fwded the message with the original sender).