ryo
ryo

Reputation: 2167

Akka Stream - Timer or Scheduler like CRON

I use Akka Stream on Scala. I'd like to set a scheduler which runs on every 24:00. I tried to search for it. But I could't find what I want to do. Could you tell me how to write code?

Upvotes: 7

Views: 7313

Answers (4)

junkgui
junkgui

Reputation: 179

This is mentioned in a comment but should really be the preferred solution using only akka-streams:

Source.tick(0.seconds, 24.hours, Done).runForeach { x =>
   //do something   
}

Upvotes: 11

MikolajS.
MikolajS.

Reputation: 1

I used:

system.scheduler.scheduleWithFixedDelay(10.seconds, 30.seconds)(
 () => {
     println("Action")
   }
)

Upvotes: 0

Bennie Krijger
Bennie Krijger

Reputation: 595

Use the build in Akka scheduler, see: http://doc.akka.io/docs/akka/current/scala/scheduler.html

You can use the scheduler like:

system.scheduler.schedule(
  initialDelay = FiniteDuration(/*offset to next 24:00*/),
  interval = FiniteDuration(24, TimeUnit.HOURS),
  receiver = self,
  message = ScheduleAkkaStream
)

Then in the actor, when the ScheduleAkkaStream is received, run the job

Upvotes: 7

johanandren
johanandren

Reputation: 11479

The most commonly used one is akka quartz scheduler: https://github.com/enragedginger/akka-quartz-scheduler

This one written by me and has no additional dependencies, a bit more lightweight than using quartz with fewer bells and whistles: https://github.com/johanandren/akron

Upvotes: 3

Related Questions