Michael Michael - 13 days ago 6
Scala Question

Scheduled Executor in Scala

In Java I can use Scheduled Executor to schedule tasks to run after a given delay. I can use it in Scala but I wonder if there is a Scala API for that.

Is there any Scala API (as opposed to

Scheduled Executor
in Java) to schedule tasks?

Answer

Akka has something similar with schedulers:

http://doc.akka.io/api/akka/2.1.4/#akka.actor.Scheduler

You can obtain one from the actor system:

val actorSystem = ActorSystem()
val scheduler = actorSystem.scheduler
val task = new Runnable { def run() { log.info("Hello") } }
implicit val executor = actorSystem.dispatcher

scheduler.schedule(
  initialDelay = Duration(5, TimeUnit.SECONDS),
  interval = Duration(10, TimeUnit.SECONDS),
  runnable = task)

If you are using Akka or something based on it, like Play, that would be the way to go.

Comments