title: play framework 2 On application startup, Akka Scheduler doesn't run function repeatedly - it runs just once
body: <p>At application startup, I want to schedule a task to run every 2 minutes (for example, it may be every 1 minute).</p>
  
  <p>I've tried in 2 ways:<br />
  1. using Akka schedule method with Runnable
  abstract def schedule(initialDelay: Duration, frequency: Duration, runnable: Runnable): Cancellable<br>
  (similar to <a href="https://stackoverflow.com/questions/10085591/how-to-integrate-sms-email-reminders-in-my-play2-0-framework-web-application">how to integrate SMS &amp; EMAIL Reminders in my play2.0 framework web application.</a>,
  except that I want it to be repetead, so I use schedule instead of scheduleOnce)<br />
  2. using Akka schedule method with an Actor
  abstract def schedule(initialDelay: Duration, frequency: Duration, receiver: ActorRef, message: Any): Cancellable 
  (similar to <a href="https://stackoverflow.com/questions/12108431/java-play2-akka-for-jobs">Java Play2- Akka for jobs</a>)</p>
  
  <p>Here is Akka scheduler API:
  <a href="http://doc.akka.io/api/akka/2.0.3/#akka.actor.Scheduler" rel="nofollow noreferrer">http://doc.akka.io/api/akka/2.0.3/#akka.actor.Scheduler</a></p>
  
  <p>In both cases I'm overriding the onStart method of Global.
  My Global is in the root package: /path/TestApp/app/Global.java</p>
  
  <p>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.</p>
  
  <p>How can I make it to run repeteadly with a certain frequency?</p>
  
  <p>If you have already done this or have any idea on how to do it... please respond.</p>
  
  <p>LATER EDIT 1:<br />
  First of all, many thanks to nico_ekito.<br />
  This is the version that works:</p>
  
  <p>Class MyUntypedActor.java<br /></p>
  
  <pre><code>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);     
          }
      }
  }
  </code></pre>
  
  <p>Class Global.java<br /></p>
  
  <pre><code>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"
          );
        } 
  }
  </code></pre>
  
  <p><br />br />
  This version works with akka actors. 
  But I also have the need to make a WS call with Akka Scheduler, something like this:<br />
  <a href="https://groups.google.com/forum/?fromgroups=#!searchin/play-framework/Akka" rel="nofollow noreferrer">https://groups.google.com/forum/?fromgroups=#!searchin/play-framework/Akka</a>$20Scheduler/play-framework/-WAr7xk3H-o/oLg9ZrwwFDgJ<br />
  but in Java version.</p>
  
  <p>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...</p>
  
  <p>LATER EDIT 2:<br />
  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...<br /><br />
  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()<br />
  because it clearly has issues.</p>