如何在Play 2.0.4中安排每天+ onStart()任务?
2022-09-03 03:52:23
我需要每天在playframework2.0.4中执行一段代码1次,当我尝试使用扩展GlobalSettings的类时,它可以工作。但它适用于每个请求实例。我希望它在服务器启动时工作,并每天1次完成其职责。
package controllers;
import java.util.concurrent.TimeUnit;
import akka.util.Duration;
import play.Application;
import play.GlobalSettings;
import play.libs.Akka;
public class ParserJobApp extends GlobalSettings{
@Override
public void onStart(Application app) {
Akka.system().scheduler().schedule(Duration.create(0, TimeUnit.MILLISECONDS),Duration.create(6, TimeUnit.SECONDS), new Runnable() {
@Override
public void run() {
System.out.println("AAA --- "+System.currentTimeMillis());
}
});
}
}
这是我的控制器,从那里开始上面的类
public class Application extends Controller {
public static Result index() {
ParserJobApp pr=new ParserJobApp();
pr.onStart(null);
System.out.println("sfsdfsdf");
return ok(index.render("Your new "));
}
}