CAMEL-9389: camel-metrics - Allow to capture message history easily
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0b00fda9 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0b00fda9 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0b00fda9 Branch: refs/heads/master Commit: 0b00fda909700e4f2665e3819eae93ad4c291b73 Parents: 18915b4 Author: Claus Ibsen <[email protected]> Authored: Fri Dec 4 13:02:37 2015 +0000 Committer: Claus Ibsen <[email protected]> Committed: Fri Dec 4 13:02:37 2015 +0000 ---------------------------------------------------------------------- .../messagehistory/MetricsMessageHistory.java | 66 +++++++ .../MetricsMessageHistoryFactory.java | 166 +++++++++++++++++ .../MetricsMessageHistoryMBean.java | 29 +++ .../MetricsMessageHistoryService.java | 179 +++++++++++++++++++ .../routepolicy/MetricsRegistryService.java | 5 +- .../metrics/routepolicy/MetricsRoutePolicy.java | 1 - .../ManagedMessageHistoryTest.java | 113 ++++++++++++ .../MetricsMessageHistoryTest.java | 75 ++++++++ 8 files changed, 629 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0b00fda9/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistory.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistory.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistory.java new file mode 100644 index 0000000..dc290f4 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistory.java @@ -0,0 +1,66 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics.messagehistory; + +import java.util.Date; + +import com.codahale.metrics.Timer; +import org.apache.camel.MessageHistory; +import org.apache.camel.NamedNode; + +/** + * A codahale metrics based {@link MessageHistory} + */ +public class MetricsMessageHistory implements MessageHistory { + + private final String routeId; + private final NamedNode namedNode; + private final Timer timer; + private final Timer.Context context; + + public MetricsMessageHistory(String routeId, NamedNode namedNode, Timer timer) { + this.routeId = routeId; + this.namedNode = namedNode; + this.timer = timer; + this.context = timer.time(); + } + + @Override + public String getRouteId() { + return routeId; + } + + @Override + public NamedNode getNode() { + return namedNode; + } + + @Override + public Date getTimestamp() { + return null; + } + + @Override + public long getElapsed() { + return timer.getCount(); + } + + @Override + public void nodeProcessingDone() { + context.stop(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0b00fda9/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryFactory.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryFactory.java new file mode 100644 index 0000000..39cf85e --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryFactory.java @@ -0,0 +1,166 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics.messagehistory; + +import java.util.Date; +import java.util.concurrent.TimeUnit; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.MessageHistory; +import org.apache.camel.NamedNode; +import org.apache.camel.StaticService; +import org.apache.camel.spi.MessageHistoryFactory; +import org.apache.camel.support.ServiceSupport; +import org.apache.camel.util.ObjectHelper; + +/** + * A factory to setup and use {@link MetricsMessageHistory} as message history implementation. + */ +public class MetricsMessageHistoryFactory extends ServiceSupport implements CamelContextAware, StaticService, MessageHistoryFactory { + + private CamelContext camelContext; + private MetricsMessageHistoryService messageHistoryService; + private MetricRegistry metricsRegistry; + private boolean useJmx; + private String jmxDomain = "org.apache.camel.metrics"; + private boolean prettyPrint; + private TimeUnit rateUnit = TimeUnit.SECONDS; + private TimeUnit durationUnit = TimeUnit.MILLISECONDS; + private String namePattern = "##name##.##routeId##.##id##.##type##"; + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + public MetricRegistry getMetricsRegistry() { + return metricsRegistry; + } + + /** + * To use a specific {@link com.codahale.metrics.MetricRegistry} instance. + * <p/> + * If no instance has been configured, then Camel will create a shared instance to be used. + */ + public void setMetricsRegistry(MetricRegistry metricsRegistry) { + this.metricsRegistry = metricsRegistry; + } + + public boolean isUseJmx() { + return useJmx; + } + + /** + * Whether to use JMX reported to enlist JMX MBeans with the metrics statistics. + */ + public void setUseJmx(boolean useJmx) { + this.useJmx = useJmx; + } + + public String getJmxDomain() { + return jmxDomain; + } + + /** + * The JMX domain name to use for the enlisted JMX MBeans. + */ + public void setJmxDomain(String jmxDomain) { + this.jmxDomain = jmxDomain; + } + + public boolean isPrettyPrint() { + return prettyPrint; + } + + /** + * Whether to use pretty print when outputting JSon + */ + public void setPrettyPrint(boolean prettyPrint) { + this.prettyPrint = prettyPrint; + } + + public TimeUnit getRateUnit() { + return rateUnit; + } + + /** + * Sets the time unit to use for requests per unit (eg requests per second) + */ + public void setRateUnit(TimeUnit rateUnit) { + this.rateUnit = rateUnit; + } + + public TimeUnit getDurationUnit() { + return durationUnit; + } + + /** + * Sets the time unit to use for timing the duration of processing a message in the route + */ + public void setDurationUnit(TimeUnit durationUnit) { + this.durationUnit = durationUnit; + } + + @Override + public MessageHistory newMessageHistory(String routeId, NamedNode namedNode, Date date) { + Timer timer = metricsRegistry.timer(createName("history", routeId, namedNode.getId())); + return new MetricsMessageHistory(routeId, namedNode, timer); + } + + private String createName(String type, String routeId, String id) { + String name = camelContext.getManagementName() != null ? camelContext.getManagementName() : camelContext.getName(); + + String answer = namePattern; + answer = answer.replaceFirst("##name##", name); + answer = answer.replaceFirst("##routeId##", routeId); + answer = answer.replaceFirst("##id##", id); + answer = answer.replaceFirst("##type##", type); + return answer; + } + + @Override + protected void doStart() throws Exception { + try { + messageHistoryService = camelContext.hasService(MetricsMessageHistoryService.class); + if (messageHistoryService == null) { + messageHistoryService = new MetricsMessageHistoryService(); + messageHistoryService.setMetricsRegistry(getMetricsRegistry()); + messageHistoryService.setUseJmx(isUseJmx()); + messageHistoryService.setJmxDomain(getJmxDomain()); + messageHistoryService.setPrettyPrint(isPrettyPrint()); + messageHistoryService.setRateUnit(getRateUnit()); + messageHistoryService.setDurationUnit(getDurationUnit()); + camelContext.addService(messageHistoryService); + } + } catch (Exception e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } + } + + @Override + protected void doStop() throws Exception { + // noop + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0b00fda9/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryMBean.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryMBean.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryMBean.java new file mode 100644 index 0000000..05ae2f6 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryMBean.java @@ -0,0 +1,29 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics.messagehistory; + +import org.apache.camel.api.management.ManagedOperation; + +public interface MetricsMessageHistoryMBean { + + @ManagedOperation(description = "Dumps the statistics as json") + String dumpStatisticsAsJson(); + + @ManagedOperation(description = "Dumps the statistics as json using seconds for time units") + String dumpStatisticsAsJsonTimeUnitSeconds(); + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0b00fda9/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryService.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryService.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryService.java new file mode 100644 index 0000000..76aaef4 --- /dev/null +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryService.java @@ -0,0 +1,179 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics.messagehistory; + +import java.util.concurrent.TimeUnit; +import javax.management.MBeanServer; + +import com.codahale.metrics.JmxReporter; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.json.MetricsModule; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +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.component.metrics.routepolicy.MetricsRegistryMBean; +import org.apache.camel.spi.ManagementAgent; +import org.apache.camel.spi.Registry; +import org.apache.camel.support.ServiceSupport; +import org.apache.camel.util.ObjectHelper; + +/** + * Service holding the {@link MetricsMessageHistory} which registers all message history metrics. + */ +@ManagedResource(description = "MetricsMessageHistory") +public final class MetricsMessageHistoryService extends ServiceSupport implements CamelContextAware, StaticService, MetricsRegistryMBean { + + private CamelContext camelContext; + private MetricRegistry metricsRegistry; + private JmxReporter reporter; + private boolean useJmx; + private String jmxDomain = "org.apache.camel.metrics"; + private boolean prettyPrint; + private TimeUnit rateUnit = TimeUnit.SECONDS; + private TimeUnit durationUnit = TimeUnit.MILLISECONDS; + private transient ObjectMapper mapper; + private transient ObjectMapper secondsMapper; + + public MetricRegistry getMetricsRegistry() { + return metricsRegistry; + } + + public void setMetricsRegistry(MetricRegistry metricsRegistry) { + this.metricsRegistry = metricsRegistry; + } + + public CamelContext getCamelContext() { + return camelContext; + } + + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + public boolean isUseJmx() { + return useJmx; + } + + public void setUseJmx(boolean useJmx) { + this.useJmx = useJmx; + } + + public String getJmxDomain() { + return jmxDomain; + } + + public void setJmxDomain(String jmxDomain) { + this.jmxDomain = jmxDomain; + } + + public boolean isPrettyPrint() { + return prettyPrint; + } + + public void setPrettyPrint(boolean prettyPrint) { + this.prettyPrint = prettyPrint; + } + + public TimeUnit getRateUnit() { + return rateUnit; + } + + public void setRateUnit(TimeUnit rateUnit) { + this.rateUnit = rateUnit; + } + + public TimeUnit getDurationUnit() { + return durationUnit; + } + + public void setDurationUnit(TimeUnit durationUnit) { + this.durationUnit = durationUnit; + } + + @Override + protected void doStart() throws Exception { + if (metricsRegistry == null) { + 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) { + ManagementAgent agent = getCamelContext().getManagementStrategy().getManagementAgent(); + if (agent != null) { + MBeanServer server = agent.getMBeanServer(); + if (server != null) { + reporter = JmxReporter.forRegistry(metricsRegistry).registerWith(server).inDomain(jmxDomain).build(); + reporter.start(); + } + } else { + throw new IllegalStateException("CamelContext has not enabled JMX"); + } + } + + // json mapper + this.mapper = new ObjectMapper().registerModule(new MetricsModule(getRateUnit(), getDurationUnit(), false)); + if (getRateUnit() == TimeUnit.SECONDS && getDurationUnit() == TimeUnit.SECONDS) { + // they both use same units so reuse + this.secondsMapper = this.mapper; + } else { + this.secondsMapper = new ObjectMapper().registerModule(new MetricsModule(TimeUnit.SECONDS, TimeUnit.SECONDS, false)); + } + } + + @Override + protected void doStop() throws Exception { + if (reporter != null) { + reporter.stop(); + reporter = null; + } + } + + @Override + public String dumpStatisticsAsJson() { + ObjectWriter writer = mapper.writer(); + if (isPrettyPrint()) { + writer = writer.withDefaultPrettyPrinter(); + } + try { + return writer.writeValueAsString(getMetricsRegistry()); + } catch (JsonProcessingException e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } + } + + public String dumpStatisticsAsJsonTimeUnitSeconds() { + ObjectWriter writer = secondsMapper.writer(); + if (isPrettyPrint()) { + writer = writer.withDefaultPrettyPrinter(); + } + try { + return writer.writeValueAsString(getMetricsRegistry()); + } catch (JsonProcessingException e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0b00fda9/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 6f87a3d..6fe782a 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 @@ -108,8 +108,6 @@ public final class MetricsRegistryService extends ServiceSupport implements Came public void setDurationUnit(TimeUnit durationUnit) { this.durationUnit = durationUnit; } - - @Override protected void doStart() throws Exception { @@ -177,6 +175,5 @@ public final class MetricsRegistryService extends ServiceSupport implements Came throw ObjectHelper.wrapRuntimeCamelException(e); } } - - + } http://git-wip-us.apache.org/repos/asf/camel/blob/0b00fda9/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicy.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicy.java b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicy.java index 4269f01..c921e83 100644 --- a/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicy.java +++ b/components/camel-metrics/src/main/java/org/apache/camel/component/metrics/routepolicy/MetricsRoutePolicy.java @@ -164,7 +164,6 @@ public class MetricsRoutePolicy extends RoutePolicySupport { answer = answer.replaceFirst("##name##", name); answer = answer.replaceFirst("##routeId##", route.getId()); answer = answer.replaceFirst("##type##", type); - // use dot to separate context from route, and dot for the type name return answer; } http://git-wip-us.apache.org/repos/asf/camel/blob/0b00fda9/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/messagehistory/ManagedMessageHistoryTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/messagehistory/ManagedMessageHistoryTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/messagehistory/ManagedMessageHistoryTest.java new file mode 100644 index 0000000..76919dc --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/messagehistory/ManagedMessageHistoryTest.java @@ -0,0 +1,113 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics.messagehistory; + +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 ManagedMessageHistoryTest extends CamelTestSupport { + + private MetricRegistry metricRegistry = new MetricRegistry(); + + @Override + protected boolean useJmx() { + return true; + } + + protected MBeanServer getMBeanServer() { + return context.getManagementStrategy().getManagementAgent().getMBeanServer(); + } + + // Setup the common MetricsRegistry for MetricsComponent and MetricsMessageHistoryFactory 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 { + CamelContext context = super.createCamelContext(); + + MetricsMessageHistoryFactory factory = new MetricsMessageHistoryFactory(); + factory.setUseJmx(true); + factory.setPrettyPrint(true); + factory.setMetricsRegistry(metricRegistry); + context.setMessageHistoryFactory(factory); + + return context; + } + + @Test + public void testMetricsRoutePolicy() throws Exception { + getMockEndpoint("mock:foo").expectedMessageCount(5); + getMockEndpoint("mock:bar").expectedMessageCount(5); + getMockEndpoint("mock:baz").expectedMessageCount(5); + + for (int i = 0; i < 10; i++) { + if (i % 2 == 0) { + template.sendBody("seda:foo", "Hello " + i); + } else { + template.sendBody("seda:bar", "Hello " + i); + } + } + + assertMockEndpointsSatisfied(); + + // there should be 3 names + assertEquals(3, metricRegistry.getNames().size()); + + // there should be 3 mbeans + Set<ObjectName> set = getMBeanServer().queryNames(new ObjectName("org.apache.camel.metrics:*"), null); + assertEquals(3, set.size()); + + String name = String.format("org.apache.camel:context=%s,type=services,name=MetricsMessageHistoryService", context.getManagementName()); + ObjectName on = ObjectName.getInstance(name); + String json = (String) getMBeanServer().invoke(on, "dumpStatisticsAsJson", null, null); + assertNotNull(json); + log.info(json); + + assertTrue(json.contains("foo.history")); + assertTrue(json.contains("bar.history")); + assertTrue(json.contains("baz.history")); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("seda:foo") + .to("mock:foo").id("foo"); + + from("seda:bar") + .to("mock:bar").id("bar") + .to("mock:baz").id("baz"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0b00fda9/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryTest.java new file mode 100644 index 0000000..17279c6 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/messagehistory/MetricsMessageHistoryTest.java @@ -0,0 +1,75 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics.messagehistory; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class MetricsMessageHistoryTest extends CamelTestSupport { + + private MetricRegistry registry = new MetricRegistry(); + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + + MetricsMessageHistoryFactory factory = new MetricsMessageHistoryFactory(); + factory.setUseJmx(false); + factory.setMetricsRegistry(registry); + context.setMessageHistoryFactory(factory); + + return context; + } + + @Test + public void testMetricsRoutePolicy() throws Exception { + getMockEndpoint("mock:foo").expectedMessageCount(5); + getMockEndpoint("mock:bar").expectedMessageCount(5); + getMockEndpoint("mock:baz").expectedMessageCount(5); + + for (int i = 0; i < 10; i++) { + if (i % 2 == 0) { + template.sendBody("seda:foo", "Hello " + i); + } else { + template.sendBody("seda:bar", "Hello " + i); + } + } + + assertMockEndpointsSatisfied(); + + // there should be 3 names + assertEquals(3, registry.getNames().size()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("seda:foo") + .to("mock:foo"); + + from("seda:bar") + .to("mock:bar") + .to("mock:baz"); + } + }; + } +}
