duckertito duckertito - 24 days ago 9
Java Question

How to schedule a task at specific time in Scala?

I want to execute my Scala function

cleaner.run()
at particular time every day. I was reading about Akka and Play framework. However it looks like Akka supports the intervals, e.g. "execute a task every 30 minutes", but it does not support executing tasks at the exact time.
I would appreciate if someone can help me to put the things in order (using my particular example), because I am a bit missed with all the information and forum threads that I read.

So, this is my Scala object and I want to schedule
cleaner.run()
every day at 23:55.

object MyTestManager {

def main(args: Array[String]) {

val cleaner = new Cleaner()

cleaner.run()

}
}


I would be interested in doing something like this, but cannot find the way of passing it to Scala 2.10.

import play.jobs.*;

/** Fire at 12pm (noon) every day **/
@On("0 0 12 * * ?")
public class Bootstrap extends Job {

public void doJob() {
Logger.info("Maintenance job ...");
...
}

}


Also, after packaging the Scala code, what is the right way to launch the application so that it keeps executing every day at 23:55? Will a simple
java -jar target/scala-2.10/my-assembly-1.0.jar
do this job, or will it execute the code just once?

Answer

What you are looking for is Akka-Quartz.

There is a trigger functionality in there. Using that functionality you can do your task.

For more info take a look here :

  1. How to fire akka scheduler daily for a particular time?
  2. https://blog.knoldus.com/2016/01/18/code-dissection-akka-quartz-scheduler-scalas-way-of-scheduling/