I am programming a web UI that monitors another Java application in Spring Boot, supported by Spring Actuator.
Right now I can poll Spring Actuator's endpoints from the other client Sprint Boot web application using a regular Get request (via Spring WebFlux's WebClient.get() method).
But if some data changes, let's say one of the metrics endpoints (i.e. the CPU usage which always changes), I have to "refresh" the get request. I can set it on a scheduled timer via the @Scheduled annotation, but I feel like there's a better way of doing it?
I know how to retrieve a Flux from a get request of constant flowing data, but on the server, a flux has to be created. Can Spring Boot Actuator provide a Flux of the metrics so I can stream it?
Here is some code, I currently call this method via a scheduled method that pings it every 500 ms.
private Flux<Health> getHealth(String baseUrl) {
var fallbackValue = new Health();
fallbackValue.setStatus("Unknown");
return webClient.get().uri(baseUrl + "/actuator/health").retrieve().bodyToFlux(Health.class)
.onErrorReturn(fallbackValue);
}
But it only updates if I call it and use the scheduled annotation to refresh it always:
@Scheduled(fixedDelay = 500) // I want to remove this, instead of refreshing, I want the data to stream
public void pollCapHandlers() {
getHealth("http://localhost:8080").subscribe(health -> {
ui.access(() -> {
healthStatusTextField.setValue(health.getStatus()); // this doesn't update in real time
});
})