javaspringakkaakka-actorakka-typed

Akka typed sending messages during spawning


The question is fairly self explanatory.

Currently my webapp is creating the actor system in startup, and spawning some top level actors using SpawnProtocol. Additionally the startup could also send some messages to these actors during start up.

When creating the top level actors, akka returns a CompletionStage that waits for the actor to be created.

What is the best design pattern for this scenario of accepting messages during the spawning of the actor? Should I make the start up wait for the actor spawning to complete, or stash the messages sent and let the thread continue? If stashing then please send some example stashing techniques that are thread safe and performant

Note that I am using akka-typed, java and spring within my application


Solution

  • The basic idea would be to use CompletionStage API to chain calls to bootstrap your app.

    CompletionStage<ActorRef<Stuff>> actorRefPromise = spawnActorRef();
    
    actorRefPromise.thenApply(actorRef -> bootStrapApp(actorRef))
    

    Since you weren't too specific about your bootstrapping I am assuming that you need one ActorRef to bootstrap you app. In the above snippet spawnActorRef() is what calls the SpawnProtocol and returns CompletionStage of your ActorRef you can then use thenApply to chain an action once that ref is available and use that to pass to the rest of your bootstrapping code.