I'm trying to catch 'Terminate Signal' from child to parent actor, however among pool of deadletter messages, the signal fails to arrive on parent actor's queue. What's the best way to resolve this?
Here's the snippet code I'm working on:
class MinerActor extends Actor {
var count:Int = 0
def receive = {
case Mine =>
//some task here
//if success
count = count + 1
//
if (count >= 100)
{
context.stop(self)
}
}
class MasterActor extends Actor {
val miner = context.actorOf(Props(new MinerActor,name = "miner")
context.watch(miner)
def receive = {
case Foo =>
while (true) {
miner ! Mine
}
case Terminated(miner) =>
println("Miner Terminated!!")
context.stop(self)
context.system.shutdown
}
}
Here the 'Terminated(miner)' case never gets called. Instead on stdout I see lots of dead-letter messages sent from Master to Miner (which is kind of expected as miner actor gets stopped). However how to prioritize the Terminate signal on the Event bus so as to reach Master Actor?
If I limit while loop to some 200 instead of infinity, after 100 deadletter messages, I receive Terminate Signal which prints "Miner Terminated!!". But how to achieve this when while loop being in infinity?
I'm new to Scala/Akka programming, my main aim here is to run '//some task' for 100 successful times and then exit the whole program. Is this a good way to achieve that task?