我正在寻找在响应式API中使用定时任务的方法。
我知道它使用线程池,因此与webflux组件不太兼容。
您有类似的解决方案吗?
您有类似的解决方案吗?
您可以在@Scheduled
方法中尝试使用Schedulers.immediate()
:
doWork()
.subscribeOn(Schedulers.immediate())
.subscribe()
有几种方法可以实现。考虑你想如何安排时间表,你也可以使用以下方式。
@Configuration
class ApplicationConfiguration() {
@PostConstruct
fun init() {
Flux.interval(Duration.ofMinutes(12))
.onBackpressureDrop()
.flatMap { /* some task that return Mono<T> */ }
.subscribeOn(Schedulers.boundedElastic())
.subscribe()
}
}
subscribeOn(Schedulers.boundedElastic())
。另外,我正在使用onBackpressureDrop
,但您的要求可能与此不同。Webflux有自己的调度器,我猜应该是这样做:
Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit);