Issue
Normally I would do something like this to schedule a job to be periodically executed in Spring with cron in a given timezone:
@Scheduled(cron = "0 0 10 * * *", zone = "Europe/Stockholm")
public void scheduleStuff() {
// Do stuff
}
This is will block the thread calling scheduleStuff
until the job is completed. However in this case the "stuff" I want to do is all implemented using Springs' non-blocking building blocks of project reactor (i.e. Mono
, Flux
etc).
E.g. let's say that I want to trigger this function periodically:
Flux<Void> stuff() {
return ..
}
I can of course simply call stuff().subscribe()
(or even stuff().block()
) but this will block the thread. Is there a better way to achieve the same things as @Scheduled(cron = "0 0 10 * * *", zone = "Europe/Stockholm")
for non-blocking code?
I'm using Spring Boot 2.1.
Solution
Actualy, subscribe()
doesn't block your thread. You could call stuff().subscribeOn(Schedulers.parallel()).subscribe()
or other scheduler to be sure that execution will be done in a separate thread, if you really need it.
Answered By - Alexander Pankin Answer Checked By - David Goodson (WPSolving Volunteer)