Author: davsclaus
Date: Mon Apr 22 10:04:32 2013
New Revision: 1470426

URL: http://svn.apache.org/r1470426
Log:
CAMEL-6301: Added reset method to context and routes mbean, to easily reset the 
children as well.

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedResetIncludeProcessorsTest.java
      - copied, changed from r1470423, 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedStatisticsTest.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedResetIncludeRoutesTest.java
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelContextMBean.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRouteMBean.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
    
camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteResetStats.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelContextMBean.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelContextMBean.java?rev=1470426&r1=1470425&r2=1470426&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelContextMBean.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelContextMBean.java
 Mon Apr 22 10:04:32 2013
@@ -171,4 +171,13 @@ public interface ManagedCamelContextMBea
     @ManagedOperation(description = "Find all Camel components available in 
the classpath")
     Map<String, Properties> findComponents() throws Exception;
 
+    /**
+     * Resets all the performance counters.
+     *
+     * @param includeRoutes  whether to reset all routes as well.
+     * @throws Exception is thrown if error occurred
+     */
+    @ManagedOperation(description = "Reset counters")
+    void reset(boolean includeRoutes) throws Exception;
+
 }
\ No newline at end of file

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRouteMBean.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRouteMBean.java?rev=1470426&r1=1470425&r2=1470426&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRouteMBean.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/api/management/mbean/ManagedRouteMBean.java
 Mon Apr 22 10:04:32 2013
@@ -95,4 +95,7 @@ public interface ManagedRouteMBean exten
     @ManagedOperation(description = "Dumps the routes stats as XML")
     String dumpRouteStatsAsXml(boolean fullStats, boolean includeProcessors) 
throws Exception;
 
+    @ManagedOperation(description = "Reset counters")
+    void reset(boolean includeProcessors) throws Exception;
+
 }
\ No newline at end of file

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java?rev=1470426&r1=1470425&r2=1470426&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
 Mon Apr 22 10:04:32 2013
@@ -336,4 +336,21 @@ public class ManagedCamelContext extends
         return CamelContextHelper.findComponents(context);
     }
 
+    public void reset(boolean includeRoutes) throws Exception {
+        reset();
+
+        // and now reset all processors for this route
+        if (includeRoutes) {
+            MBeanServer server = 
getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
+            if (server != null) {
+                // get all the routes mbeans and sort them accordingly to 
their index
+                ObjectName query = 
ObjectName.getInstance("org.apache.camel:context=*/" + 
getContext().getManagementName() + ",type=routes,*");
+                Set<ObjectName> names = server.queryNames(query, null);
+                for (ObjectName name : names) {
+                    server.invoke(name, "reset", new Object[]{true}, new 
String[]{"boolean"});
+                }
+            }
+        }
+    }
+
 }

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java?rev=1470426&r1=1470425&r2=1470426&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
 Mon Apr 22 10:04:32 2013
@@ -24,9 +24,13 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
+import javax.management.AttributeValueExp;
 import javax.management.MBeanServer;
 import javax.management.MBeanServerInvocationHandler;
 import javax.management.ObjectName;
+import javax.management.Query;
+import javax.management.QueryExp;
+import javax.management.StringValueExp;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
@@ -295,6 +299,24 @@ public class ManagedRoute extends Manage
         return answer.toString();
     }
 
