7

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?

Blackbird
  • 2,368
  • 4
  • 26
  • 41

1 Answers1

13

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.

Andrew Jones
  • 1,382
  • 10
  • 26
  • The code you're posted (using an actor) is very similar to what I have now. I have a problem with it: http://stackoverflow.com/questions/19816148/simple-akka-mailbox-configuration-to-discard-overflowing-messages – Blackbird Nov 11 '13 at 08:16
  • 1
    I've updated my answer after sleeping on it. Much of the code that I originally had was unnecessary. This code has been tested in the REPL and works. – Andrew Jones Nov 11 '13 at 13:56
  • Thanks, it replies to the question. Still stuck with my problem though (cf link in my comment above): even doing this way, it uses some sort of message queue behind the scenes. – Blackbird Nov 11 '13 at 22:54