At application startup, I want to schedule a task to run every 2 minutes (for example, it may be every 1 minute).
I've tried in 2 ways:
1. using Akka schedule method with Runnable
abstract def schedule(initialDelay: Duration, frequency: Duration, runnable: Runnable): Cancellable
(similar to how to integrate SMS & EMAIL Reminders in my play2.0 framework web application.,
except that I want it to be repetead, so I use schedule instead of scheduleOnce)
2. using Akka schedule method with an Actor
abstract def schedule(initialDelay: Duration, frequency: Duration, receiver: ActorRef, message: Any): Cancellable
(similar to Java Play2- Akka for jobs)
Here is Akka scheduler API: http://doc.akka.io/api/akka/2.0.3/#akka.actor.Scheduler
In both cases I'm overriding the onStart method of Global. My Global is in the root package: /path/TestApp/app/Global.java
In both cases, when I run the app in production mode (in dev mode it doesn't work at all... there was a question on stack about this) it runs the function or it sends the message only once.
How can I make it to run repeteadly with a certain frequency?
If you have already done this or have any idea on how to do it... please respond.
LATER EDIT 1:
First of all, many thanks to nico_ekito.
This is the version that works:
Class MyUntypedActor.java
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
public class MyUntypedActor extends UntypedActor {
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {
if (message instanceof String) {
log.info("Received String message: {}", message);
}
else {
unhandled(message);
}
}
}
Class Global.java
import java.util.concurrent.TimeUnit;
import play.Application;
import play.GlobalSettings;
import play.libs.Akka;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.util.Duration;
public class Global extends GlobalSettings {
@Override
public void onStart(Application app) {
ActorRef instance = Akka.system().actorOf(new Props(MyUntypedActor.class));
Akka.system().scheduler().schedule(
Duration.Zero(),
Duration.create(10, TimeUnit.SECONDS),
instance, "VALIDATE"
);
}
}
br />
This version works with akka actors.
But I also have the need to make a WS call with Akka Scheduler, something like this:
https://groups.google.com/forum/?fromgroups=#!searchin/play-framework/Akka$20Scheduler/play-framework/-WAr7xk3H-o/oLg9ZrwwFDgJ
but in Java version.
So... how do I schedule a function to be run with a certain frequency without using actors? For my other cases, actors work, but for a WS call it seems like it should be simpler...
LATER EDIT 2:
I've found out that case 1 (using Akka schedule method with Runnable) didn't worked because in the same onStart method I had defined an actor... it seems that Runnable doesn't "like" actor instances...
Case 2, which is in essence like the case from nico_ekito still doesn't work... So I would avoid the syntax with
new Props().withCreator(new UntypedActorFactory()
because it clearly has issues.