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 2014/12/10 17:32:21 UTC

[11/15] camel git commit: CAMEL-8044: Camel commands should be more reusable for remote JVMs

CAMEL-8044: Camel commands should be more reusable for remote JVMs


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/83900763
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/83900763
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/83900763

Branch: refs/heads/master
Commit: 83900763caecb384b8bc4c8a7be3d4fb5c852c7b
Parents: da299a3
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Dec 10 15:08:09 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Dec 10 15:08:09 2014 +0100

----------------------------------------------------------------------
 .../camel/commands/AbstractCamelController.java |  44 +++++--
 .../apache/camel/commands/CamelController.java  |  20 +--
 .../apache/camel/commands/RouteInfoCommand.java | 128 +++++++------------
 .../camel/commands/RouteProfileCommand.java     |  45 ++-----
 4 files changed, 106 insertions(+), 131 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/83900763/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
----------------------------------------------------------------------
diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
index f0186ae..0282e7a 100644
--- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
+++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java
@@ -20,12 +20,15 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
 import javax.xml.bind.JAXBException;
 
 import org.apache.camel.CamelContext;
@@ -38,6 +41,7 @@ import org.apache.camel.model.ModelHelper;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.rest.RestDefinition;
 import org.apache.camel.model.rest.RestsDefinition;
+import org.apache.camel.spi.ManagementAgent;
 import org.apache.camel.spi.RestRegistry;
 import org.apache.camel.util.JsonSchemaHelper;
 import org.apache.camel.util.ObjectHelper;
@@ -104,16 +108,6 @@ public abstract class AbstractCamelController implements CamelController {
         return routes;
     }
 
