I have created a Flux
that uses subscribeOn(Subscribers.boundedElastic)
. It is wrapping an Iterator
I have created. What I would like is for the Flux
to request the next element in a worker thread while the Subscriber
is working on the current result. Ideally the Flux
would call hasNext() -> next() -> hasNext() THEN send the result of the next() call to the subscriber. I tried doing this with publish(2)
thinking the pre-fetch of 2 would do the trick but didn't seem to do what I wanted. Here is the code for creating the Flux.
@Override
public Flux<SearchResponse> createDeepQueryFlux(@NonNull PITSearchInput input) {
validator.validate(input);
return Flux.<SearchResponse, PointInTimeIterator>generate(
() -> new PointInTimeIterator(forwardingRestHighLevelClient, input),
(deepQueryIterator, sink) -> {
log.info("Generate called");
if (deepQueryIterator.hasNext()) {
sink.next(deepQueryIterator.next());
} else {
sink.complete();
}
return deepQueryIterator;
},
(deepQueryIterator) -> deepQueryIterator.shutdown())
.subscribeOn(Schedulers.boundedElastic());
}