Skip to content

Instantly share code, notes, and snippets.

@shinobu-aoki
Created April 2, 2012 07:10
Show Gist options
  • Save shinobu-aoki/2281360 to your computer and use it in GitHub Desktop.
Save shinobu-aoki/2281360 to your computer and use it in GitHub Desktop.
GlobalSettingsとAkkaを使ってplay 2.0でJob(Actor)を定期実行する
import play.api._
import play.api.libs.concurrent.Akka
import akka.actor._
import akka.util.duration._
object Global extends GlobalSettings {
case class LogMessage()
class DateActor extends Actor {
def receive = {
case LogMessage => Logger.info(new java.util.Date().toString)
}
}
var actorJob: Cancellable = _
override def onStart(app: Application) {
Logger.info("Application has started")
val actorSystem = Akka.system(app)
val dateActor = actorSystem.actorOf(Props[DateActor], name = "dateactor")
actorJob = actorSystem.scheduler.schedule(0 seconds, 1 minutes, dateActor, LogMessage)
}
override def onStop(app: Application) {
actorJob.cancel()
Logger.info("Application shutdown...")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment