lovesh
lovesh

Reputation: 5401

Why dont akka actors have a postStart method?

Here is a situation i have to deal with - I am using websockets with play framework and each websocket connection has its own Actor as described here. Now as soon as the websocket connection is made i need to start another Actor that subscribes to a Redis channel and on getting any published message to the channel passes that message to its parent i.e the Websocket Actor. So i need to start the Redis Subscriber Actor after the Websocket Actor has started. But actors dont have a postStart method. I tried creating the Redis Subscriber Actor in the preStart method of the Websocket Actor and it works fine but i dont understand the reason for Actors not having a postStart method. Isn't this a common scenario where actors create children actors. Or is this approach of doing things incorrect?

Upvotes: 3

Views: 1421

Answers (1)

cmbaxter
cmbaxter

Reputation: 35453

As indicated in my comment, I'm not sure why preStart is not sufficient for your needs, it's one good place to put child actor creation (the other being the constructor body). The thing about preStart is that it really means pre message handling. The actor itself has been started up but is not yet receiving messages from the mailbox. This is a good place to make sure any other dependencies are created before you start handling messages. If you did this after the actor started handling messages you could hit a race condition where the dependency (child) is not created yet

You should review the actor lifecycle diagram here for more clarity

Upvotes: 9

Related Questions