gpt4 book ai didi

playframework-2.0 - play 框架 2.1 - 调度异步任务

转载 作者:行者123 更新时间:2023-12-04 11:20:48 31 4
gpt4 key购买 nike

在 play 的 2.0.x 文档中,您可以看到如何安排异步任务:

http://www.playframework.org/documentation/2.0.4/ScalaAkka

Akka.system.scheduler.schedule(0 seconds, 30 minutes, testActor, "tick")

你怎么能用最近重新发布的 Play 2.1 实现同样的事情???

整个 akka 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/project/migration-guide-2.0.x-2.1.x.html

也在这里问: https://groups.google.com/d/topic/play-framework/7VcwNea6QlM/discussion

最佳答案

使用 sample codeAkka 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."))
    }

    }

    编辑

    注意,正如我从 Julien 在群组中的帖子中看到的,这足以导入 defaultContext只要:

    import play.api.libs.concurrent.Execution.Implicits.defaultContext

    关于playframework-2.0 - play 框架 2.1 - 调度异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13466047/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com