You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2016/02/18 17:22:09 UTC
[2/2] camel git commit: CAMEL-9616: Support looking up MetricRegistry
by type only in the Metrics component
CAMEL-9616: Support looking up MetricRegistry by type only in the Metrics component
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/62b080e5
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/62b080e5
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/62b080e5
Branch: refs/heads/master
Commit: 62b080e5986efc028cd30bacd5534fa96ad8b8ad
Parents: 78e9111
Author: Antonin Stefanutti <an...@stefanutti.fr>
Authored: Thu Feb 18 15:57:07 2016 +0100
Committer: Andrea Cosentino <an...@gmail.com>
Committed: Thu Feb 18 17:20:45 2016 +0100
----------------------------------------------------------------------
.../camel-metrics/src/main/docs/metrics.adoc | 41 ++++++++++++++++++--
.../component/metrics/MetricsComponent.java | 12 +++++-
.../component/metrics/MetricsComponentTest.java | 14 +++++++
3 files changed, 62 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/62b080e5/components/camel-metrics/src/main/docs/metrics.adoc
----------------------------------------------------------------------
diff --git a/components/camel-metrics/src/main/docs/metrics.adoc b/components/camel-metrics/src/main/docs/metrics.adoc
index 801b0bf..7517d3e 100644
--- a/components/camel-metrics/src/main/docs/metrics.adoc
+++ b/components/camel-metrics/src/main/docs/metrics.adoc
@@ -40,10 +40,13 @@ metrics:[ meter | counter | histogram | timer ]:metricname[?options]
[[MetricsComponent-registry]]Metric Registry
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-Camel Metrics component uses by default `MetricRegistry` with
-`Slf4jReporter` and 60 second reporting interval. Default registry can
-be replaced with custom one by providing bean with name `metricRegistry`
-in Camel registry. For example using Spring Java Configuration.
+Camel Metrics component uses by default a `MetricRegistry` instance with
+a `Slf4jReporter` that has a 60 second reporting interval.
+This default registry can be replaced with a custom one by providing
+a `MetricRegistry` bean. If multiple `MetricRegistry` beans exist in the
+application, the one with name `metricRegistry` is used.
+
+For example using Spring Java Configuration:
[source,java]
----
@@ -69,6 +72,29 @@ public static class MyConfig extends SingleRouteCamelConfiguration {
}
----
+Or using CDI:
+[source,java]
+----
+class MyBean extends RouteBuilder {
+
+ @Override
+ public void configure() {
+ from("...")
+ // Register the 'my-meter' meter in the MetricRegistry below
+ .to("metrics:meter:my-meter");
+ }
+
+ @Produces
+ // If multiple MetricRegistry beans
+ // @Named(MetricsComponent.METRIC_REGISTRY_NAME)
+ MetricRegistry registry() {
+ MetricRegistry registry = new MetricRegistry();
+ // ...
+ return registry;
+ }
+}
+----
+
CAUTION: `MetricRegistry` uses internal thread(s) for reporting. There is no
public API in version `3.0.1` for users to clean up on exit. Thus using
Camel Metrics Component leads to Java classloader leak and my cause
@@ -526,3 +552,10 @@ String json = service.dumpStatisticsAsJson();
And the JMX API the MBean is registered in the `type=services` tree
with `name=MetricsMessageHistoryService`.
+
+[[MetricsComponent-SeeAlso]]
+See Also
+^^^^^^^^
+
+* The `camel-example-cdi-metrics` example that illustrates the integration
+ between Camel, Metrics and CDI.
http://git-wip-us.apache.org/repos/asf/camel/blob/62b080e5/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java
index 71ea228..5e544ac 100644
--- a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java
+++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/MetricsComponent.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.metrics;
import java.util.Map;
+import java.util.Set;
import java.util.concurrent.TimeUnit;
import com.codahale.metrics.MetricRegistry;
@@ -92,7 +93,16 @@ public class MetricsComponent extends UriEndpointComponent {
}
MetricRegistry getMetricRegistryFromCamelRegistry(Registry camelRegistry, String registryName) {
- return camelRegistry.lookupByNameAndType(registryName, MetricRegistry.class);
+ MetricRegistry registry = camelRegistry.lookupByNameAndType(registryName, MetricRegistry.class);
+ if (registry != null) {
+ return registry;
+ } else {
+ Set<MetricRegistry> registries = camelRegistry.findByType(MetricRegistry.class);
+ if (registries.size() == 1) {
+ return registries.iterator().next();
+ }
+ }
+ return null;
}
MetricRegistry createMetricRegistry() {
http://git-wip-us.apache.org/repos/asf/camel/blob/62b080e5/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java
----------------------------------------------------------------------
diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java
index 6f6b431..569933f 100644
--- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java
+++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.metrics;
+import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
@@ -189,12 +190,25 @@ public class MetricsComponentTest {
}
@Test
+ public void testGetOrCreateMetricRegistryFoundInCamelRegistryByType() throws Exception {
+ when(camelRegistry.lookupByNameAndType("name", MetricRegistry.class)).thenReturn(null);
+ when(camelRegistry.findByType(MetricRegistry.class)).thenReturn(Collections.singleton(metricRegistry));
+ MetricRegistry result = component.getOrCreateMetricRegistry(camelRegistry, "name");
+ assertThat(result, is(metricRegistry));
+ inOrder.verify(camelRegistry, times(1)).lookupByNameAndType("name", MetricRegistry.class);
+ inOrder.verify(camelRegistry, times(1)).findByType(MetricRegistry.class);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ @Test
public void testGetOrCreateMetricRegistryNotFoundInCamelRegistry() throws Exception {
when(camelRegistry.lookupByNameAndType("name", MetricRegistry.class)).thenReturn(null);
+ when(camelRegistry.findByType(MetricRegistry.class)).thenReturn(Collections.<MetricRegistry>emptySet());
MetricRegistry result = component.getOrCreateMetricRegistry(camelRegistry, "name");
assertThat(result, is(notNullValue()));
assertThat(result, is(not(metricRegistry)));
inOrder.verify(camelRegistry, times(1)).lookupByNameAndType("name", MetricRegistry.class);
+ inOrder.verify(camelRegistry, times(1)).findByType(MetricRegistry.class);
inOrder.verifyNoMoreInteractions();
}