No problem. I've updated the pastie - http://pastie.org/9573588

Only thing I can spot is that build() is being called before the 
declaration+initialization of name, so perhaps the line `var name:String = 
""` is being executed twice. Once 
before build() to declare the variable, and then once after build() to 
initialize it (e.g. just as a normal line being called during the 
constructor). 
Perhaps this second call is actually overwriting what build() has set, and 
making name again equal "". 

Thanks, 
Hamilton

On Thursday, September 18, 2014 5:06:30 PM UTC-4, √ wrote:
>
> Could you show me that piece of code?
>
> On Thu, Sep 18, 2014 at 4:49 PM, Hamilton Turner <hami...@gmail.com 
> <javascript:>> wrote:
>
>> Ok, heartbeatSchedule makes sense now - there are no threading guarantees 
>> with the future's onSuccess, so I just need to send myself a message 
>> instead. 
>>
>> I'm still confused about what's happening with variable name. I forgot to 
>> include the call to build() (sorry!), it's called in the constructor for 
>> ExampleProblemActor, 
>> immediately after I declare var heartbeatSchedule, so build() should be 
>> executed on the thread that the actor is being initialized with. 
>>
>> Best, 
>> Hamilton
>>
>> On Thursday, September 18, 2014 3:32:46 PM UTC-4, √ wrote:
>>>
>>> Hi Hamilton,
>>>
>>> On Thu, Sep 18, 2014 at 1:32 PM, Hamilton Turner <hami...@gmail.com> 
>>> wrote:
>>>
>>>> The below *minimum example* shows some very simple actor code that's 
>>>> not working as expected. 
>>>>
>>>> I'm setting the value for "name" and for "heartbeatSchedule", but in 
>>>> postStop they are both set to their initial values
>>>> and not to the values I set. This feels like a threading problem, but 
>>>> it's that exactly what the Actor should be helping me avoid?
>>>>
>>>> Pastie is http://pastie.org/9572947# if that's easier to read. 
>>>>
>>>> Best,
>>>> Hamilton
>>>>
>>>> class ExampleProblemActor() extends Actor {
>>>>  
>>>>  var name:String = ""
>>>>  var heartbeatSchedule: Cancellable = null
>>>>
>>>>  override def postStop = {
>>>>    // At this point, name == "" and heartbeatSchedule==null. Why?
>>>>    info(s"Name is '$name'")
>>>>    if (heartbeatSchedule != null)
>>>>      heartbeatSchedule.cancel
>>>>    else
>>>>      debug("Heartbeat schedule was null") 
>>>>  }
>>>>
>>>>  override def preStart() {
>>>>    implicit val system = context.system
>>>>    val boot = future {
>>>>      info(s"Waiting for my process to come online")
>>>>      if (false == ps.wait(ID, 200 second))
>>>>        throw new IllegalStateException("Not online")
>>>>    } 
>>>>  
>>>>    boot onSuccess { case _ =>
>>>>      heartbeatSchedule = system.scheduler.schedule(0 seconds, 1 seconds
>>>> , self, DoHeartbeat)
>>>>      // heartbeatSchedule != null here - I checked
>>>>    } 
>>>>
>>>
>>> This above is not legal, you are closing over the actor and are writing 
>>> to the actor from another thread. onSuccess is executed on another thread 
>>> when boot is completed.
>>>  
>>>
>>>>  }
>>>>
>>>>  def receive = {
>>>>    case DoHeartbeat => {
>>>>      <snip> 
>>>>      }
>>>>    case _ => {
>>>>      info(s"Received unknown message")
>>>>    }
>>>>  }
>>>>
>>>>  def build(): (Process, String) = {
>>>>    info(s"Building and starting")
>>>>
>>>>    var hostname = "hostname".!!.stripLineEnd;
>>>>    name = s"$hostname-2345" 
>>>>    // name != "" here
>>>>    info(s"Building $name")
>>>>    <snip>
>>>>  }
>>>>
>>>
>>> build() is never called in your code.
>>>  
>>>
>>>> }
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>  -- 
>>>> >>>>>>>>>> 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,
>>> √
>>>  
>>  -- 
>> >>>>>>>>>> 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 <javascript:>.
>> To post to this group, send email to akka...@googlegroups.com 
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Cheers,
> √
>  

-- 
>>>>>>>>>>      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+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to