I need to execute a piece of code 1 time everyday in playframework2.0.4 when I try to do with the class extends GlobalSettings it works. But it works for every instance requesting. I want it works when server starts and does its duty everyday 1 time.
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());
}
});
}
}
And this is my controller where start the class above
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 "));
}
}
Scheduler tasks should be placed only in Global class. Create two tasks, schedule only once first with
initialDelay= 0 milliseconds.For the second task, you need to calculate seconds between current DateTime and next planned occurrence (ie. tomorrow at 8:00 o’clock) using common date/time classes, then set this difference as
initialDelayand also setfrequencyto 24 hours.In result, it will start at the application start and will schedule the task for execution each day at required hour.
Edit
There’s complete sample, (save/edit the class:
/app/Global.java):