Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flux Reactor - simple schedule every hour

How can I get the same effect with Reactor instead of java's scheduler?

Executors.newSingleThreadScheduledExecutor()
         .scheduleAtFixedRate(() -> counter.set(0) , computeDelay(), computePeriod(), TimeUnit.MILLISECONDS)

I tried

 Flux
    .interval(Duration.ofMillis(computeDelay()),  Duration.ofMinutes(RESET_PERIOD_MINUTES))
    .doOnNext( counter.set(0))
    .subscribe())

But It generates uneccessary Long value. I found some Schedulers in Flux API but while trying to create one I got Disposable object, then I have no idea what should do with it

like image 379
minizibi Avatar asked Nov 08 '22 09:11

minizibi


1 Answers

you found both options. the Flux.interval is useful to compose with other operators. it emits longs that represents each tick, because Flux has to emit something

Scheduler.schedulePeriodically is the other option, pretty much equivalent to ExecutorService.scheduleAtFixedRate, except it returns a Disposable rather than a Future of Void. (in both case you'd use that to cancel the work)

like image 152
Simon Baslé Avatar answered Nov 15 '22 10:11

Simon Baslé