I'm using Play 2.3.8(activator) & Mongodb as db
I've some products in products collection and each product has expiry date and once its expiry
I need to remove documents in products collection.
I'm planing to write cron job to remove documents in products collection which will run every day at once in particular time.
I'm thinking I can use Annotations like @on, @Every in java(I'm writing code in play java, not play scala). but when I googled i got some plugins or tools or solutions
a) https://github.com/ssachtleben/play-plugins/tree/master/cron
b) Quartz Job schedular as dependency to play 2.3(activator)
c) Akka async jobs(I don't how to use this, how to intigrate with play and even I'm new to Akka)
I'm in confusion state, Could you please suggest me in following
which one I can use for my requirement?
Am I in correct path to do my job?
Is there any thing which will do my job at database level? Thanks in advance.
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;
@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);
}
}
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()
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With