scala 如何使用 Play Framework 2.1 安排每小时工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15073819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to schedule an hourly job with Play Framework 2.1?
提问by ripper234
In Play 1 it was simply:
在 Play 1 中,它很简单:
@Every(value = "1h")
public class WebsiteStatusReporter extends Job {
@Override
public void doJob() throws Exception {
// do something
}
}
What is the equivalent for Play 2.1?
Play 2.1 的等价物是什么?
I know that Play uses akka and I found this code sample:
我知道 Play 使用 akka 并且我发现了这个代码示例:
import play.api.libs.concurrent.Execution.Implicits._
Akka.system.scheduler.schedule(0.seconds, 30.minutes, testActor, "tick")
But being a Scala noob I don't understand how works. Can someone provide a complete, working example (end to end)?
但作为一个 Scala 菜鸟,我不明白是如何工作的。有人可以提供一个完整的工作示例(端到端)吗?
回答by ndeverge
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.
它只是在应用程序开始时启动一个守护进程,然后每 5 分钟启动一次。它使用 Play 2.1 并按预期工作。
Note that this code uses the Global objectwhich allows to run some code on application startup.
回答by EECOLOR
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)
回答by biesior
Take a look into Akka's doc
查看Akka 的文档
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 testActormessage Tick
意思是:从现在开始 0 秒开始,每 30 分钟发送一次给 actortestActor消息 Tick
what's more for simple tasks you probably don;t need to use Actors - you can just schedule the new Runnable:
对于您可能不需要的简单任务,更重要的是不需要使用 Actors - 您可以只安排新的 Runnable:
def schedule(
initialDelay: Duration, frequency: Duration, runnable: Runnable): Cancellable
回答by binshi
A simple play scheduler without using Actors.
一个不使用 Actor 的简单播放调度器。
It can be done using org.quartz.scheduler and calling the scheduler from the Global class.
可以使用 org.quartz.scheduler 并从 Global 类调用调度程序来完成。

