camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject camel git commit: CAMEL-8014 Let route policy and metrics component reuse same metricsregistry
Date Tue, 18 Nov 2014 02:57:48 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.14.x e8ed23455 -> b5526911e


CAMEL-8014 Let route policy and metrics component reuse same metricsregistry


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b5526911
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b5526911
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b5526911

Branch: refs/heads/camel-2.14.x
Commit: b5526911e0755cd1326f4058a53ced7accc87026
Parents: e8ed234
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Tue Nov 18 10:39:28 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Tue Nov 18 10:57:35 2014 +0800

----------------------------------------------------------------------
 .../routepolicy/MetricsRegistryService.java     | 14 +++++++++-
 .../ManagedMetricsRoutePolicyTest.java          | 27 ++++++++++++++------
 2 files changed, 32 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b5526911/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/routepolicy/MetricsRegistryService.java
----------------------------------------------------------------------
diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/routepolicy/MetricsRegistryService.java
b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/routepolicy/MetricsRegistryService.java
index 12c6ceb..6f87a3d 100644
--- a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/routepolicy/MetricsRegistryService.java
+++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/routepolicy/MetricsRegistryService.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.metrics.routepolicy;
 
 import java.util.concurrent.TimeUnit;
+
 import javax.management.MBeanServer;
 
 import com.codahale.metrics.JmxReporter;
@@ -29,7 +30,9 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.StaticService;
 import org.apache.camel.api.management.ManagedResource;
+import org.apache.camel.component.metrics.MetricsComponent;
 import org.apache.camel.spi.ManagementAgent;
+import org.apache.camel.spi.Registry;
 import org.apache.camel.support.ServiceSupport;
 import org.apache.camel.util.ObjectHelper;
 
@@ -105,11 +108,18 @@ public final class MetricsRegistryService extends ServiceSupport implements
Came
     public void setDurationUnit(TimeUnit durationUnit) {
         this.durationUnit = durationUnit;
     }
+    
+    
 
     @Override
     protected void doStart() throws Exception {
         if (metricsRegistry == null) {
-            metricsRegistry = new MetricRegistry();
+            Registry camelRegistry = getCamelContext().getRegistry();
+            metricsRegistry = camelRegistry.lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME,
MetricRegistry.class);
+            // create a new metricsRegistry by default
+            if (metricsRegistry == null) {
+                metricsRegistry = new MetricRegistry();
+            }
         }
 
         if (useJmx) {
@@ -167,4 +177,6 @@ public final class MetricsRegistryService extends ServiceSupport implements
Came
             throw ObjectHelper.wrapRuntimeCamelException(e);
         }
     }
+    
+   
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b5526911/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/ManagedMetricsRoutePolicyTest.java
----------------------------------------------------------------------
diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/ManagedMetricsRoutePolicyTest.java
b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/ManagedMetricsRoutePolicyTest.java
index 03c5c8f..d5c6114 100644
--- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/ManagedMetricsRoutePolicyTest.java
+++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/routepolicy/ManagedMetricsRoutePolicyTest.java
@@ -17,18 +17,21 @@
 package org.apache.camel.component.metrics.routepolicy;
 
 import java.util.Set;
+
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
 import com.codahale.metrics.MetricRegistry;
 import org.apache.camel.CamelContext;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.metrics.MetricsComponent;
+import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
 
 public class ManagedMetricsRoutePolicyTest extends CamelTestSupport {
 
-    private MetricRegistry registry = new MetricRegistry();
+    private MetricRegistry metricRegistry = new MetricRegistry();
 
     @Override
     protected boolean useJmx() {
@@ -38,6 +41,14 @@ public class ManagedMetricsRoutePolicyTest extends CamelTestSupport {
     protected MBeanServer getMBeanServer() {
         return context.getManagementStrategy().getManagementAgent().getMBeanServer();
     }
+    
+    // Setup the common MetricsRegistry for MetricsComponent and MetricsRoutePolicy to use
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry registry = super.createRegistry();
+        registry.bind(MetricsComponent.METRIC_REGISTRY_NAME, metricRegistry);
+        return registry;
+    }
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
@@ -45,7 +56,6 @@ public class ManagedMetricsRoutePolicyTest extends CamelTestSupport {
 
         MetricsRoutePolicyFactory factory = new MetricsRoutePolicyFactory();
         factory.setUseJmx(true);
-        factory.setMetricsRegistry(registry);
         factory.setPrettyPrint(true);
         context.addRoutePolicyFactory(factory);
 
@@ -66,19 +76,20 @@ public class ManagedMetricsRoutePolicyTest extends CamelTestSupport {
 
         assertMockEndpointsSatisfied();
 
-        // there should be 2 names
-        assertEquals(2, registry.getNames().size());
+        // there should be 3 names
+        assertEquals(3, metricRegistry.getNames().size());
 
-        // there should be 2 mbeans
+        // there should be 3 mbeans
         Set<ObjectName> set = getMBeanServer().queryNames(new ObjectName("org.apache.camel.metrics:*"),
null);
-        assertEquals(2, set.size());
+        assertEquals(3, set.size());
 
         String name = String.format("org.apache.camel:context=%s,type=services,name=MetricsRegistryService",
context.getManagementName());
         ObjectName on = ObjectName.getInstance(name);
         String json = (String) getMBeanServer().invoke(on, "dumpStatisticsAsJson", null,
null);
         assertNotNull(json);
         log.info(json);
-
+        
+        assertTrue(json.contains("test"));
         assertTrue(json.contains("bar.responses"));
         assertTrue(json.contains("foo.responses"));
     }
@@ -88,7 +99,7 @@ public class ManagedMetricsRoutePolicyTest extends CamelTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("seda:foo").routeId("foo")
+                from("seda:foo").routeId("foo").to("metrics:counter:test")
                     .to("mock:result");
 
                 from("seda:bar").routeId("bar")


Mime
View raw message