1. Introduction
Recently I had the following situation: my application should use already available actuator information, do something with it, and return the result in the end. So, the first thing that was on my mind was to call the actuator REST endpoint from the same service that exposes those actuator endpoints. After short research, it turned out that there is an actuator bean called HealthEndpoint that would provide us with readiness and liveness data from within the application code.
2. Solution
Let's first check if we have actuator dependency in our pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.4.0</version>
</dependency>
Let's add HealthEndpoint bean to our service:
@Autowired
private HealthEndpoint healthEndpoint;
To access readiness and liveness endpoint information, we should invoke healthForPath method and pass part of the health check URL we would like to access.
HealthComponent readinessHealthComponent = healthEndpoint.healthForPath("readiness");
HealthComponent livenessHealthComponent = healthEndpoint.healthForPath("liveness");
3. Conclusion
In this short tutorial, we discussed the usage of actuator features from within the code of service that exposes actuator routes. More details on specific usages and configurations of actuators could be found here.