使用示例代码和Akka API我进行了快速测试,对我有用。
比较 2.0.4 和 2.1RC1 之间的代码,我可以看到调度程序只有两个变化:
替换进口
// import akka.util.duration._
import scala.concurrent.duration._
添加导入:
import play.api.libs.concurrent.Execution.Implicits._
app/controllers/Application.scala
package controllers
import play.api._
import play.api.mvc._
import play.libs.Akka
import akka.actor._
import scala.concurrent.duration._
import play.api.libs.concurrent.Execution.Implicits._
object Application extends Controller {
def index = Action {
// say hello
Logger.info("hello, index action started")
val Tick = "tick"
val Tack = "tack"
val tickActor = Akka.system.actorOf(Props(new Actor {
def receive = {
case Tick => Logger.info("that still ticks!")
case Tack => Logger.warn("... 7 seconds after start, only once")
}
}))
// Repeat every 5 seconds, start 5 seconds after start
Akka.system.scheduler.schedule(
5 seconds,
5 seconds,
tickActor,
Tick
)
// do only once, 7 seconds after start
Akka.system.scheduler.scheduleOnce(7 seconds, tickActor, Tack)
Ok(views.html.index("Your new application is ready."))
}
}
编辑
Nota bene,正如我从 Julien 在该组的帖子中看到的那样,defaultContext
仅导入就足够了:
import play.api.libs.concurrent.Execution.Implicits.defaultContext