Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduling a function to be run every second with Akka's Scheduler

Tags:

scala

akka

I want to run this Scala function every second:

object AuthTasker {
  def cleanTokens() {
    [...]
  }
}

Akka's Scheduler has the following function: schedule(initialDelay: FiniteDuration, interval: FiniteDuration)(f: ⇒ Unit)

Can I use that function in order to call AuthTasker.cleanToken() every second?

like image 827
Blackbird Avatar asked Nov 10 '13 23:11

Blackbird


1 Answers

To answer your question: Yes, you can call AuthTasker.cleanToken() every second with the schedule method. I recommend the following (included the imports for you):

import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import ExecutionContext.Implicits.global

val system = akka.actor.ActorSystem("system")
system.scheduler.schedule(0 seconds, 1 seconds)(AuthTasker.cleanTokens)

Note: This updated code calls the scheduled method every one second rather than every 0 seconds. I caught the mistake when looking back at the code.

like image 101
Andrew Jones Avatar answered Nov 14 '22 23:11

Andrew Jones