Java – how to write cron jobs in play framework 2.3
I'm using play 2.3 8 (activator) & mongodb is DB
I have some products in the product series. Each product has a validity period. Once it expires, I need to delete the documents in the product collection
I am planning to write a cron job to delete documents in the product collection, which will run every day at a specific time
I think I can use annotations like @ on, @ everything in Java (I write code in Java, not in scala) But when I use Google search, I get some plug-ins, tools or solutions
a) https://github.com/ssachtleben/play-plugins/tree/master/cron
b) Quartz job schedule as dependency 2.3 (activator)
c) Akka works asynchronously (I don't know how to use it, how to use the game, even I'm a novice of akka)
I'm in a state of confusion. Can you give me some suggestions below
>Which one can I use according to my requirements? > Did I finish my work correctly? > Is there anything I can do at the database level? Thank you in advance
Solution
This can be done using global class, and the OnStart method can be used https://www.playframework.com/documentation/2.5.x/JavaGlobal
An abstract view of coding is given below I hope it helps
public class Global extends GlobalSettings { private Cancellable scheduler; @Override public 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); } @Override public void onStop(Application app) { scheduler.cancel(); super.onStop(app); } }