I want to use Spring's reactive WebClient to poll a REST endpoint that uses long-polling.
The endpoint provides messages for a chat-channel. When I call it and there is no message, it blocks (i.e. does not return) until a message appears (or 30 seconds pass).
So, in a synchronous world, I would dedicate a thread to monitor this channel, call the endpoint via RestTemplate, wait for the result, write it to a shared queue and start the next request. A consumer can then react to new items appearing on the queue.
In the reactive world, this is a bit different. Ideally, the consumer would subscribe to a Flux of messages. The question is how to construct this Flux.
The logic should be:
Mono<String> message = WebClient.get(). […] .bodyToMono(String.class);
// When the mono completes, create a new one just as described above
// Combine all of the monos into a Flux
flux.subscribe(message -> System.out.println("New message" + message);
I think that I need some kind of switch… operator but I can find the correct one.