How to write cron work in game structure 2.3

I am using Play 2.3.8 (activator) and Mongodb as db

I have some products in the product collection, and each product has an expiration date and after its expiration I need to delete documents in the product collection.

I plan to write a cron task to delete documents in a collection of products that will be launched every day at the same time at a specific time.

I think I can use annotations like @on, @Every in java (I write code in a java game, not a scala game). but when i googled i got some plugins or tools or solutions

a) https://github.com/ssachtleben/play-plugins/tree/master/cron

b) Quartz task depending on the game 2.3 (activator)

c) The work of Akyn async (I can’t use it, how to indry with the game and even I'm new to Akka)

I'm confused, could you please suggest me the following

  • which can i use for my requirement?

  • Do I have the right way to do my job?

  • Are there any things that will do my work at the database level? Thanks in advance.

+5
source share
3 answers

This can be done using the global class, and back to the onstart method. https://www.playframework.com/documentation/2.5.x/JavaGlobal

Below is an abstract view of the encoding. We hope for this help.

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); } } 
+7
source

In the game, the cron role is performed through Akka Scheduler - although the pattern is very simple - it is a pretty powerful tool.

More information can be found on the Akka page and even here on StackOverflow, i.e. How to schedule daily + onStart () task in Play 2.0.4? - this is a sample for 2.0.4 in any case, you can convert it to 2.3.x easy

+1
source
  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() ); } 
+1
source

All Articles