I make repeated paged WebClient requests to a third-party web service. The implementation I have now works but is blocking.
My implementation so far:
var elementsPerPage = 10;
Flux
.generate(
() -> 0,
(pageIndex, emitter) -> {
BlahServiceResponse blahServiceResponse =
webClient
.get()
.uri("/blah?pageIndex={pageIndex}", pageIndex)
.retrieve()
.bodyToMono(BlahServiceResponse.class)
.block(); // Yuck!!!
if (blahServiceResponse.getStudents().size() > 0) {
emitter.next(blahServiceResponse);
} else {
emitter.complete();
}
return pageIndex + elementsPerPage;
}
)
.subscribe(System.out::println); // Replace me with actual logic
For understandable reasons, an "IllegalStateException: The generator didn't call any of the SynchronousSink method" exception is thrown if the code above is changed to the following:
webClient
.get()
...
.bodyToMono(BlahServiceResponse.class)
.subscribe(emitter::next);
So I started looking for an async Sink and realized it was Flux|MonoSink. But as far as I can see there is no builder method in Flux that supports stateful element generation with Flux|MonoSink.
Am I missing something and is there a more elegant approach to this?