播放框架2.1-安排异步任务 [英] play framework 2.1 - scheduling async tasks
问题描述
在游戏的2.0.x文档中,您可以了解如何安排异步任务:
In play's 2.0.x doc you can see how to schedule asynchronous tasks:
http://www.playframework.org/documentation/2.0.4/ScalaAkka >
Akka.system.scheduler.schedule(0 seconds, 30 minutes, testActor, "tick")
使用最新的Play 2.1,您如何实现同一目标?
How can you achieve the same thing withthe recently releades Play 2.1???
整个akka API似乎已经改变了...
The whole akka API seems to have changed...
我已检查:
https://github.com/playframework/Play20/wiki/Highlights https://github.com/playframework/Play20/wiki/迁移 并且 http://doc.akka. io/docs/akka/2.1.0-RC1/project/migration-guide-2.0.x-2.1.x.html
https://github.com/playframework/Play20/wiki/Highlights https://github.com/playframework/Play20/wiki/Migration and also http://doc.akka.io/docs/akka/2.1.0-RC1/project/migration-guide-2.0.x-2.1.x.html
也在此处询问: https://groups.google.com/d/topic/play -framework/7VcwNea6QlM/讨论
推荐答案
使用 Akka API 我进行了快速测试,为我工作.
Using sample code and Akka API I made fast test, works for me.
比较2.0.4和2.1RC1之间的代码,我看到在调度程序的情况下只有两个更改:
Comparing code between 2.0.4 and 2.1RC1 I can see there're only two changes in case of scheduler:
-
已替换的导入
replaced import
// import akka.util.duration._
import scala.concurrent.duration._
添加了导入:
added import:
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足够只导入defaultContext
了:
Nota bene, as I can see from Julien's post on the group, that's enough to import defaultContext
only:
import play.api.libs.concurrent.Execution.Implicits.defaultContext
这篇关于播放框架2.1-安排异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!