Master Spring Boot Actuator: HTTP & JMX Monitoring, Custom Endpoints, and JMX MBean Registration
Learn how to enable and use Spring Boot Actuator's monitoring features—including HTTP and JMX endpoints—configure built‑in endpoints, expose custom metrics, dynamically adjust log levels, and manually register JMX MBeans, with code examples and integration tips for Prometheus and Grafana.
Any service without monitoring is blind to its own runtime state, making fault handling difficult; therefore monitoring is essential for every service.
Most micro‑service applications are built with Spring Boot, so understanding Spring Boot's monitoring capabilities is crucial. This article demonstrates monitoring using Spring Boot 2.3.1.RELEASE.
HTTP Endpoints Monitoring
Actuator endpoints allow you to observe and interact with the application. Spring Boot provides many built‑in endpoints and lets you add custom ones. Each endpoint has a unique ID and can be accessed via HTTP or JMX.
In Spring Boot 1.x the URL pattern is http://<i>ip</i>:<i>port</i>/{id}. In Spring Boot 2.x a base path /actuator is added, so the pattern becomes http://<i>ip</i>:<i>port</i>/actuator/{id}.
To enable HTTP monitoring, add the actuator starter dependency:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>After starting the application, http://localhost:8080/actuator/health returns a JSON health status.
Only health and info are exposed by default. To expose additional endpoints, configure:
management:
endpoints:
web:
exposure:
include: [health,info,mappings] # or "*" to expose allYou can also enable or disable a specific endpoint dynamically:
management.endpoint.<id>.enabled=trueKey Built‑in Endpoints
health : Shows application health; can be configured to display details (e.g., Redis status) with management.endpoint.health.show-details=always.
loggers : Displays current log levels; you can change a logger’s level at runtime via a POST request.
metrics : Provides JVM, memory, thread, and Tomcat metrics; each metric can be queried individually.
Custom Monitoring Endpoints
Spring Boot allows you to create custom endpoints using annotations: @Endpoint: Defines a monitoring endpoint supporting both HTTP and JMX. @WebEndpoint: Defines an endpoint supporting only HTTP. @JmxEndpoint: Defines an endpoint supporting only JMX.
Method‑level annotations: @ReadOperation: Exposes a GET‑style operation. @WriteOperation: Exposes a POST‑style operation. @DeleteOperation: Exposes a DELETE‑style operation. @Selector: Binds a method parameter to a sub‑path element.
Example custom endpoint:
@Endpoint(id="myEndpoint")
@Component
public class MyEndpoint {
private String STATUS = "up";
private String DETAIL = "All good";
@ReadOperation
public JSONObject test3() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("status", STATUS);
jsonObject.put("detail", DETAIL);
return jsonObject;
}
@ReadOperation
public JSONObject test3_1(@Selector String name) {
JSONObject jsonObject = new JSONObject();
if ("status".equals(name)) {
jsonObject.put("status", STATUS);
} else if ("detail".equals(name)) {
jsonObject.put("detail", DETAIL);
}
return jsonObject;
}
@WriteOperation
public void test4(@Selector String name, @Nullable String value) {
if (!StringUtils.isEmpty(value)) {
if ("status".equals(name)) {
STATUS = value;
} else if ("detail".equals(name)) {
DETAIL = value;
}
}
}
}After starting the application, the custom endpoint is reachable at http://localhost:8080/actuator/myEndpoint. The @Selector allows querying sub‑metrics, and @WriteOperation enables dynamic updates.
JMX Monitoring
JMX (Java Management Extensions) provides monitoring of JVM resources such as threads, memory, and CPU. Use the JDK tool jConsole to connect to a running Spring Boot application and view registered MBeans.
To register a custom MBean manually:
public interface SystemInfoMBean {
int getCpuCore();
long getTotalMemory();
void shutdown();
} public class SystemInfo implements SystemInfoMBean {
@Override
public int getCpuCore() {
return Runtime.getRuntime().availableProcessors();
}
@Override
public long getTotalMemory() {
return Runtime.getRuntime().totalMemory();
}
@Override
public void shutdown() {
System.exit(0);
}
} public class JmxRegisterMain {
public static void main(String[] args) throws Exception {
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("com.lonely.wolf.note.springboot.actuator.jmx:type=SystemInfo");
SystemInfo systemInfo = new SystemInfo();
mBeanServer.registerMBean(systemInfo, objectName);
System.in.read(); // keep the program running
}
}Running this main class registers the MBean, which becomes visible in jConsole. Spring Boot automatically registers beans annotated with @Endpoint or @JmxEndpoint as MBeans.
Integration with Prometheus
Spring Boot can export metrics to Prometheus via Micrometer. Add the following dependency:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>Prometheus (often paired with Grafana) can then scrape the /actuator/prometheus endpoint.
Summary
This article covered the use of Spring Boot Actuator, detailing HTTP and JMX monitoring types, showing how to configure built‑in endpoints, create custom endpoints, and manually register a JMX MBean, as well as briefly introducing Prometheus integration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
