joerghoh commented on code in PR #8:
URL:
https://github.com/apache/sling-org-apache-sling-commons-metrics/pull/8#discussion_r1723479870
##########
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);
Review Comment:
```objectname``` is still ```null``` here. You rather need
```suggestion
LOG.debug("JMX Notification : Cannot handle
notification, because it's not a MBeanServerNotification ({})", notification);
```
##########
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:
I think a comment here would be useful as well, because you (try to)
register every MBean here, while at the same time the same MBean can be added
by the listener as well. Some clarification (at least that it's safe and does
not result in double registrations or something like that) would be useful.
##########
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() {
Review Comment:
some comment would be helpful
--
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]