@Configuration
@EnableMBeanExport(defaultDomain = "myapp")
public class JmxConfig {}@Service
@ManagedResource(
objectName = "myapp:type=Service,name=UserService",
description = "User management service"
)
public class UserService {
private int maxConnections = 100;
private AtomicLong requestCount = new AtomicLong(0);
@ManagedAttribute(description = "Maximum connections")
public int getMaxConnections() {
return maxConnections;
}
@ManagedAttribute
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
@ManagedMetric(
category = "throughput",
displayName = "Total Requests",
metricType = "COUNTER",
unit = "requests"
)
public long getTotalRequests() {
return requestCount.get();
}
@ManagedOperation(description = "Reset statistics")
public void resetStats() {
requestCount.set(0);
}
@ManagedOperation(description = "Process batch")
@ManagedOperationParameters({
@ManagedOperationParameter(name = "batchId", description = "Batch identifier"),
@ManagedOperationParameter(name = "priority", description = "Processing priority")
})
public void processBatch(String batchId, int priority) {
// Process batch
}
}@Service
@ManagedResource(objectName = "myapp:type=AlertService")
@ManagedNotifications({
@ManagedNotification(
name = "System Alert",
description = "Critical system event",
notificationTypes = "myapp.alert"
)
})
public class AlertService implements NotificationPublisherAware {
private NotificationPublisher publisher;
@Override
public void setNotificationPublisher(NotificationPublisher publisher) {
this.publisher = publisher;
}
@ManagedOperation(description = "Send alert")
public void sendAlert(String message, String severity) {
Notification notification = new Notification(
"myapp.alert",
this,
System.currentTimeMillis(),
message
);
notification.setUserData(Map.of("severity", severity));
publisher.sendNotification(notification);
}
}@Configuration
public class JmxConfig {
@Bean
public AnnotationMBeanExporter mbeanExporter() {
AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
exporter.setDefaultDomain("myapp");
exporter.setEnsureUniqueRuntimeObjectNames(true);
return exporter;
}
}@Service
public class JmxRegistrationService {
@Autowired
private MBeanServer mBeanServer;
public void registerMBean() throws Exception {
ObjectName name = new ObjectName("myapp:type=Custom,name=MyBean");
MyMBean mbean = new MyMBean();
mBeanServer.registerMBean(mbean, name);
}
public void unregisterMBean() throws Exception {
ObjectName name = new ObjectName("myapp:type=Custom,name=MyBean");
mBeanServer.unregisterMBean(name);
}
}