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?
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
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
}
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
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
I used:
system.scheduler.scheduleWithFixedDelay(10.seconds, 30.seconds)(
() => {
println("Action")
}
)
© 2022 - 2024 — McMap. All rights reserved.