'How do you take an initial sample from a flux?
The sample operation takes the first entry from flux after the duration. How can I make it such that I take the first entry on subscribe and then do the rest?
My thinking is that it is something along the lines of sample with publisher where the publisher sends a signal immediately and followed by intervals, but I am not exactly sure how to go about that.
As I typed the above... I am thinking it is something to do with Flux.concat(Flux.just(true), Flux.interval(Duration.ofSeconds(2)))
for the publisher.
Solution 1:[1]
You can transform the source Flux
to a hot one with 2
downstream subscribers:
Flux<T> flux = sourceFlux.publish().refCount(2);
Then you can create two Flux
instances, one taking the first item which will be emitted immediately and another that will be skipping the first item then will sample the next values:
Flux<T> first = flux.take(1);
Flux<T> rest = flux.skip(1).sample(Duration.ofMillis(1000));
You can then merge the two Flux
es:
Flux.merge(first, rest);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | tmarwen |