sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chet...@apache.org
Subject svn commit: r1724354 - in /sling/trunk/bundles/commons/metrics/src: main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
Date Wed, 13 Jan 2016 04:58:38 GMT
Author: chetanm
Date: Wed Jan 13 04:58:38 2016
New Revision: 1724354

URL: http://svn.apache.org/viewvc?rev=1724354&view=rev
Log:
SLING-5424 - MBeanServer reference in MetricServiceImpl should be made optional

-- Added test to check for MBean registration - Due to missing start call on JMXReporter
    Mbeans were not getting registered
-- Made reference optional - If not set default PlatformMBeanServer is used

Modified:
    sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
    sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java

Modified: sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java?rev=1724354&r1=1724353&r2=1724354&view=diff
==============================================================================
--- sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
(original)
+++ sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java
Wed Jan 13 04:58:38 2016
@@ -20,6 +20,7 @@
 package org.apache.sling.commons.metrics.internal;
 
 import java.io.IOException;
+import java.lang.management.ManagementFactory;
 import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.Hashtable;
@@ -36,6 +37,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.sling.commons.metrics.Meter;
 import org.apache.sling.commons.metrics.MetricsService;
 import org.apache.sling.commons.metrics.Timer;
@@ -52,21 +54,14 @@ public class MetricsServiceImpl implemen
     private final ConcurrentMap<String, Metric> metrics = new ConcurrentHashMap<String,
Metric>();
     private final MetricRegistry registry = new MetricRegistry();
 
-    @Reference
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
     private MBeanServer server;
 
     private JmxReporter reporter;
 
     @Activate
     private void activate(BundleContext context, Map<String, Object> config) {
-        //TODO Domain name should be based on calling bundle
-        //For that we can register ServiceFactory and make use of calling
-        //bundle symbolic name to determine the mapping
-
-        reporter = JmxReporter.forRegistry(registry)
-                .inDomain("org.apache.sling")
-                .registerWith(server)
-                .build();
+        enableJMXReporter();
 
         final Dictionary<String, String> svcProps = new Hashtable<String, String>();
         svcProps.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Metrics Service");
@@ -205,4 +200,20 @@ public class MetricsServiceImpl implemen
 
         boolean isInstance(Metric metric);
     }
+
+    private void enableJMXReporter() {
+        //TODO Domain name should be based on calling bundle
+        //For that we can register ServiceFactory and make use of calling
+        //bundle symbolic name to determine the mapping
+
+        if (server == null){
+            server = ManagementFactory.getPlatformMBeanServer();
+        }
+
+        reporter = JmxReporter.forRegistry(registry)
+                .inDomain("org.apache.sling")
+                .registerWith(server)
+                .build();
+        reporter.start();
+    }
 }

Modified: sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java?rev=1724354&r1=1724353&r2=1724354&view=diff
==============================================================================
--- sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
(original)
+++ sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java
Wed Jan 13 04:58:38 2016
@@ -21,9 +21,14 @@ package org.apache.sling.commons.metrics
 
 import java.lang.management.ManagementFactory;
 import java.util.Collections;
+import java.util.Set;
 
 import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.Query;
+import javax.management.QueryExp;
 
+import com.codahale.metrics.JmxReporter;
 import com.codahale.metrics.MetricRegistry;
 import org.apache.sling.commons.metrics.Counter;
 import org.apache.sling.commons.metrics.Histogram;
@@ -36,9 +41,13 @@ import org.junit.After;
 import org.junit.Rule;
 import org.junit.Test;
 
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 public class MetricServiceTest {
@@ -48,8 +57,8 @@ public class MetricServiceTest {
     private MetricsServiceImpl service = new MetricsServiceImpl();
 
     @After
-    public void registerMBeanServer() {
-        context.registerService(MBeanServer.class, ManagementFactory.getPlatformMBeanServer());
+    public void deactivate(){
+        MockOsgi.deactivate(service);
     }
 
     @Test
@@ -118,6 +127,23 @@ public class MetricServiceTest {
         service.timer("test");
     }
 
+    @Test
+    public void jmxRegistration() throws Exception{
+        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+        activate();
+        Meter meter = service.meter("test");
+        assertNotNull(meter);
+        QueryExp q = Query.isInstanceOf(Query.value(JmxReporter.JmxMeterMBean.class.getName()));
+        Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.sling:name=*"),
q);
+        assertThat(names, is(not(empty())));
+
+        MockOsgi.deactivate(service);
+
+        names = server.queryNames(new ObjectName("org.apache.sling:name=*"), q);
+        assertThat(names, is(empty()));
+
+    }
+
     private MetricRegistry getRegistry(){
         return context.getService(MetricRegistry.class);
     }



Mime
View raw message