infoscale commented on code in PR #8:
URL: 
https://github.com/apache/sling-org-apache-sling-commons-metrics/pull/8#discussion_r1726940549


##########
src/main/java/org/apache/sling/commons/metrics/internal/JmxExporterFactory.java:
##########
@@ -78,14 +85,46 @@ public class JmxExporterFactory {
     MetricsService metrics;
     
     MBeanServer server;
-    
+
+    NotificationListener listener = new NotificationListener() {
+        public void handleNotification(Notification notification, Object 
handback) {
+            if 
(MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(notification.getType()))
 {
+                ObjectName objectname = null;
+                try {
+                    if(notification instanceof MBeanServerNotification) {
+                        MBeanServerNotification mbeanNotification = 
(MBeanServerNotification) notification;
+                        objectname = mbeanNotification.getMBeanName();
+                        LOG.debug("JMX Notification : match {} with pattern = 
{}", objectname, Arrays.asList(patterns));
+                        for (String pattern : patterns) {
+                            if (objectname.toString().matches(pattern)) {
+                                LOG.debug("JMX Notification : register metrics 
for MBean: {}", objectname);
+                                registerMBeanProperties(objectname);
+                                break;
+                            }
+                        }
+                    } else {
+                        LOG.debug("JMX Notification : Cannot register metrics 
for objectname = {}", objectname);
+                    }
+                } catch (InstanceNotFoundException | ReflectionException | 
IntrospectionException e) {
+                    LOG.error("JMX Notification : Cannot register metrics for 
objectname = {}", objectname, e);
+                }
+            }
+        }
+    };
+
     @Activate
     @Modified
     public void activate(Config config) {
         server = ManagementFactory.getPlatformMBeanServer();
-        registerMetrics(config.objectnames());
+        patterns = config.objectnames();
+        try {
+            server.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, 
listener, null, null);
+        } catch (InstanceNotFoundException e) {
+            LOG.error("Cannot add notification listener to 
MBeanServerDelegate", e);
+        }
+        registerMetrics(patterns);

Review Comment:
   Added javadoc comments to explain the behaviour



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to