13

Play 1 では、単純に次のようになりました。

@Every(value = "1h")
public class WebsiteStatusReporter extends Job {

    @Override
    public void doJob() throws Exception {
        // do something
    }
}

Play 2.1 に相当するものは何ですか?

Play が akka を使用していることは知っており、次のコード サンプルを見つけました

import play.api.libs.concurrent.Execution.Implicits._
Akka.system.scheduler.schedule(0.seconds, 30.minutes, testActor, "tick")

しかし、Scala初心者なので、どのように機能するのかわかりません。誰かが完全で実用的な例 (エンドツーエンド) を提供できますか?

4

4 に答える 4

22

これは私のコードからの抜粋です:

import scala.concurrent.duration.DurationInt
import akka.actor.Props.apply
import play.api.Application
import play.api.GlobalSettings
import play.api.Logger
import play.api.Play
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.concurrent.Akka
import akka.actor.Props
import actor.ReminderActor

object Global extends GlobalSettings {

  override def onStart(app: Application) {

    val controllerPath = controllers.routes.Ping.ping.url
    play.api.Play.mode(app) match {
      case play.api.Mode.Test => // do not schedule anything for Test
      case _ => reminderDaemon(app)
    }

  }

  def reminderDaemon(app: Application) = {
    Logger.info("Scheduling the reminder daemon")
    val reminderActor = Akka.system(app).actorOf(Props(new ReminderActor()))
    Akka.system(app).scheduler.schedule(0 seconds, 5 minutes, reminderActor, "reminderDaemon")
  }

}

アプリの開始時にデーモンを開始し、その後 5 分ごとに開始するだけです。Play 2.1 を使用しており、期待どおりに動作します。

このコードは、アプリケーションの起動時にいくつかのコードを実行できるGlobal オブジェクトを使用していることに注意してください。

于 2013-02-25T20:50:44.660 に答える
4

例:

case object Tick

class TestActor extends Actor {

  def receive = {
    case Tick => //...
  }
}

val testActor = Akka.system.actorOf(Props[TestActor], name = "testActor")

Akka.system.scheduler.schedule(0.seconds, 30.minutes, testActor, Tick)
于 2013-02-25T18:45:03.407 に答える
3

Akka のドキュメントを見てみましょう

あなたが与えたサンプルは次のとおりです。

def schedule(
  initialDelay: Duration,
  frequency: Duration,
  receiver: ActorRef,
  message: Any): Cancellable

手段: 今から 0 秒後に開始し、30 分ごとにアクターtestActorメッセージ に送信します。Tick

さらに、アクターを使用する必要がない単純なタスクの場合は、新しい Runnable をスケジュールするだけです。

  def schedule(
    initialDelay: Duration, frequency: Duration, runnable: Runnable): Cancellable

他の回答のより詳細な説明

于 2013-02-25T18:51:43.520 に答える