preStart() question


#1

When an actor starts up and preStart() is run, is it guaranteed that the actor will not receive any messages via onReceive() until the entire code block in preStart() is run?

The reason I ask is I have some business logic that has to run during initialization and just want to make sure that nothing else in the actor is happening.


(Konrad `ktoso` Malawski) #2

Yes, that’s guaranteed. As the name implies “pre start” => “before starting doing other stuff” :-)
If you do any async (futures in preStart though) it will not wait for them to complete ofc.


(Brett Tofel) #3

From here: https://doc.akka.io/docs/akka/2.5/guide/tutorial_1.html
seems pretty definitive:

The Akka actor API exposes many lifecycle hooks that you can override in an actor implementation. The most commonly used are preStart() and postStop().

preStart() is invoked after the actor has started but before it processes its first message.
postStop() is invoked just before the actor stops. No messages are processed after this point.