rip*_*234 13 scala playframework playframework-2.1
在Play 1中它只是:
@Every(value = "1h")
public class WebsiteStatusReporter extends Job {
@Override
public void doJob() throws Exception {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
Play 2.1的等价物是什么?
我知道Play使用了akka,我找到了这个代码示例:
import play.api.libs.concurrent.Execution.Implicits._
Akka.system.scheduler.schedule(0.seconds, 30.minutes, testActor, "tick")
Run Code Online (Sandbox Code Playgroud)
但作为斯卡拉诺布,我不明白它是如何运作的.有人可以提供一个完整的,有效的例子(端到端)吗?
nde*_*rge 22
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")
}
}
Run Code Online (Sandbox Code Playgroud)
它只是在应用程序启动时启动一个守护进程,然后每隔5分钟启动一次.它使用Play 2.1,它按预期工作.
请注意,此代码使用Global对象,该对象允许在应用程序启动时运行某些代码.
| 归档时间: |
|
| 查看次数: |
11731 次 |
| 最近记录: |