Manuj Kathuria Manuj Kathuria - 2 months ago 4x
Scala Question

ScheduledThreadPoolExecutor scala

I am trying to implement a Sceduled Job in scala play framework.

Following is my code:

def subCron = {

val task = new Runnable {
def run() = {
writingToFile(s"Name,Job,M,Age\n", "Media1", "FileName")

val pool = new ScheduledThreadPoolExecutor(1)

val schedule = pool.scheduleWithFixedDelay(task, 0, 30, TimeUnit.SECONDS);


Basic idea is to write a file after a specific interval. I am using
function in my
, but it seems that the code does not execute when I call the function.

Please suggest on how to make it work.


The scheduled task is being canceled right after it is defined, so it will not run.

Apart from that, Akka (which already comes with Play) also supports scheduling tasks.

import play.api.libs.concurrent.Execution.Implicits.defaultContext
// Delete a file after 10 seconds
val cancellable = system.scheduler.scheduleOnce(10.seconds) {

// Print the message "Hi" every 30 seconds, starting after 5 seconds 
val cancellable = system.scheduler.schedule(5.seconds, 300.seconds) {

Examples adapted from: