Hi, Justin
Thank you for the reply, here is a revised description of the question
Class A is a non-actor class containing some methods in which some messages
are sent to the workerActor
WorkerActor is to do some real work, e.g. forward messages to other system
components and monitor the task status...
At the beginning, we initialize workerActor directly in the constructor of
A. Then, we added a new Class B which is supposed to be the supervisor of
the WorkerActor. We want this because in our scenario, we want to stop the
system upon the failure of WorkerActor, so we use Class B to achieve this.
We create B in the constructor of A and create workerActor in B's
constructor.
However, the weird thing is we found that some methods in A (sending
messages to worker) are called before A is assigned with any non-null
value, so some NPEs are thrown
I'm not sure about the reason of this, though I highly suspect that it's
because of the non-blocking actorOf...but by intuitive, since we create
supervisorActor as
val supervisor = actorOf(Props(new B()))
new B should be executed in the main thread, i.e. all statements including
the one create A should be executed in the main thread...
Here is a bigger picture of our program
Class MainClass {
val aInstance = new A()
def submitJob {aInstance.submitJob()}
}
Class A {
val workerActor: WorkerActor = null
val supervisorActor: B = MainClass.env.system.actorOf(Props(new B))
def submitJob() {
workerActor ! JobSubmitted //NPE sometimes
}
}
Class B (aInstance: A) {
aInstance.workerActor = context.actorOf(Props(new WorkerActor))
}
user creates MainClass instance and calls submitJob to trigger everything
Thank you!
On Friday, April 25, 2014 11:05:22 AM UTC-4, Justin du coeur wrote:
>
> It's hard to give the right suggestion from what you say here, but the
> basic approach doesn't feel very "Akka-ish". It sounds like you are trying
> to hand-manage a few Actors from some linear, synchronous code, and that's
> usually a recipe for trouble. For example, the Supervisor should always be
> responsible for creating its Workers, and it's pretty common for the worker
> operations to route through the Supervisor. The "external" code (outside
> the Actors) should usually be pretty minimal, if any is needed at all.
>
> But it's very hard to know from this snippet -- there isn't enough here to
> even understand what the code is doing, much less what you're trying to
> accomplish. Can you describe your problem a bit better?
>
>
> On Fri, Apr 25, 2014 at 8:17 AM, Nan Zhu <[email protected]<javascript:>
> > wrote:
>
>> Hi, all
>>
>> I'm a newbie to akka, I have a question on the initialization process of
>> akka
>>
>> My question comes from the following scenario
>>
>> class A {
>> val workerActor: ActorRef = _
>> val supervisorActor: ActorRef = actorOf(Props[B])
>>
>> def work() {workerActor ! message} //NPE line
>> }
>>
>> class B {
>> val supervisorStrategy = ...
>> def receive = {...}
>> workerActor = context.actorOf(...) //buggy line
>> }
>>
>> it seems that the "buggy line" is executed in another thread (I think
>> it's true, as I found actorOf is non-blocking since akka 2.1), so sometimes
>> I will meet a NPE when the work() is called before B is fully started
>>
>> What I want is to block A's constructor thread until workerActor is not
>> null
>>
>> I can send a message to supervisorActor in a blocking way and returns a
>> ActorRef from the supervisor in A's constructor, but it seems that I have
>> to define a timeout Value on that blocking operation....which may involve
>> some hard-coding
>>
>> The other option includes busy loop in A's constructor...but it's
>> obviously not a first option
>>
>> I also saw this https://gist.github.com/viktorklang/856818, but I'm not
>> sure if it is what I want
>>
>> Any suggestion is highly appreciated!
>>
>> Best,
>>
>> Nan
>>
>> --
>> >>>>>>>>>> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected]<javascript:>
>> .
>> Visit this group at http://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
>>>>>>>>>> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.