2013-02-25 72 views
12

在播放1这是干脆:如何使用Play Framework 2.1安排每小时工作?

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

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

什么是等效播放2.1?

我知道Play使用阿卡和我found this code sample

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

但作为一个斯卡拉小白我不明白是如何工作的。有人可以提供一个完整的工作示例(端到端)吗?

回答

22

下面是一个code of mine的摘录:

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 object

+0

有趣。不过,我的onStart()方法不会使用上面的代码调用... – ripper234 2013-02-25 21:05:54

+1

因为您必须使用我的答案中提到的Global对象。 'Global.scala'必须存储在您的应用程序文件夹(没有任何包) – 2013-02-25 21:07:21

+1

约定超过配置:(它终于工作!非常感谢 – ripper234 2013-02-25 21:10:26

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) 
+0

什么是'0.seconds'和'30.minutes'?这些不为我编译。 – ripper234 2013-02-25 19:53:40

+0

当我在课后编译代码的第一行时(无论它是什么),我得到了'期望的类或对象定义'。任何想法为什么? – ripper234 2013-02-25 20:01:07

+1

使用此导入作为'seconds'部分:'import scala.concurrent.duration._' – EECOLOR 2013-02-25 20:03:26

3

看看到你给Akka's doc

样品是:

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 

More detailed description in other answer

+0

你能看看我与EECOLOR的通信吗?这是我所达到的最后一个代码,它编译了,但没有做任何事情。 https://gist.github.com/ripper234/5032857 – ripper234 2013-02-25 20:41:46

+1

也许我错过了一些东西,但通常调度任务转到Global对象(就像@nico_ekito显示:P) – biesior 2013-02-25 20:52:58

-1

一个简单的播放调度程序,不使用Actor。

可以使用org.quartz.scheduler并从Global类中调用调度程序来完成。

Sample scheduler

+0

这可能在理论上回答这个问题,但最好在未来的用户中包含答案的基本部分,并提供参考链接[link-dominated answers](// meta.stackexchange.com/questions/8231)可以通过[ link rot](// en.wikipedia.org/wiki/Link_rot)。 – Mogsdad 2016-03-10 03:28:50

+0

谢谢。未来会避免它。 – binshi 2016-03-11 00:52:17

相关问题