Akka Stream - Timer or Scheduler like CRON
Asked Answered
T

4

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?

Tangential answered 20/7, 2016 at 8:15 Comment(0)
C
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

Corody answered 20/7, 2016 at 11:45 Comment(1)
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()Luann
S
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   
}
Suricate answered 17/1, 2019 at 22:46 Comment(0)
C
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

Corody answered 20/7, 2016 at 11:45 Comment(1)
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()Luann
F
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

File answered 20/7, 2016 at 8:32 Comment(0)
P
0

I used:

system.scheduler.scheduleWithFixedDelay(10.seconds, 30.seconds)(
 () => {
     println("Action")
   }
)
Pyosis answered 9/12, 2020 at 18:32 Comment(2)
I don't think that this what the question meant. It wants to run every 24 hours, according to the accepted answer as well.Sulfatize
The first parameter means time to start, and the second is time after that it will be repeated.Pyosis

© 2022 - 2024 — McMap. All rights reserved.