2012-11-20 16 views
13

プレイの2.0.xのドキュメントで使用すると、非同期タスクをスケジュールする方法を見ることができます:プレイフレームワーク2.1 - スケジューリング非同期タスク

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

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

どのようにあなたが最近releadesが2.1を再生withthe同じことを達成することができますか? ??

全体アッカ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/discussionsample codeを使用して

答えて

20

とを10私は速いテストをし、私のために働く。私はそこに見ることができます2.0.4と2.1RC1の間のコードを比較

は、スケジューラの場合の2つだけ変化している:

  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.")) 
    } 

} 

編集

注意ベネ、私はグループのジュリアンのポストから見ることができるように、それはdefaultContextのみインポートするだけで十分です:

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

biesior's answerが大きい

ただし、には〜gあなたがしたくない場合は、俳優を通して。ここでは古き良き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") 
    } 
}) 
2

例えば、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() 
);