+    public void reset(boolean includeProcessors) throws Exception {
+        reset();
+
+        // and now reset all processors for this route
+        if (includeProcessors) {
+            MBeanServer server = 
getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
+            if (server != null) {
+                // get all the processor mbeans and sort them accordingly to 
their index
+                ObjectName query = 
ObjectName.getInstance("org.apache.camel:context=*/" + 
getContext().getManagementName() + ",type=processors,*");
+                QueryExp queryExp = Query.match(new 
AttributeValueExp("RouteId"), new StringValueExp(getRouteId()));
+                Set<ObjectName> names = server.queryNames(query, queryExp);
+                for (ObjectName name : names) {
+                    server.invoke(name, "reset", null, null);
+                }
+            }
+        }
+    }
+
     @Override
     public boolean equals(Object o) {
         return this == o || (o != null && getClass() == o.getClass() && 
route.equals(((ManagedRoute)o).route));

Copied: 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedResetIncludeProcessorsTest.java
 (from r1470423, 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedStatisticsTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedResetIncludeProcessorsTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedResetIncludeProcessorsTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedStatisticsTest.java&r1=1470423&r2=1470426&rev=1470426&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedStatisticsTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedResetIncludeProcessorsTest.java
 Mon Apr 22 10:04:32 2013
@@ -17,33 +17,29 @@
 package org.apache.camel.management;
 
 import java.util.Set;
+import javax.management.AttributeValueExp;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
+import javax.management.Query;
+import javax.management.QueryExp;
+import javax.management.StringValueExp;
 
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
 
 /**
  * @version 
  */
-public class ManagedStatisticsTest extends ManagementTestSupport {
+public class ManagedResetIncludeProcessorsTest extends ManagementTestSupport {
 
-    public void testManageStatistics() throws Exception {
+    public void testReset() throws Exception {
         // get the stats for the route
         MBeanServer mbeanServer = getMBeanServer();
 
-        Set<ObjectName> set = mbeanServer.queryNames(new 
ObjectName("*:type=routes,*"), null);
+        QueryExp queryExp = Query.match(new AttributeValueExp("RouteId"), new 
StringValueExp("first"));
+        Set<ObjectName> set = mbeanServer.queryNames(new 
ObjectName("*:type=routes,*"), queryExp);
         assertEquals(1, set.size());
-
         ObjectName on = set.iterator().next();
 
-        // use route to get the total time
-        Long completed = (Long) mbeanServer.getAttribute(on, 
"ExchangesCompleted");
-        assertEquals(0, completed.longValue());
-        
-        MockEndpoint result = getMockEndpoint("mock:result");
-        result.expectedMessageCount(5);
-
         // send in 5 messages
         template.sendBody("direct:start", "A");
         template.sendBody("direct:start", "B");
@@ -51,32 +47,54 @@ public class ManagedStatisticsTest exten
         template.sendBody("direct:start", "D");
         template.sendBody("direct:start", "E");
 
+        // and 1 for the 2nd route
+        template.sendBody("direct:baz", "F");
+
         assertMockEndpointsSatisfied();
         
         // should be 5 on the route
-        completed = (Long) mbeanServer.getAttribute(on, "ExchangesCompleted");
+        Long completed = (Long) mbeanServer.getAttribute(on, 
"ExchangesCompleted");
         assertEquals(5, completed.longValue());
-        
-        String first = (String) mbeanServer.getAttribute(on, 
"FirstExchangeCompletedExchangeId");
-        assertEquals(result.getReceivedExchanges().get(0).getExchangeId(), 
first);
 
-        String firstFail = (String) mbeanServer.getAttribute(on, 
"FirstExchangeFailureExchangeId");
-        assertNull(firstFail);
+        // and on the processors as well
+        set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), 
queryExp);
+        assertEquals(3, set.size());
+        for (ObjectName name : set) {
+            completed = (Long) mbeanServer.getAttribute(name, 
"ExchangesCompleted");
+            assertEquals(5, completed.longValue());
+        }
 
-        String last = (String) mbeanServer.getAttribute(on, 
"LastExchangeCompletedExchangeId");
-        assertEquals(result.getReceivedExchanges().get(4).getExchangeId(), 
last);
+        // reset which should reset all processors also
+        mbeanServer.invoke(on, "reset", new Object[]{true}, new 
String[]{"boolean"});
 
-        String lastFail = (String) mbeanServer.getAttribute(on, 
"LastExchangeFailureExchangeId");
-        assertNull(lastFail);
+        // should be 0 on the route
+        completed = (Long) mbeanServer.getAttribute(on, "ExchangesCompleted");
+        assertEquals(0, completed.longValue());
 
-        // should be 5 on the processors
-        ObjectName foo = 
ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=processors,name=\"foo\"");
-        completed = (Long) mbeanServer.getAttribute(foo, "ExchangesCompleted");
-        assertEquals(5, completed.longValue());
+        // and on the processors as well
+        set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), 
queryExp);
+        assertEquals(3, set.size());
+        for (ObjectName name : set) {
+            completed = (Long) mbeanServer.getAttribute(name, 
"ExchangesCompleted");
+            assertEquals(0, completed.longValue());
+        }
+
+        // test that the 2nd route is untouched, as we only reset the first 
route
+        queryExp = Query.match(new AttributeValueExp("RouteId"), new 
StringValueExp("second"));
+        set = mbeanServer.queryNames(new ObjectName("*:type=routes,*"), 
queryExp);
+        assertEquals(1, set.size());
+        on = set.iterator().next();
 
-        ObjectName mock = 
ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=processors,name=\"mock\"");
-        completed = (Long) mbeanServer.getAttribute(mock, 
"ExchangesCompleted");
-        assertEquals(5, completed.longValue());
+        completed = (Long) mbeanServer.getAttribute(on, "ExchangesCompleted");
+        assertEquals(1, completed.longValue());
+
+        // and on the processors as well
+        set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), 
queryExp);
+        assertEquals(1, set.size());
+        for (ObjectName name : set) {
+            completed = (Long) mbeanServer.getAttribute(name, 
"ExchangesCompleted");
+            assertEquals(1, completed.longValue());
+        }
     }
 
     @Override
