how to write cron job in play framework 2.3
This can be done using the Global Class, and over riding the onstart method. https://www.playframework.com/documentation/2.5.x/JavaGlobal
An abstract view of the coding is given below. Hope this help
public class Global extends GlobalSettings {private Cancellable scheduler;@Overridepublic void onStart(Application application) { int timeDelayFromAppStartToLogFirstLogInMs = 0; int timeGapBetweenMemoryLogsInMinutes = 10; scheduler = Akka.system().scheduler().schedule(Duration.create(timeDelayFromAppStartToLogFirstLogInMs, TimeUnit.MILLISECONDS), Duration.create(timeGapBetweenMemoryLogsInMinutes, TimeUnit.MINUTES), new Runnable() { @Override public void run() { System.out.println("Cron Job"); // Call a function (to print JVM stats) } }, Akka.system().dispatcher()); super.onStart(application);}@Overridepublic void onStop(Application app) { scheduler.cancel(); super.onStop(app);}}
Akka.system().scheduler().scheduleOnce( Duration.create(0, TimeUnit.MILLISECONDS), new Runnable() { public void run() { Logger.info("ON START --- " + System.currentTimeMillis()); } }, Akka.system().dispatcher() ); Akka.system().scheduler().schedule( Duration.create(nextExecutionInSeconds(8, 0), TimeUnit.SECONDS), Duration.create(24, TimeUnit.HOURS), new Runnable() { @Override public void run() { Logger.info("EVERY DAY AT 8:00 --- " + System.currentTimeMillis()); } }, Akka.system().dispatcher() ); Akka.system().scheduler().schedule( Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay 0 milliseconds Duration.create(60, TimeUnit.SECONDS), //Frequency 30 minutes new Runnable() { @Override public void run() { Logger.info("creating the runnable"); Logger.info("EVERY 60 MInutes --- " + System.currentTimeMillis()); executeAllMongoAggregations(); } }, Akka.system().dispatcher() ); } Akka.system().scheduler().scheduleOnce( Duration.create(0, TimeUnit.MILLISECONDS), new Runnable() { public void run() { Logger.info("ON START --- " + System.currentTimeMillis()); } }, Akka.system().dispatcher() ); Akka.system().scheduler().schedule( Duration.create(nextExecutionInSeconds(8, 0), TimeUnit.SECONDS), Duration.create(24, TimeUnit.HOURS), new Runnable() { @Override public void run() { Logger.info("EVERY DAY AT 8:00 --- " + System.currentTimeMillis()); } }, Akka.system().dispatcher() ); Akka.system().scheduler().schedule( Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay 0 milliseconds Duration.create(60, TimeUnit.SECONDS), //Frequency 30 minutes new Runnable() { @Override public void run() { Logger.info("creating the runnable"); Logger.info("EVERY 60 MInutes --- " + System.currentTimeMillis()); } }, Akka.system().dispatcher() ); }
In Play role of the cron is done via Akka Scheduler - although sample is very easy - it's quite powerful tool.
More details can be found on Akka's page and even here on the StackOverflow i.e. How to schedule task daily + onStart() in Play 2.0.4? - this is sample for 2.0.4 anyway you'll be able to convert it to 2.3.x easy