I'm completely new to this, but here's a Scala actor that's created with my class and only lives to generate messages for other actors. I would like it to wake up every second, gather some metrics, ship them off to other actors, and go back to sleep. When terminating the application, I need to send this actor a message to quit
class Node() {
println("A new Node is being constructed")
val load_monitor: Actor = actor {
val s: Sigar = new Sigar
while (true) {
Thread.sleep(1000);
// Will be replaced with something like
// load_manager | s.getCpuSystem
println(s.getCpuPerc)
self.receiveWithin(100) {
case "EXIT" => exit()
case TIMEOUT => {}
}
}
}
// Other Node code below...
}
This seems to work, but I don't have confidence it's correct. Is calling Thread.sleep smart? If this is a full thread, I now have a sleeping thread. If this is an event, have I now blocked some event processing queue? Can I use the receive/react stuff to do this more properly? I'm having trouble understanding if receive/react effectively stops the execution of an actor, which means I could not wake up every second to check metrics. I've also considered the following code, but I don't know if it is any better than the above code!
class Node() {
println("A new Node is being constructed")
val load_monitor: Actor = actor {
val s: Sigar = new Sigar
while (true) {
self.receiveWithin(1000) {
case "EXIT" => exit()
// Will be replaced with something like
// load_manager | s.getCpuSystem
case TIMEOUT => println(s.getCpuPerc)
}
}
}
// Other Node code below...
}