I am trying to get an Actor to work but it only works partially.
MyActor goes to work every second and runs a Job by calling itself in a loop.
This part works fine.
I’d also like to call MyActor randomly from other parts of my application.
This does NOT work.
MyActor increments its mailboxSize but then continues with the loop and never fetches any of the messages sent from outside.
case class DoJob(fast:Boolean)
object MyActor extends Actor {
def act = {
loop {
MyActor ! DoJob(false)
receive {
case DoJob(fast) => {
Job.perform(fast)
}
}
Thread.sleep(1000)
}
}
}
The following does not work in terms of calling DoJob(true) but merely increments MyActor’s mailbox size:
MyActor ! DoJob(true)
What could be wrong here?
Each call to
receiveonly gets one message. Since you send yourself one message right before calling receive you will never reduce the mailbox size in that loop.You should make a separate thread to send the heartbeat, or use
receiveWithinand aTIMEOUTcase.