在剧中的2.0.x的文档,你可以看到如何安排异步任务:
http://www.playframework.org/documentation/2.0.4/ScalaAkka
Akka.system.scheduler.schedule(0 seconds, 30 minutes, testActor, "tick")
你怎么能做到同样的事情的任意不等阶最近releades播放2.1 ???
整个阿卡API似乎有所改变......
我检查:
https://github.com/playframework/Play20/wiki/Highlights https://github.com/playframework/Play20/wiki/Migration也http://doc.akka.io/docs/akka/2.1.0- RC1 /项目/迁移指南-2.0.x版本,2.1.x.html
还问在这里: https://groups.google.com/d/topic/play-framework/7VcwNea6QlM/discussion
使用示例代码和阿卡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."))
}
}
编辑
诺塔好处,因为我可以从朱利安在组后看到,这足以导入defaultContext
只:
import play.api.libs.concurrent.Execution.Implicits.defaultContext
biesior的答案是伟大的
不过,你不必去通过一个演员,如果你不想。 下面是使用好老了相同的答案java.lang.Runnable
:
Akka.system.scheduler.schedule(5 minutes, 5 minutes, new Runnable {
def run() {
Logger.info("that still ticks!")
}
})
Akka.system.scheduler.scheduleOnce(7 minutes, new Runnable {
def run() {
Logger.warn("... 7 seconds after start, only once")
}
})
例如Java中的15 AM运行每星期六任务:
DateTime now = new DateTime();
DateTime plannedStart = new DateTime()
.withDayOfWeek(DateTimeConstants.SATURDAY)
.withHourOfDay(15)
.withMinuteOfHour(0)
.withSecondOfMinute(0)
.withMillisOfSecond(0);
DateTime nextRun = (now.isAfter(plannedStart))
? plannedStart.plusDays(7)
: plannedStart;
Long nextRunInSeconds = (long) secondsBetween(now, nextRun).getSeconds();
Akka.system().scheduler().schedule(
Duration.create(nextRunInSeconds, TimeUnit.SECONDS),
Duration.create(7, TimeUnit.DAYS) ,
new Runnable() {
public void run() {
Logger.info("-------------------------scheduler start : " + new DateTime());
}
},
Akka.system().dispatcher()
);