This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit 59756f4fe3d9cf84a68cb386dff80c7a85625c53 Author: Chetan Mehrotra <[email protected]> AuthorDate: Wed Jan 13 04:58:38 2016 +0000 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 git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1724354 13f79535-47bb-0310-9956-ffa450edef68 --- .../metrics/internal/MetricsServiceImpl.java | 29 ++++++++++++++------- .../metrics/internal/MetricServiceTest.java | 30 ++++++++++++++++++++-- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java index a0e0520..d2f2728 100644 --- a/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java +++ b/src/main/java/org/apache/sling/commons/metrics/internal/MetricsServiceImpl.java @@ -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.Activate; 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 implements MetricsService { 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 implements MetricsService { 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(); + } } diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java index a028abf..5885426 100644 --- a/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java +++ b/src/test/java/org/apache/sling/commons/metrics/internal/MetricServiceTest.java @@ -21,9 +21,14 @@ package org.apache.sling.commons.metrics.internal; 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); } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
