So according to the documents of Spring it will publish metrics on a REST endpoint and a message channel.
The REST endpoint works fine as I get the expected result. However I would like to handle each change in the metrics. So it says it will by default publish messages to a channel called "metricsChannel"
I tried to create the following class which would listen to this channel, but it does not seem to fire. Everything else has been kept default for the Spring Boot application.
package services.core;
import org.springframework.stereotype.Service;
import org.springframework.integration.annotation.ServiceActivator;
@Service
public class MetricService {
@ServiceActivator(inputChannel = "metricsChannel")
public void handleMessage(org.springframework.messaging.Message<?> message) {
System.out.println("Message [" + message.toString() + "] is received");
}
}
To clarify: my code did actually work, but for me it felt like a gotcha.
Quote from Spring docs:
So by "all metric update events", I thought that the system metrics (memory usage, cpu load etc.) would fall under these events. Actually they are not, they are just being published whenever your custom counters change or for example the number of requests for a certain endpoint.
Initially I was waiting for a message each second or so after bootup, to no avail. Ultimately started to call the metrics endpoint and suddenly the messages starting popping in the console/channel each time I called it.
I've just tested that and works well:
I've done that in our
web-sockets
sample on theserver
part. Added this:to that project Gradle config.
And I see this in console, when I started the
client
app:But, yes: your code is good as well.