CAMEL-6419: Added stats action to controlbus component to get performance stats easily.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8b84f686 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8b84f686 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8b84f686 Branch: refs/heads/master Commit: 8b84f6863f37a510db197265f7c0fa0889cf8d9b Parents: 5b942e3 Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Jun 3 19:48:04 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Jun 3 19:48:22 2013 +0200 ---------------------------------------------------------------------- .../component/controlbus/ControlBusProducer.java | 31 ++++++ .../component/controlbus/ControlBusStatsTest.java | 78 +++++++++++++++ 2 files changed, 109 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/8b84f686/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java b/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java index 6a8bd65..78a262a 100644 --- a/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java +++ b/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java @@ -16,11 +16,17 @@ */ package org.apache.camel.component.controlbus; +import javax.management.MBeanServer; +import javax.management.ObjectName; + import org.apache.camel.AsyncCallback; +import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.Exchange; import org.apache.camel.Expression; +import org.apache.camel.Route; import org.apache.camel.ServiceStatus; +import org.apache.camel.api.management.mbean.ManagedCamelContextMBean; import org.apache.camel.impl.DefaultAsyncProducer; import org.apache.camel.spi.Language; import org.apache.camel.util.CamelLogger; @@ -156,6 +162,31 @@ public class ControlBusProducer extends DefaultAsyncProducer { if (status != null) { result = status.name(); } + } else if ("stats".equals(action)) { + + // camel context or per route + String name = getEndpoint().getCamelContext().getManagementName(); + if (name == null) { + result = "JMX is disabled, cannot get stats"; + } else { + ObjectName on; + String operation; + if (id == null) { + CamelContext camelContext = getEndpoint().getCamelContext(); + on = getEndpoint().getCamelContext().getManagementStrategy().getManagementNamingStrategy().getObjectNameForCamelContext(camelContext); + operation = "dumpRoutesStatsAsXml"; + } else { + Route route = getEndpoint().getCamelContext().getRoute(id); + on = getEndpoint().getCamelContext().getManagementStrategy().getManagementNamingStrategy().getObjectNameForRoute(route); + operation = "dumpRouteStatsAsXml"; + } + if (on != null) { + MBeanServer server = getEndpoint().getCamelContext().getManagementStrategy().getManagementAgent().getMBeanServer(); + result = server.invoke(on, operation, new Object[]{true, true}, new String[]{"boolean", "boolean"}); + } else { + result = "Cannot lookup route with id " + id; + } + } } if (result != null && !getEndpoint().isAsync()) { http://git-wip-us.apache.org/repos/asf/camel/blob/8b84f686/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStatsTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStatsTest.java b/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStatsTest.java new file mode 100644 index 0000000..20b100b --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStatsTest.java @@ -0,0 +1,78 @@ +/** + * 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.controlbus; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +/** + * + */ +public class ControlBusStatsTest extends ContextTestSupport { + + @Override + protected boolean useJmx() { + return true; + } + + public void testControlBusRouteStat() throws Exception { + getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); + + template.sendBody("direct:foo", "Hello World"); + + assertMockEndpointsSatisfied(); + + String xml = template.requestBody("controlbus:route?routeId=foo&action=stats", null, String.class); + assertNotNull(xml); + + assertTrue(xml.contains("routeStat")); + assertTrue(xml.contains("processorStat")); + assertTrue(xml.contains("id=\"foo\"")); + assertTrue(xml.contains("exchangesCompleted=\"1\"")); + } + + public void testControlBusContextStat() throws Exception { + getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World"); + + template.sendBody("direct:bar", "Hello World"); + + assertMockEndpointsSatisfied(); + + String xml = template.requestBody("controlbus:route?action=stats", null, String.class); + assertNotNull(xml); + + assertTrue(xml.contains("camelContextStat")); + assertTrue(xml.contains("routeStat")); + assertTrue(xml.contains("processorStat")); + assertTrue(xml.contains("id=\"bar\"")); + assertTrue(xml.contains("id=\"foo\"")); + assertTrue(xml.contains("exchangesCompleted=\"1\"")); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:foo").routeId("foo") + .to("mock:foo"); + + from("direct:bar").routeId("bar") + .to("mock:bar"); + } + }; + } +}