Blackbird
Blackbird

Reputation: 2378

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

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?

Upvotes: 7

Views: 4400

Answers (1)

Andrew Jones
Andrew Jones

Reputation: 1382

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.

Upvotes: 13

Related Questions