score:22
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.
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
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)
Source: stackoverflow.com
Related Query
- How to schedule an hourly job with Play Framework 2.1?
- How to use IntelliJ with Play Framework and Scala
- How to create a global variable with Play Framework 2.0
- How do I get Intellij IDEA 12.0 to work with Play Framework 2.1.0 app and Scala 2.10.0?
- How to print @ symbol in HTML with play framework (scala)
- How to run Play Framework 2.2.0 with -feature option
- How to correctly schedule task in Play Framework 2.4.2 scala?
- How to set up play framework ApplicationLoader and Macwire to work with custom routes?
- How i can integrate Apache Spark with the Play Framework to display predictions in real time?
- How to tune Play Framework application with proper threadpools?
- How to use the Play Framework with Google App Engine with locally installed Java 7?
- Play framework JSON transformers, how to work with recursive paths (jsPath)?
- How to use SORM framework with Play Framework?
- How to create an instance of a model with the ebean framework and scala in Play 2.2
- How to read and write Anorm object with the new JSON API in Play Framework 2.1-RC2?
- With Scala 2.10.2, SBT 0.13.0, Specs2 & Play Framework 2.2.1 how can I control logging whilst running tests?
- How do I set up Play Framework 2.0 in Netbeans with Scala IDE
- How to delete file right after processing it with Play Framework
- How to create JSON output from a combined group of composite classes with Play framework
- Play Framework Enumerator : How can you zip enumerator with Index
- How do you set the viewport, width, with phantomjs with play 2 framework
- How to check if URL is a valid link with play framework
- How to "rewrite" urls or routes with Play Framework 2
- how to schedule a quartz job with scala
- How to show streams inline as HTTP chunked response with play framework
- How to redirect request in play framework with scala?
- Play Framework 2 / How to ensure years with 4 digits from form's input date?
- How to send email with attached file in scala and play framework (2.3.9) using activator?
- How to map checkboxes in Play Framework with Scala Forms?
- How handle a POST request with Kafka, Alpakka Kafka, Play Framework and Websocket?
More Query from same tag
- Writing functional code, example of find function
- Parallelised collections in Spark
- Monadic fold with State monad in constant space (heap and stack)?
- how to extract correct id from json with jsonpath Expression in Gatling
- watchTermination is not triggered in an akka-http flow
- Is there a chance installing a not yet released plugin corrupt eclipse installation
- How to find the longest common prefix of two strings in Scala?
- Better Approach than If
- org.apache.spark.SparkException: This RDD lacks a SparkContext error
- Make constructor parameters public in subclass
- Which elememt does DataFrame. DropDuplicate drop
- DataFrame to LabeledPoint: df.collect() Error
- serving hierarchical static content with spray
- How to sum/combine each value in List of List in scala
- Can't gain the `pipeTo` method on [[scala.concurrent.Future]]
- Use variable for type checking code in Scala
- Split a list in several files scala
- How to create List from Range
- Get tweets from API using Akka-HTTP, Twitter4j
- Why can't Actors complete all work although I created 10000 of them?
- groupByKey in Spark dataset, execute custom logic along the aggregation
- Immutable Vector or List Builder in Scala
- Using scala play-json 2.4.x, how do I extract the name of a json object into a different object?
- For `case class Cc(a: Int, b: Int) extends MyTraitA`, where does `MyTraitA` appear with `Product` and `Serializable` traits?
- Iterate Set in order Play Framework
- Slick 2.0 Insert with default values and get back the ID
- Logging from an sbt plugin
- Scala Spark - Mapping values of tuple to single
- Scala remove a single object from a list
- Slick - Join Table with Rows Limit