【问题标题】:play framework 2.1 - scheduling async tasksplay framework 2.1 - 调度异步任务
【发布时间】:2012-11-08 02:20:48
【问题描述】:

在 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

【问题讨论】:

    标签: playframework-2.0 scheduled-tasks akka playframework-2.1


    【解决方案1】:

    使用 sample codeAkka API 我进行了快速测试,对我有用。

    比较 2.0.4 和 2.1RC1 之间的代码,我可以看到调度程序只有两个变化:

    1. 替换导入

      // import akka.util.duration._
      import scala.concurrent.duration._
      
    2. 添加导入:

      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
    

    【讨论】:

      【解决方案2】:

      biesior's answer 很棒

      但是,如果您不想,您不必通过演员。这是使用旧 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")
        }
      })
      

      【讨论】:

        【解决方案3】:

        例如在 java 中每周六早上 15 点运行一个任务:

        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()
        );
        

        【讨论】:

          猜你喜欢
          • 2013-03-04
          • 2013-06-20
          • 1970-01-01
          • 2015-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多