@@ -84,10 +102,13 @@ public class ManagedStatisticsTest exten
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start")
+                from("direct:start").routeId("first")
                     .to("log:foo").id("foo")
                     .to("log:bar").id("bar")
                     .to("mock:result").id("mock");
+
+                from("direct:baz").routeId("second")
+                    .to("mock:baz").id("baz");
             }
         };
     }

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedResetIncludeRoutesTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedResetIncludeRoutesTest.java?rev=1470426&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedResetIncludeRoutesTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedResetIncludeRoutesTest.java
 Mon Apr 22 10:04:32 2013
@@ -0,0 +1,117 @@
+/**
+ * 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.management;
+
+import java.util.Set;
+import javax.management.AttributeValueExp;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.Query;
+import javax.management.QueryExp;
+import javax.management.StringValueExp;
+
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version 
+ */
+public class ManagedResetIncludeRoutesTest extends ManagementTestSupport {
+
+    public void testReset() throws Exception {
+        // get the stats for the route
+        MBeanServer mbeanServer = getMBeanServer();
+
+        QueryExp queryExp = Query.match(new AttributeValueExp("RouteId"), new 
StringValueExp("first"));
+        Set<ObjectName> set = mbeanServer.queryNames(new 
ObjectName("*:type=routes,*"), queryExp);
+        assertEquals(1, set.size());
+        ObjectName on = set.iterator().next();
+
+        // send in 5 messages
+        template.sendBody("direct:start", "A");
+        template.sendBody("direct:start", "B");
+        template.sendBody("direct:start", "C");
+        template.sendBody("direct:start", "D");
+        template.sendBody("direct:start", "E");
+
+        // and 1 for the 2nd route
+        template.sendBody("direct:baz", "F");
+
+        assertMockEndpointsSatisfied();
+
+        // should be 5 on the route
+        Long completed = (Long) mbeanServer.getAttribute(on, 
"ExchangesCompleted");
+        assertEquals(5, completed.longValue());
+
+        // and on the processors as well
+        set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), 
queryExp);
+        assertEquals(3, set.size());
+        for (ObjectName name : set) {
+            completed = (Long) mbeanServer.getAttribute(name, 
"ExchangesCompleted");
+            assertEquals(5, completed.longValue());
+        }
+
+        // reset which should reset all routes also
+        ObjectName ctx = 
ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=context,name=\"camel-1\"");
+        mbeanServer.invoke(ctx, "reset", new Object[]{true}, new 
String[]{"boolean"});
+
+        // should be 0 on the route
+        completed = (Long) mbeanServer.getAttribute(on, "ExchangesCompleted");
+        assertEquals(0, completed.longValue());
+
+        // and on the processors as well
+        set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), 
queryExp);
+        assertEquals(3, set.size());
+        for (ObjectName name : set) {
+            completed = (Long) mbeanServer.getAttribute(name, 
"ExchangesCompleted");
+            assertEquals(0, completed.longValue());
+        }
+
+        // test that the 2nd route is also reset
+        queryExp = Query.match(new AttributeValueExp("RouteId"), new 
StringValueExp("second"));
+        set = mbeanServer.queryNames(new ObjectName("*:type=routes,*"), 
queryExp);
+        assertEquals(1, set.size());
+        on = set.iterator().next();
+
+        completed = (Long) mbeanServer.getAttribute(on, "ExchangesCompleted");
+        assertEquals(0, completed.longValue());
+
+        // and on the processors as well
+        set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), 
queryExp);
+        assertEquals(1, set.size());
+        for (ObjectName name : set) {
+            completed = (Long) mbeanServer.getAttribute(name, 
"ExchangesCompleted");
+            assertEquals(0, completed.longValue());
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").routeId("first")
+                    .to("log:foo").id("foo")
+                    .to("log:bar").id("bar")
+                    .to("mock:result").id("mock");
+
+                from("direct:baz").routeId("second")
+                    .to("mock:baz").id("baz");
+            }
+        };
+    }
+
+}
\ No newline at end of file

