How to use scheduler in scala? :)
val actorSystem = ActorSystem()
val scheduler = actorSystem.scheduler
val task = new Runnable { def run() { println("Hello") } }
implicit val executor = actorSystem.dispatcher
scheduler.schedule(
initialDelay = Duration(5, TimeUnit.SECONDS),
interval = Duration(10, TimeUnit.SECONDS),
runnable = task)
//The above one , won't be useful if we want to schedule a job at a particular time.
//It is useful if we schedule like 'run every 5 minutes.'
//So use this.
// https://github.com/enragedginger/akka-quartz-scheduler
//build.sbt
"com.enragedginger" %% "akka-quartz-scheduler" % "1.6.0-akka-2.4.x"
//application.conf
akka {
quartz {
schedules {
ValidationJob {
description = "Runs daily to calculate the validation reports and email it"
expression = "*/5 * * * * ?" //Cron job. Yes this is what I want.
}
}
}
}
import java.util.concurrent.TimeUnit
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import com.typesafe.akka.extension.quartz.QuartzSchedulerExtension
class PrintActor extends Actor{ def receive = { case msg => println(msg) } }
val actorSystem = ActorSystem("MyActorSystem")
val actorRef: ActorRef = actorSystem.actorOf(Props[PrintActor], "Myactor")
val myScheduler = QuartzSchedulerExtension(actorSystem)
myScheduler.schedule("ValidationJob", actorRef, "This is the message coming from quartz...")