7

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?

ryo
  • 2,009
  • 5
  • 24
  • 44

4 Answers4

11

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   
}
junkgui
  • 179
  • 2
  • 7
7

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

Bennie Krijger
  • 595
  • 7
  • 10
  • 2
    I don't understand how you can get the ScheduleAkkaStream event in your akka stream? Would it not be simpler to just call Source.tick() – 0x26res Jul 30 '18 at 10:12
3

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

johanandren
  • 11,249
  • 1
  • 25
  • 30
0

I used:

system.scheduler.scheduleWithFixedDelay(10.seconds, 30.seconds)(
 () => {
     println("Action")
   }
)
MikolajS.
  • 1
  • 1
  • 1
    I don't think that this what the question meant. It wants to run every 24 hours, according to the accepted answer as well. – Tomer Shetah Dec 09 '20 at 20:59
  • The first parameter means time to start, and the second is time after that it will be repeated. – MikolajS. Dec 10 '20 at 23:06