An Akka-2 instance should remain in an infinite loop and check every 10 minutes for data to process.
How do I design the loop so that the instance calls upon itself, checks for work to do and then sleeps for an interval?
Also I see that one cannot query the mailbox-size anymore. How do you make sure that messages are ignored as long as the working-task ( in this case the dispatch function) is active?
case class Dispatch()
// Automatical start? The start function has been removed since Akka 2 ?
val dispatcher = system.actorOf(Props[MailDispatcher])
class MailDispatcher extends Actor {
private val interval = Config.getLong("mail.check.interval")
context.setReceiveTimeout(Duration(interval, TimeUnit.SECONDS))
def receive = {
case ReceiveTimeout => {
self ! Dispatch
}
case Dispatch => dispatch()
case e: Exception => Logger.error("unexpected Error: " + e)
}
def dispatch() {
// trigger mail-dispatch
}
}
I’d suggest the following:
use: http://akka.io/docs/akka/2.0-RC2/scala/actors.html#initial-receive-timeout
Then when you get a ReceiveTimeout message, you poll for work, and send the work to your own mailbox.