Reputation: 1555
I have an Akka actor that validates random data and makes some changes to it based on that data's show time and updates it. Currently what I'm doing is using this code inside a controller:
static ActorRef instance = Akka.system().actorOf(new Props(ValidateAndChangeIt.class));
static {
Akka.system().scheduler().schedule(
Duration.Zero(),
Duration.create(5, TimeUnit.MINUTES),
instance, "VALIDATE"
);
}
The problem with using this inside a controller is that someone has to access a page processed by that controller for the actor to start, and if this doesn't happen, everything stays paused.
Is there a way to do this at server start? I actually don't know how it behaves if the actor generates an exception. Does it stop future schedules or does it continue? In case it doesn't, is there any way of making the actor re-schedule in case any crash or error?
Upvotes: 5
Views: 4850
Reputation: 399
Play Framework provides a way by which scheduling of a job can be done in the Global.java
without you explicitly invoking it.
public class Global extends GlobalSettings {
private Cancellable scheduler;
@Override
public void onStart(Application app) {
super.onStart(app);
schedule();
}
@Override
public void onStop(Application app) {
//Stop the scheduler
if (scheduler != null) {
scheduler.cancel();
this.scheduler = null;
}
}
private void schedule() {
try {
ActorRef helloActor = Akka.system().actorOf(new Props(HelloActor.class));
scheduler = Akka.system().scheduler().schedule(
Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay 0 milliseconds
Duration.create(30, TimeUnit.MINUTES), //Frequency 30 minutes
helloActor,
"tick",
Akka.system().dispatcher(), null);
}catch (IllegalStateException e){
Logger.error("Error caused by reloading application", e);
}catch (Exception e) {
Logger.error("", e);
}
}
}
And create the Actor, HelloActor.java
In the on onReceive
method, you can do the processing of data, sending emails, etc.
public class HelloActor extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
// Do the processing here. Or better call another class that does the processing.
// This method will be called when ever the job runs.
if (message.equals("tick")) {
//Do something
// controllers.Application.sendEmails();
} else {
unhandled(message);
}
}
}
Hope this helps.
Upvotes: 1
Reputation: 21564
For running your code at server startup, take a look at the Global object: move the code from your controller to the onStart()
method:
public class Global extends GlobalSettings {
@Override
public void onStart(Application app) {
ActorRef instance = Akka.system().actorOf(new Props(ValidateAndChangeIt.class));
Akka.system().scheduler().schedule(
Duration.Zero(),
Duration.create(5, TimeUnit.MINUTES),
instance, "VALIDATE"
);
}
}
Upvotes: 13