You may want to look into ConnectableFlux
, which is similar to Flux
, but is specifically designed to continuously emit elements. You can create a WebClient
object, which produces a Mono via its exchange method by default. Then, simply refer to the route you created in your LogRailerController class to call the feed method.
public void connectFeed() {
ConnectableFlux<String> printEverySecond = WebClient.create("/") // Since your route is "/"
.post()
.body(...)
.exchange() // produces a Mono object
.flatMap(response -> response.bodyToMono(String.class)) // transformed into a Mono<String>
.flux() // now a Flux<String>
.replay(Duration.of(1, ChronoUnit.SECONDS))
.publish(); // now a ConnectableFlux<String>
printEverySecond.subscribe();
printEverySecond.connect();
}
Instead of using post().getBody()
...flatMap(...)
, you could also just use get(), and call .bodyToMono(String.class)
right after .exchange
.
Doing this, you even place your feed()
logic in the flatMap. The main issue with this strategy, and when using @RestController
too, is that the request will eventually time out, which is kind of tricky get around using RxNetty. With that said, I'd recommend having a separate component class that calls ClientClass.printEverySecond() when it returns after 10 replays, or every 10 seconds, or whichever way you think best. The advantage of this strategy over using a @RestController
is precisely that it can be called from another class the same way you would call any other bean method.
Note that all the topics here are within the scope of starter webflux dependency - I don't think you would need any others.