-    public Route getRoute(String routeId, String camelContextName) {
-        List<Route> routes = this.getRoutes(camelContextName);
-        for (Route route : routes) {
-            if (route.getId().equals(routeId)) {
-                return route;
-            }
-        }
-        return null;
-    }
-
     @SuppressWarnings("deprecation")
     public String getRouteModelAsXml(String routeId, String camelContextName) {
         CamelContext context = this.getCamelContext(camelContextName);
@@ -134,6 +128,36 @@ public abstract class AbstractCamelController implements CamelController {
         return xml;
     }
 
+    @Override
+    public String getRouteStatsAsXml(String routeId, String camelContextName, boolean fullStats, boolean includeProcessors) {
+        CamelContext context = this.getCamelContext(camelContextName);
+        if (context == null) {
+            return null;
+        }
+
+        try {
+            ManagementAgent agent = context.getManagementStrategy().getManagementAgent();
+            if (agent != null) {
+                MBeanServer mBeanServer = agent.getMBeanServer();
+                Set<ObjectName> set = mBeanServer.queryNames(new ObjectName(agent.getMBeanObjectDomainName() + ":type=routes,name=\"" + routeId + "\",*"), 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(camelContextName)) {
+                        String xml = (String) mBeanServer.invoke(routeMBean, "dumpRouteStatsAsXml", new Object[]{fullStats, includeProcessors}, new String[]{"boolean", "boolean"});
+                        return xml;
+                    }
+                }
+            }
+        } catch (Exception e) {
+            throw ObjectHelper.wrapRuntimeCamelException(e);
+        }
+        return null;
+    }
+
     @SuppressWarnings("deprecation")
     public String getRestModelAsXml(String camelContextName) {
         CamelContext context = this.getCamelContext(camelContextName);

http://git-wip-us.apache.org/repos/asf/camel/blob/83900763/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java
----------------------------------------------------------------------
diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java
index ddf4040..756caeb 100644
--- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java
+++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java
@@ -65,15 +65,6 @@ public interface CamelController {
     List<Route> getRoutes(String camelContextName, String filter);
 
     /**
-     * Return the route with the given route ID.
-     *
-     * @param routeId          the route ID.
-     * @param camelContextName the Camel context name.
-     * @return the route.
-     */
-    Route getRoute(String routeId, String camelContextName);
-
-    /**
      * Return the definition of a route as XML identified by a ID and a Camel context.
      *
      * @param routeId          the route ID.
@@ -83,6 +74,17 @@ public interface CamelController {
     String getRouteModelAsXml(String routeId, String camelContextName);
 
     /**
+     * Returns detailed route statistics as XML identified by a ID and a Camel context.
+     *
+     * @param routeId           the route ID.
+     * @param camelContextName  the Camel context.
+     * @param fullStats         whether to include verbose stats
+     * @param includeProcessors whether to embed per processor stats from the route
+     * @return the route statistics as XML
+     */
+    String getRouteStatsAsXml(String routeId, String camelContextName, boolean fullStats, boolean includeProcessors);
+
+    /**
      * Return the endpoints
      *
      * @param camelContextName the Camel context.

http://git-wip-us.apache.org/repos/asf/camel/blob/83900763/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteInfoCommand.java
----------------------------------------------------------------------
diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteInfoCommand.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteInfoCommand.java
index e648546..eacfbb5 100644
--- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteInfoCommand.java
+++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteInfoCommand.java
@@ -17,23 +17,23 @@
 package org.apache.camel.commands;
 
 import java.io.PrintStream;
+import java.io.StringReader;
 import java.text.SimpleDateFormat;
 import java.util.Date;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Unmarshaller;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Route;
-import org.apache.camel.spi.ManagementAgent;
+import org.apache.camel.util.RouteStatDump;
 
 /**
  * Command to display detailed information about a Camel route.
  */
 public class RouteInfoCommand extends AbstractRouteCommand {
 
+    public static final String XML_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
+    public static final String OUTPUT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss";
     private StringEscape stringEscape;
 
     public RouteInfoCommand(String route, String context) {
@@ -52,89 +52,57 @@ public class RouteInfoCommand extends AbstractRouteCommand {
         out.println(stringEscape.unescapeJava("\u001B[1m\u001B[33mCamel Route " + camelRoute.getId() + "\u001B[0m"));
         out.println(stringEscape.unescapeJava("\tCamel Context: " + camelRoute.getRouteContext().getCamelContext().getName()));
         out.println("");
-        out.println(stringEscape.unescapeJava("\u001B[1mProperties\u001B[0m"));
-        for (Map.Entry<String, Object> entry : camelRoute.getProperties().entrySet()) {
-            out.println(stringEscape.unescapeJava("\t" + entry.getKey() + " = " + entry.getValue()));
-        }
-        out.println("");
         out.println(stringEscape.unescapeJava("\u001B[1mStatistics\u001B[0m"));
-        if (camelContext != null) {
-            ManagementAgent agent = camelContext.getManagementStrategy().getManagementAgent();
-            if (agent != null) {
-                MBeanServer mBeanServer = agent.getMBeanServer();
-                Set<ObjectName> set = mBeanServer.queryNames(new ObjectName(agent.getMBeanObjectDomainName() + ":type=routes,name=\"" + camelRoute.getId() + "\",*"), 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())) {
-                        Integer inflightExchange = (Integer) mBeanServer.getAttribute(routeMBean, "InflightExchanges");
-                        out.println(stringEscape.unescapeJava("\tInflight Exchanges: " + inflightExchange));
-                        Long exchangesTotal = (Long) mBeanServer.getAttribute(routeMBean, "ExchangesTotal");
-                        out.println(stringEscape.unescapeJava("\tExchanges Total: " + exchangesTotal));
-                        Long exchangesCompleted = (Long) mBeanServer.getAttribute(routeMBean, "ExchangesCompleted");
-                        out.println(stringEscape.unescapeJava("\tExchanges Completed: " + exchangesCompleted));
-                        Long exchangesFailed = (Long) mBeanServer.getAttribute(routeMBean, "ExchangesFailed");
-                        out.println(stringEscape.unescapeJava("\tExchanges Failed: " + exchangesFailed));
-                        Long minProcessingTime = (Long) mBeanServer.getAttribute(routeMBean, "MinProcessingTime");
-                        out.println(stringEscape.unescapeJava("\tMin Processing Time: " + minProcessingTime + " ms"));
-                        Long maxProcessingTime = (Long) mBeanServer.getAttribute(routeMBean, "MaxProcessingTime");
-                        out.println(stringEscape.unescapeJava("\tMax Processing Time: " + maxProcessingTime + " ms"));
-                        Long meanProcessingTime = (Long) mBeanServer.getAttribute(routeMBean, "MeanProcessingTime");
-                        out.println(stringEscape.unescapeJava("\tMean Processing Time: " + meanProcessingTime + " ms"));
-                        Long totalProcessingTime = (Long) mBeanServer.getAttribute(routeMBean, "TotalProcessingTime");
-                        out.println(stringEscape.unescapeJava("\tTotal Processing Time: " + totalProcessingTime + " ms"));
-                        Long lastProcessingTime = (Long) mBeanServer.getAttribute(routeMBean, "LastProcessingTime");
-                        out.println(stringEscape.unescapeJava("\tLast Processing Time: " + lastProcessingTime + " ms"));
-                        Long deltaProcessingTime = (Long) mBeanServer.getAttribute(routeMBean, "DeltaProcessingTime");
-                        out.println(stringEscape.unescapeJava("\tDelta Processing Time: " + deltaProcessingTime + " ms"));
-                        String load01 = (String) mBeanServer.getAttribute(routeMBean, "Load01");
-                        String load05 = (String) mBeanServer.getAttribute(routeMBean, "Load05");
-                        String load15 = (String) mBeanServer.getAttribute(routeMBean, "Load15");
-                        out.println(stringEscape.unescapeJava("\tLoad Avg: " + load01 + ", " + load05 + ", " + load15));
+        String xml = camelController.getRouteStatsAsXml(camelRoute.getId(), camelContext.getName(), true, false);
+        if (xml != null) {
+            JAXBContext context = JAXBContext.newInstance(RouteStatDump.class);
+            Unmarshaller unmarshaller = context.createUnmarshaller();
+
+            RouteStatDump route = (RouteStatDump) unmarshaller.unmarshal(new StringReader(xml));
+
+            out.println(stringEscape.unescapeJava("\tExchanges Total: " + route.getExchangesCompleted() + route.getExchangesFailed()));
+            out.println(stringEscape.unescapeJava("\tExchanges Completed: " + route.getExchangesCompleted()));
+            out.println(stringEscape.unescapeJava("\tExchanges Failed: " + route.getExchangesFailed()));
+            out.println(stringEscape.unescapeJava("\tMin Processing Time: " + route.getMinProcessingTime() + " ms"));
+            out.println(stringEscape.unescapeJava("\tMax Processing Time: " + route.getMaxProcessingTime() + " ms"));
+            out.println(stringEscape.unescapeJava("\tMean Processing Time: " + route.getMeanProcessingTime() + " ms"));
+            out.println(stringEscape.unescapeJava("\tTotal Processing Time: " + route.getTotalProcessingTime() + " ms"));
+            out.println(stringEscape.unescapeJava("\tLast Processing Time: " + route.getLastProcessingTime() + " ms"));
+            out.println(stringEscape.unescapeJava("\tDelta Processing Time: " + route.getDeltaProcessingTime() + " ms"));
 
-                        // Test for null to see if a any exchanges have been processed first to avoid NPE
-                        Object resetTimestampObj = mBeanServer.getAttribute(routeMBean, "ResetTimestamp");
-                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-                        if (resetTimestampObj == null) {
-                            // Print an empty value for scripting
-                            out.println(stringEscape.unescapeJava("\tReset Statistics Date:"));
-                        } else {
-                            Date firstExchangeTimestamp = (Date) resetTimestampObj;
-                            out.println(stringEscape.unescapeJava("\tReset Statistics Date: " + format.format(firstExchangeTimestamp)));
-                        }
+            // Test for null to see if a any exchanges have been processed first to avoid NPE
+            if (route.getResetTimestamp() == null) {
+                // Print an empty value for scripting
+                out.println(stringEscape.unescapeJava("\tReset Statistics Date:"));
+            } else {
+                Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getResetTimestamp());
+                String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
+                out.println(stringEscape.unescapeJava("\tReset Statistics Date: " + text));
+            }
 
-                        // Test for null to see if a any exchanges have been processed first to avoid NPE
-                        Object firstExchangeTimestampObj = mBeanServer.getAttribute(routeMBean, "FirstExchangeCompletedTimestamp");
-                        if (firstExchangeTimestampObj == null) {
-                            // Print an empty value for scripting
-                            out.println(stringEscape.unescapeJava("\tFirst Exchange Date:"));
-                        } else {
-                            Date firstExchangeTimestamp = (Date) firstExchangeTimestampObj;
-                            out.println(stringEscape.unescapeJava("\tFirst Exchange Date: " + format.format(firstExchangeTimestamp)));
-                        }
+            // Test for null to see if a any exchanges have been processed first to avoid NPE
+            if (route.getFirstExchangeCompletedTimestamp() == null) {
+                // Print an empty value for scripting
+                out.println(stringEscape.unescapeJava("\tFirst Exchange Date:"));
+            } else {
+                Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getFirstExchangeCompletedTimestamp());
+                String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
+                out.println(stringEscape.unescapeJava("\tFirst Exchange Date: " + text));
+            }
 
-                        // Again, check for null to avoid NPE
-                        Object lastExchangeCompletedTimestampObj = mBeanServer.getAttribute(routeMBean, "LastExchangeCompletedTimestamp");
-                        if (lastExchangeCompletedTimestampObj == null) {
-                            // Print an empty value for scripting
-                            out.println(stringEscape.unescapeJava("\tLast Exchange Completed Date:"));
-                        } else {
-                            Date lastExchangeCompletedTimestamp = (Date) lastExchangeCompletedTimestampObj;
-                            out.println(stringEscape.unescapeJava("\tLast Exchange Completed Date: " + format.format(lastExchangeCompletedTimestamp)));
-                        }
-                    }
-                }
+            // Test for null to see if a any exchanges have been processed first to avoid NPE
+            if (route.getLastExchangeCompletedTimestamp() == null) {
+                // Print an empty value for scripting
+                out.println(stringEscape.unescapeJava("\tLast Exchange Date:"));
             } else {
-                out.println("");
-                out.println(stringEscape.unescapeJava("\u001B[31mJMX Agent of Camel is not reachable. Maybe it has been disabled on the Camel context"));
-                out.println(stringEscape.unescapeJava("In consequence, some statistics are not available.\u001B[0m"));
+                Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getLastExchangeCompletedTimestamp());
+                String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
+                out.println(stringEscape.unescapeJava("\tLast Exchange Date: " + text));
             }
 
             out.println("");
-            String xml = camelController.getRouteModelAsXml(camelRoute.getId(), camelRoute.getRouteContext().getCamelContext().getName());
+            xml = camelController.getRouteModelAsXml(camelRoute.getId(), camelRoute.getRouteContext().getCamelContext().getName());
             if (xml != null) {
                 out.println(stringEscape.unescapeJava("\u001B[1mDefinition\u001B[0m"));
                 out.println(stringEscape.unescapeJava(xml));

http://git-wip-us.apache.org/repos/asf/camel/blob/83900763/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteProfileCommand.java
----------------------------------------------------------------------
diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteProfileCommand.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteProfileCommand.java
index cb746ec..c086122 100644
--- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteProfileCommand.java
+++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteProfileCommand.java
@@ -18,15 +18,11 @@ package org.apache.camel.commands;
 
 import java.io.PrintStream;
 import java.io.StringReader;
-import java.util.Set;
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Unmarshaller;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Route;
-import org.apache.camel.spi.ManagementAgent;
 import org.apache.camel.util.ProcessorStatDump;
 import org.apache.camel.util.RouteStatDump;
 
@@ -54,6 +50,7 @@ public class RouteProfileCommand extends AbstractRouteCommand {
 
     @Override
     public void executeOnRoute(CamelController camelController, CamelContext camelContext, Route camelRoute, PrintStream out, PrintStream err) throws Exception {
+
         JAXBContext context = JAXBContext.newInstance(RouteStatDump.class);
         Unmarshaller unmarshaller = context.createUnmarshaller();
 
@@ -65,36 +62,20 @@ public class RouteProfileCommand extends AbstractRouteCommand {
             System.out.println(String.format(HEADER_FORMAT, "Id", "Count", "Last (ms)", "Delta (ms)", "Mean (ms)", "Min (ms)", "Max (ms)", "Total (ms)", "Self (ms)"));
         }
 
-        ManagementAgent agent = camelContext.getManagementStrategy().getManagementAgent();
-        if (agent != null) {
-            MBeanServer mBeanServer = agent.getMBeanServer();
-            Set<ObjectName> set = mBeanServer.queryNames(new ObjectName(agent.getMBeanObjectDomainName() + ":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())) {
-
-                    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));
+        String xml = camelController.getRouteStatsAsXml(camelRoute.getId(), camelContext.getName(), true, true);
+        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(), route.getTotalProcessingTime(), route.getSelfProcessingTime()));
+        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(), route.getTotalProcessingTime(), route.getSelfProcessingTime()));
 
-                    for (ProcessorStatDump ps : route.getProcessorStats()) {
-                        // the self time is the total time of the processor itself
-                        long 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(), ps.getAccumulatedProcessingTime(), selfTime));
-                    }
-                }
-            }
-        } else {
-            System.out.println("");
-            System.out.println(stringEscape.unescapeJava("\u001B[31mJMX Agent of Camel is not reachable. Maybe it has been disabled on the Camel context"));
-            System.out.println(stringEscape.unescapeJava("In consequence, profile are not available.\u001B[0m"));
+        for (ProcessorStatDump ps : route.getProcessorStats()) {
+            // the self time is the total time of the processor itself
+            long 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(), ps.getAccumulatedProcessingTime(), selfTime));
         }
 
         // we want to group routes from the same context in the same table