AKKA Actor Scheduler

AKKA Actor Scheduler

If I want the actor work like cron repeat the messages. I will do like this.
    val schedulerSyncActor = Akka.system.actorOf(SchedulerSyncActor.props, name = "scheduler-sync-actor")
    logger.info("init the schedule jobs-----------")
    Akka.system.scheduler.schedule(
      0 seconds,
      60 seconds,
      schedulerSyncActor,
      "fire-in-the-hole"
    )
    logger.info("schedulerSyncActor executing every " + contextio_sync_interval + " seconds")
If I just want to delay the msg for some time
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import play.api.Play.current
import scala.concurrent.duration._

case msg: DelayResumeParseCOMPersistMessage =>{
  logger.debug("System will send the message delay " + msg.delay + " seconds")
  context.system.scheduler.scheduleOnce(msg.delay second, self, msg.message)
}

The idea is that the DelayResumeParseCOMPersistMessage will be received, and we will delay some seconds, and then, the the message content back to itself again. That is how the self work.

References:
http://doc.akka.io/docs/akka/current/scala/scheduler.html

猜你喜欢

转载自sillycat.iteye.com/blog/2276145