Reputation: 1061
Reading the Akka 2.6.10 API Docs, the difference between akka.actor.typed.scaladsl.Behaviors.setup
and akka.actor.typed.scaladsl.Behaviors.receive
ought to have been immediately clear to me. But it wasn't.
The documentation site provides some excellent examples, but it still took me a lot of pondering to catch on to the intended purpose for each function, which was never really stated explicitly.
In the hope of saving future Akka (Typed) newbies some time, I will try to clarify the differences between these behavior-defining functions. This is basic stuff, but it's important for understanding the rest.
Upvotes: 3
Views: 2409
Reputation: 1061
Behaviors.setup
defines behavior that does not wait to receive a message before executing. It simply executes its body immediately after an actor is spawned from it.
You must return a Behavior[T]
from it, which might be a message-handling behavior. To do this, you would likely use Behaviors.receive
or Behaviors.receiveMessage
. (Or else Behaviors.stopped
, if the actor is only required to do its work once once, and then disappear.)
Because it executes without waiting, Behaviors.setup
is often used to define the behavior for the first actor in your system. Its initial behavior will likely be responsible for spawning the next brood of actors that your program will need, before adopting a new, message-handling behavior.
Behaviors.receive
defines message-handling behavior. You pass it a function that has parameters for both the actor context, and an argument containing the message. Actors created from this behavior will do nothing until they receive a message of a type that this behavior can handle.
Upvotes: 9