Reputation: 171
I would like to ask for pattern when it is required to init actor state from database. I have a DAO which return Future[...] and normaly to be non blocking message should be send to actor on future complete. But this case is different. I cannot receive any message from actor mailbox before initialization complete. Is the only way to block actor thread while waiting for database future complete?
Upvotes: 2
Views: 624
Reputation: 6982
The most trivial approach would be to define two receive methods, e.g. initializing
and initialized
, starting with def receive = initializing
. Inside your inizializing
context, you could simply send back a message like InitializationNotReady
to tell the other actor that he should try it again later. After your actor is initialized, you switch your context with context become initialized
to the new state, where you can operate normally.
After all, another good aproach could be to have a look at Akka Persistence. It enables stateful actors to persist their internal state so that it can be recovered when an actor is started, restarted after a JVM crash or by a supervisor, or migrated in a cluster.
In you case, you can restore your state from a database, since their are multiple storage options for Akka persistence. you can find them here. After that recovery, you can receive messages like you're used to it.
Upvotes: 4