Search code examples
scalaakkaactor

Running functions simultaneously from an akka actor


From an actor (akka.actor.Actor) how can I execute a method twice simultaneously? Try to do it with Futures, but if the future does not respond, the actor does not take more requests ...


Solution

  • Generally, actors should not block. Dealing with actors, you should think in dataflow manner. Draw a dataflow diagram like this:

              --> methodCall1 --> |----------|
     actor1 /                     |  actor2  |  
            \ --> methodCall2 --> |__________|
    

    Actor2 can be inplemented as an Akka actor, which counts incoming messages and reacts when both messages has arrived. In this case, methodCall1 and methodCall must know actor2 and send messages to it.

    Or actor2 can be a CompletableFuture which is derived from the future of one of method calls using method thenCombine.