Modeling synchronous startup behavior

86 views
Skip to first unread message

Rich Henry

unread,
Oct 19, 2015, 9:20:56 AM10/19/15
to Akka User List
I had seen a previous post that didn't really seem to come to any definite conclusion about this subject.

I have some synchronous behavior I need to accomplish during my actor's construction, but the constructor is called asynchronously.

1) Is the accepted way to do this to use an initialization message of some kind with the ask pattern?

2) Is there any documentation beyond the short section in the manual that talks about actor lifecycle specifics?

Viktor Klang

unread,
Oct 19, 2015, 9:40:44 AM10/19/15
to Akka User List
Hi Rich,

and why can't it be executed in preStart()?

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,

Rich Henry

unread,
Oct 19, 2015, 10:11:48 AM10/19/15
to Akka User List
I guess that's the why of question 2. Im not clear when the different lifecycle phases, like preStart, occur. 

I would assume preStart occurs after object construction, and is asynchronous to the system call used to create the actor, is that not so?

Viktor Klang

unread,
Oct 19, 2015, 10:24:32 AM10/19/15
to Akka User List
On Mon, Oct 19, 2015 at 4:11 PM, Rich Henry <rhen...@gmail.com> wrote:
I guess that's the why of question 2. Im not clear when the different lifecycle phases, like preStart, occur. 

I would assume preStart occurs after object construction, and is asynchronous to the system call used to create the actor, is that not so?





--
Cheers,

Rich Henry

unread,
Oct 19, 2015, 10:50:02 AM10/19/15
to Akka User List
I would if it told me when that chain of events starts and stops relative to the actorOf call itself.

My actor needs to register with another non-actor subsystem before i interact with it, so If i write code like...

val subsys = new SubSys()
val a
= system.actorOf(Props(classOf[MyActor], subsys)) // constructor registers with subsys
subsys
.go()

There is a race condition as the constructor for my actor doesn't get called before .go(). 

Viktor Klang

unread,
Oct 19, 2015, 11:00:58 AM10/19/15
to Akka User List
Hi Rich,

On Mon, Oct 19, 2015 at 4:50 PM, Rich Henry <rhen...@gmail.com> wrote:
I would if it told me when that chain of events starts and stops relative to the actorOf call itself.

actorOf is asynchronous so that's completely separate from the lifecycle.
 

My actor needs to register with another non-actor subsystem before i interact with it, so If i write code like...

val subsys = new SubSys()
val a
= system.actorOf(Props(classOf[MyActor], subsys)) // constructor registers with subsys
subsys
.go()

There is a race condition as the constructor for my actor doesn't get called before .go(). 

Is `subsys` threadsafe? Can your MyActor send a message somewhere to indicate that it has registered with subsys?



--
Cheers,

Rich Henry

unread,
Oct 19, 2015, 11:16:51 AM10/19/15
to Akka User List
All the other components are thread-safe.

I just need an order of operations including the actorOf call, so coming full circle, my original plan was to use some kind of initialization message with the ask pattern to sync it up.

I was trying to figure out if this is the best i can do.

Adam

unread,
Oct 20, 2015, 3:23:07 AM10/20/15
to Akka User List
Hi,

In my system I have an initialization sequence that must take place at a certain order.
Basically I don't want to open my system for incoming requests, before all services are properly started.
So what I needed is sort of similar to what you describe here.

I've implemented it without ask.
Whenever a "service" actor is started it starts initialization in preStart and ultimately sends a ServiceStarted back to it's parent.
As I sometimes have a hierarchy of such services, some of them do not immediately reply from preStart as they themselves wait for of their dependencies to reply with ServiceStarted.
During this sequence I have a receive timeout set in the waiting Actors and I remove it once they are fully initialized.

In my case this all takes place within the same JVM, so I didn't bother with Acks, so overall it's not too complex.

I hope this helps.

Rich Henry

unread,
Oct 20, 2015, 3:39:59 PM10/20/15
to Akka User List
This is definitely another option, but the object starting my actor is not an actor.

I could possibly use inbox() to achieve a similar effect. I will think about it. Thank you.

אדם חונן

unread,
Oct 21, 2015, 12:16:33 AM10/21/15
to akka...@googlegroups.com

Well, for the integration points, assuming you need a response, uaing ask is a very good approach.
I think the Play framework takes tgis approach.

You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/69j5j77t87E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.

Rich Henry

unread,
Oct 21, 2015, 1:25:34 PM10/21/15
to Akka User List
For posterity, I ended up doing this, and now i'm assured that the constructor ran and I didn't have to add any ad hoc messages...

  class MyActor(p: Promise[ActorRef]) extends Actor {
   
override def preStart() {
      p
.complete(Success(self))
   
}    
   
   
override def receive = {
     
case x => println(x)
   
}
 
}
 
  val system
= ActorSystem("system1")
  val promise
= Promise[ActorRef]
  system
.actorOf(Props(classOf[MyActor], promise))
  val actor
= Await.result(promise.future, 10 seconds)

If there are any negatives to this approach i'd like to hear about it.

Thanks for the comments. - Rich

Richard Bradley

unread,
Oct 25, 2015, 7:39:03 AM10/25/15
to Akka User List
> If there are any negatives to this approach i'd like to hear about it.

If the actor is restarted on error, then the same promise will be passed as a constructor parameter to the new instance.
I think the Ask pattern here is more conventional Akka practice, but both should work . 
Reply all
Reply to author
Forward
0 new messages