score:22

Accepted answer

Here is an extract from a code of mine:

import scala.concurrent.duration.DurationInt
import akka.actor.Props.apply
import play.api.Application
import play.api.GlobalSettings
import play.api.Logger
import play.api.Play
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.concurrent.Akka
import akka.actor.Props
import actor.ReminderActor

object Global extends GlobalSettings {

  override def onStart(app: Application) {

    val controllerPath = controllers.routes.Ping.ping.url
    play.api.Play.mode(app) match {
      case play.api.Mode.Test => // do not schedule anything for Test
      case _ => reminderDaemon(app)
    }

  }

  def reminderDaemon(app: Application) = {
    Logger.info("Scheduling the reminder daemon")
    val reminderActor = Akka.system(app).actorOf(Props(new ReminderActor()))
    Akka.system(app).scheduler.schedule(0 seconds, 5 minutes, reminderActor, "reminderDaemon")
  }

}

It simply starts a daemon at the start of the app, and then, every 5 minutes. It uses Play 2.1 and it works as expected.

Note that this code uses the Global object which allows to run some code on application startup.

score:-1

A simple play scheduler without using Actors.

It can be done using org.quartz.scheduler and calling the scheduler from the Global class.

Sample scheduler

score:3

Take a look into Akka's doc

sample you gave is:

def schedule(
  initialDelay: Duration,
  frequency: Duration,
  receiver: ActorRef,
  message: Any): Cancellable

Means: start 0 seconds from now, at every 30 minutes send to the actor testActor message Tick

what's more for simple tasks you probably don;t need to use Actors - you can just schedule the new Runnable:

  def schedule(
    initialDelay: Duration, frequency: Duration, runnable: Runnable): Cancellable

More detailed description in other answer

score:4

An example:

case object Tick

class TestActor extends Actor {

  def receive = {
    case Tick => //...
  }
}

val testActor = Akka.system.actorOf(Props[TestActor], name = "testActor")

Akka.system.scheduler.schedule(0.seconds, 30.minutes, testActor, Tick)

Related Query

More Query from same tag