Modified: 
camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteResetStats.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteResetStats.java?rev=1470426&r1=1470425&r2=1470426&view=diff
==============================================================================
--- 
camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteResetStats.java
 (original)
+++ 
camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteResetStats.java
 Mon Apr 22 10:04:32 2013
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.karaf.commands;
 
-import java.util.Iterator;
 import java.util.Set;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
@@ -34,8 +33,6 @@ public class RouteResetStats extends Abs
 
     @Override
     public void executeOnRoute(CamelContext camelContext, Route camelRoute) 
throws Exception {
-        String id = camelRoute.getId();
-
         ManagementAgent agent = 
camelContext.getManagementStrategy().getManagementAgent();
         if (agent != null) {
             MBeanServer mBeanServer = agent.getMBeanServer();
@@ -43,27 +40,10 @@ public class RouteResetStats extends Abs
             // reset route mbeans
             ObjectName query = 
ObjectName.getInstance(agent.getMBeanObjectDomainName() + ":type=routes,*");
             Set<ObjectName> set = mBeanServer.queryNames(query, null);
-            Iterator<ObjectName> iterator = set.iterator();
-            while (iterator.hasNext()) {
-                ObjectName routeMBean = iterator.next();
-
+            for (ObjectName routeMBean : set) {
                 String camelId = (String) mBeanServer.getAttribute(routeMBean, 
"CamelId");
                 if (camelId != null && camelId.equals(camelContext.getName())) 
{
-                    mBeanServer.invoke(routeMBean, "reset", null, null);
-                }
-            }
-
-            // reset processor mbeans that belongs to the given route
-            query = ObjectName.getInstance(agent.getMBeanObjectDomainName() + 
":type=processors,*");
-            set = mBeanServer.queryNames(query, null);
-            iterator = set.iterator();
-            while (iterator.hasNext()) {
-                ObjectName processorMBean = iterator.next();
-                // must belong to this camel context and match the route id
-                String camelId = (String) 
mBeanServer.getAttribute(processorMBean, "CamelId");
-                String routeId = (String) 
mBeanServer.getAttribute(processorMBean, "RouteId");
-                if (camelId != null && camelId.equals(camelContext.getName()) 
&& routeId != null && routeId.equals(id)) {
-                    mBeanServer.invoke(processorMBean, "reset", null, null);
+                    mBeanServer.invoke(routeMBean, "reset", new 
Object[]{true}, new String[]{"boolean"});
                 }
             }
         }


Reply via email to