Where is the job support in Play 2.0?

旧城冷巷雨未停 提交于 2019-11-26 16:35:04

问题


In Play 1.0, we can define some jobs which will be executed in the background:

@OnApplicatonStart
@Every("1h")
public class DataJob extends Job {
    public void doJob() {
       // ...
    }
}

But I can't find it in Play 2.0. Do I miss something?


回答1:


You could use the scheduler service in akka.

http://doc.akka.io/docs/akka/2.0/java/scheduler.html

http://doc.akka.io/docs/akka/2.0/scala/scheduler.html

Basically you create an actor that executes your logic if it receives a certain message.




回答2:


For the acutal job part this seems to be the way in Java,

Akka.system().scheduler().schedule(
        Duration.create(0, MILLISECONDS),   // initial delay 
        Duration.create(5, MINUTES),        // run job every 5 minutes

        new Runnable() 
        {
            public void run() 
            {
                ....
            }
        }
    );



回答3:


Fixed the links in original accepted answer which posted by JonasAnso

To obtain the functionality of OnApplicationStart you can use Global onStart

  • https://www.playframework.com/documentation/2.4.x/api/scala/index.html#play.api.GlobalSettings
  • https://www.playframework.com/documentation/2.4.x/api/java/play/GlobalSettings.html

Here you can schedule your actors using Akka.

  • https://www.playframework.com/documentation/2.4.x/ScalaAkka
  • https://www.playframework.com/documentation/2.4.x/JavaAkka

Hope it helps.



来源:https://stackoverflow.com/questions/9339714/where-is-the-job-support-in-play-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!