I'm searching a way to use scheduled tasks in a reactive API. I know that it uses the thread pool so it's not very compatible with the webflux components.
Do you have an equivalent to do the job?
I'm searching a way to use scheduled tasks in a reactive API. I know that it uses the thread pool so it's not very compatible with the webflux components.
Do you have an equivalent to do the job?
You may try to use Schedulers.immediate() inside @Scheduled method:
doWork()
.subscribeOn(Schedulers.immediate())
.subscribe()
As a consequence tasks run on the thread that submitted them.
There are several ways to do. Considering how you want to schedule it you can use following as well.
@Configuration
class ApplicationConfiguration() {
@PostConstruct
fun init() {
Flux.interval(Duration.ofMinutes(12))
.onBackpressureDrop()
.flatMap { /* some task that return Mono<T> */ }
.subscribeOn(Schedulers.boundedElastic())
.subscribe()
}
}
Please note subscribeOn(Schedulers.boundedElastic()) is not required unless the call is blocking. Also I am using onBackpressureDrop but your requirements may be different than that.
Webflux has it's own scheduler, and I gues this way should do it:
Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit);