You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/04/04 15:49:26 UTC

svn commit: r1464546 - in /camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands: AbstractRouteCommand.java RouteProfile.java

Author: davsclaus
Date: Thu Apr  4 13:49:26 2013
New Revision: 1464546

URL: http://svn.apache.org/r1464546
Log:
CAMEL-6237: Polished karaf commands

Modified:
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/AbstractRouteCommand.java
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteProfile.java

Modified: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/AbstractRouteCommand.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/AbstractRouteCommand.java?rev=1464546&r1=1464545&r2=1464546&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/AbstractRouteCommand.java (original)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/AbstractRouteCommand.java Thu Apr  4 13:49:26 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.karaf.commands;
 
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 
 import org.apache.camel.CamelContext;
@@ -45,6 +47,9 @@ public abstract class AbstractRouteComma
             System.err.println("Camel routes using " + route + " not found.");
             return null;
         }
+        // we want the routes sorted
+        Collections.sort(camelRoutes, new RouteComparator());
+
         for (Route camelRoute : camelRoutes) {
             CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
             // Setting thread context classloader to the bundle classloader to enable
@@ -60,4 +65,25 @@ public abstract class AbstractRouteComma
 
         return null;
     }
+
+    /**
+     * To sort the routes.
+     */
+    private static final class RouteComparator implements Comparator<Route> {
+
+        @Override
+        public int compare(Route o1, Route o2) {
+            // sort by camel context first
+            String camel1 = o1.getRouteContext().getCamelContext().getName();
+            String camel2 = o2.getRouteContext().getCamelContext().getName();
+
+            if (camel1.equals(camel2)) {
+                // and then route names in the same context
+                return o1.getId().compareTo(o2.getId());
+            } else {
+                return camel1.compareTo(camel2);
+            }
+        }
+    }
+
 }

Modified: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteProfile.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteProfile.java?rev=1464546&r1=1464545&r2=1464546&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteProfile.java (original)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteProfile.java Thu Apr  4 13:49:26 2013
@@ -17,7 +17,6 @@
 package org.apache.camel.karaf.commands;
 
 import java.io.StringReader;
-import java.util.Iterator;
 import java.util.Set;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
@@ -31,87 +30,76 @@ import org.apache.camel.spi.ManagementAg
 import org.apache.camel.util.ProcessorStatDump;
 import org.apache.camel.util.RouteStatDump;
 import org.apache.camel.util.URISupport;
-import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
-import org.apache.karaf.shell.console.OsgiCommandSupport;
 import org.apache.karaf.util.StringEscapeUtils;
 
 /**
  * Command to display profile information about a Camel route.
  */
-@Command(scope = "camel", name = "route-profile", description = "Display profile information about a Camel route.")
-public class RouteProfile extends OsgiCommandSupport {
+@Command(scope = "camel", name = "route-profile", description = "Display profile information about Camel route(s).")
+public class RouteProfile extends AbstractRouteCommand {
 
-    protected static final String HEADER_FORMAT = "%-30s %10s %10s %12s %12s %12s %12s %12s %12s";
-    protected static final String OUTPUT_FORMAT = "[%-28s] [%8d] [%8d] [%10d] [%10d] [%10d] [%10d] [%10d] [%10d]";
+    protected static final String HEADER_FORMAT = "%-30s %10s %12s %12s %12s %12s %12s %12s";
+    protected static final String OUTPUT_FORMAT = "[%-28s] [%8d] [%10d] [%10d] [%10d] [%10d] [%10d] [%10s]";
 
-    @Argument(index = 0, name = "route", description = "The Camel route ID.", required = true, multiValued = false)
-    String route;
-
-    @Argument(index = 1, name = "context", description = "The Camel context name.", required = false, multiValued = false)
-    String context;
-
-    private CamelController camelController;
-
-    public void setCamelController(CamelController camelController) {
-        this.camelController = camelController;
-    }
+    private String previousCamelContextName;
 
+    @Override
     public Object doExecute() throws Exception {
-        Route camelRoute = camelController.getRoute(route, context);
-
-        if (camelRoute == null) {
-            System.err.println("Camel route " + route + " not found.");
-            return null;
-        }
+        previousCamelContextName = null; // reset state
+        return super.doExecute();
+    }
 
+    @Override
+    public void executeOnRoute(CamelContext camelContext, Route camelRoute) throws Exception {
         JAXBContext context = JAXBContext.newInstance(RouteStatDump.class);
         Unmarshaller unmarshaller = context.createUnmarshaller();
 
-        System.out.println(StringEscapeUtils.unescapeJava("\u001B[1m\u001B[33mCamel Route " + camelRoute.getId() + "\u001B[0m"));
-        System.out.println(StringEscapeUtils.unescapeJava("\tEndpoint uri: " + URISupport.sanitizeUri(camelRoute.getEndpoint().getEndpointUri())));
-        System.out.println(StringEscapeUtils.unescapeJava("\tCamel Context: " + camelRoute.getRouteContext().getCamelContext().getName()));
-        System.out.println("");
-        System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mProfile\u001B[0m"));
-        CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
-        if (camelContext != null) {
-            ManagementAgent agent = camelContext.getManagementStrategy().getManagementAgent();
-            if (agent != null) {
-                MBeanServer mBeanServer = agent.getMBeanServer();
-                Set<ObjectName> set = mBeanServer.queryNames(new ObjectName(DefaultManagementAgent.DEFAULT_DOMAIN + ":type=routes,name=\"" + route + "\",*"), null);
-                Iterator<ObjectName> iterator = set.iterator();
-                if (iterator.hasNext()) {
-                    ObjectName routeMBean = iterator.next();
-
-                    // the route must be part of the camel context
-                    String camelId = (String) mBeanServer.getAttribute(routeMBean, "CamelId");
-                    if (camelId != null && camelId.equals(camelContext.getName())) {
-
-                        // TODO: add column with total time (delta for self time)
-
-                        String xml = (String) mBeanServer.invoke(routeMBean, "dumpRouteStatsAsXml", new Object[]{Boolean.FALSE, Boolean.TRUE}, new String[]{"boolean", "boolean"});
-                        RouteStatDump route = (RouteStatDump) unmarshaller.unmarshal(new StringReader(xml));
-
-                        System.out.println(String.format(HEADER_FORMAT, "Id", "Completed", "Failed", "Last (ms)", "Delta (ms)", "Mean (ms)", "Min (ms)", "Max (ms)", "Self (ms)"));
-                        System.out.println(String.format(OUTPUT_FORMAT, route.getId(), route.getExchangesCompleted(), route.getExchangesFailed(), route.getLastProcessingTime(),
-                                route.getDeltaProcessingTime(), route.getMeanProcessingTime(), route.getMinProcessingTime(), route.getMaxProcessingTime(), route.getTotalProcessingTime(), 0));
-
-                        // output in reverse order which prints the route as we want
-                        for (ProcessorStatDump ps : route.getProcessorStats()) {
-                            // the self time is the total time of the processor itself
-                            long selfTime = ps.getTotalProcessingTime();
-                            // indent route id with 2 spaces
-                            System.out.println(String.format(OUTPUT_FORMAT, "  " + ps.getId(), ps.getExchangesCompleted(), ps.getExchangesFailed(), ps.getLastProcessingTime(),
-                                    ps.getDeltaProcessingTime(), ps.getMeanProcessingTime(), ps.getMinProcessingTime(), ps.getMaxProcessingTime(), selfTime));
-                        }
+        // write new header for new camel context
+        if (previousCamelContextName == null || !previousCamelContextName.equals(camelContext.getName())) {
+            System.out.println("");
+            System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mProfile\u001B[0m"));
+            System.out.println(StringEscapeUtils.unescapeJava("\tCamel Context: " + camelRoute.getRouteContext().getCamelContext().getName()));
+            System.out.println(String.format(HEADER_FORMAT, "Id", "Count", "Last (ms)", "Delta (ms)", "Mean (ms)", "Min (ms)", "Max (ms)", "Self (ms)"));
+        }
+//        System.out.println(StringEscapeUtils.unescapeJava("\u001B[1m\u001B[33mCamel Route " + camelRoute.getId() + "\u001B[0m"));
+//        System.out.println(StringEscapeUtils.unescapeJava("\tEndpoint uri: " + URISupport.sanitizeUri(camelRoute.getEndpoint().getEndpointUri())));
+
+        ManagementAgent agent = camelContext.getManagementStrategy().getManagementAgent();
+        if (agent != null) {
+            MBeanServer mBeanServer = agent.getMBeanServer();
+            Set<ObjectName> set = mBeanServer.queryNames(new ObjectName(DefaultManagementAgent.DEFAULT_DOMAIN + ":type=routes,name=\"" + camelRoute.getId() + "\",*"), null);
+            for (ObjectName routeMBean : set) {
+                // the route must be part of the camel context
+                String camelId = (String) mBeanServer.getAttribute(routeMBean, "CamelId");
+                if (camelId != null && camelId.equals(camelContext.getName())) {
+
+                    // TODO: add column with total time (delta for self time)
+
+                    String xml = (String) mBeanServer.invoke(routeMBean, "dumpRouteStatsAsXml", new Object[]{Boolean.FALSE, Boolean.TRUE}, new String[]{"boolean", "boolean"});
+                    RouteStatDump route = (RouteStatDump) unmarshaller.unmarshal(new StringReader(xml));
+
+                    long count = route.getExchangesCompleted() + route.getExchangesFailed();
+                    System.out.println(String.format(OUTPUT_FORMAT, route.getId(), count, route.getLastProcessingTime(), route.getDeltaProcessingTime(),
+                            route.getMeanProcessingTime(), route.getMinProcessingTime(), route.getMaxProcessingTime(), ""));
+
+                    for (ProcessorStatDump ps : route.getProcessorStats()) {
+                        // the self time is the total time of the processor itself
+                        String selfTime = "" + ps.getTotalProcessingTime();
+                        count = ps.getExchangesCompleted() + ps.getExchangesFailed();
+                        // indent route id with 2 spaces
+                        System.out.println(String.format(OUTPUT_FORMAT, "  " + ps.getId(), count, ps.getLastProcessingTime(),
+                                ps.getDeltaProcessingTime(), ps.getMeanProcessingTime(), ps.getMinProcessingTime(), ps.getMaxProcessingTime(), selfTime));
                     }
                 }
-            } else {
-                System.out.println("");
-                System.out.println(StringEscapeUtils.unescapeJava("\u001B[31mJMX Agent of Camel is not reachable. Maybe it has been disabled on the Camel context"));
-                System.out.println(StringEscapeUtils.unescapeJava("In consequence, profile are not available.\u001B[0m"));
             }
+        } else {
+            System.out.println("");
+            System.out.println(StringEscapeUtils.unescapeJava("\u001B[31mJMX Agent of Camel is not reachable. Maybe it has been disabled on the Camel context"));
+            System.out.println(StringEscapeUtils.unescapeJava("In consequence, profile are not available.\u001B[0m"));
         }
-        return null;
+
+        // we want to group routes from the same context in the same table
+        previousCamelContextName = camelContext.getName();
     }
 }