public String dumpRoutesStatsAsXml(boolean fullStats, boolean includeProcessors)
      throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.append("<camelContextStat")
        .append(String.format(" id=\"%s\" state=\"%s\"", getCamelId(), getState()));
    // use substring as we only want the attributes
    String stat = dumpStatsAsXml(fullStats);
    sb.append(" exchangesInflight=\"").append(getInflightExchanges()).append("\"");
    sb.append(" ").append(stat.substring(7, stat.length() - 2)).append(">\n");

    MBeanServer server = getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
    if (server != null) {
      // gather all the routes for this CamelContext, which requires JMX
      String prefix =
          getContext().getManagementStrategy().getManagementAgent().getIncludeHostName()
              ? "*/"
              : "";
      ObjectName query =
          ObjectName.getInstance(
              "org.apache.camel:context="
                  + prefix
                  + getContext().getManagementName()
                  + ",type=routes,*");
      Set<ObjectName> routes = server.queryNames(query, null);

      List<ManagedProcessorMBean> processors = new ArrayList<ManagedProcessorMBean>();
      if (includeProcessors) {
        // gather all the processors for this CamelContext, which requires JMX
        query =
            ObjectName.getInstance(
                "org.apache.camel:context="
                    + prefix
                    + getContext().getManagementName()
                    + ",type=processors,*");
        Set<ObjectName> names = server.queryNames(query, null);
        for (ObjectName on : names) {
          ManagedProcessorMBean processor =
              context
                  .getManagementStrategy()
                  .getManagementAgent()
                  .newProxyClient(on, ManagedProcessorMBean.class);
          processors.add(processor);
        }
      }
      Collections.sort(processors, new OrderProcessorMBeans());

      // loop the routes, and append the processor stats if needed
      sb.append("  <routeStats>\n");
      for (ObjectName on : routes) {
        ManagedRouteMBean route =
            context
                .getManagementStrategy()
                .getManagementAgent()
                .newProxyClient(on, ManagedRouteMBean.class);
        sb.append("    <routeStat")
            .append(String.format(" id=\"%s\" state=\"%s\"", route.getRouteId(), route.getState()));
        // use substring as we only want the attributes
        stat = route.dumpStatsAsXml(fullStats);
        sb.append(" exchangesInflight=\"").append(route.getExchangesInflight()).append("\"");
        sb.append(" ").append(stat.substring(7, stat.length() - 2)).append(">\n");

        // add processor details if needed
        if (includeProcessors) {
          sb.append("      <processorStats>\n");
          for (ManagedProcessorMBean processor : processors) {
            // the processor must belong to this route
            if (route.getRouteId().equals(processor.getRouteId())) {
              sb.append("        <processorStat")
                  .append(
                      String.format(
                          " id=\"%s\" index=\"%s\" state=\"%s\"",
                          processor.getProcessorId(), processor.getIndex(), processor.getState()));
              // use substring as we only want the attributes
              stat = processor.dumpStatsAsXml(fullStats);
              sb.append(" exchangesInflight=\"")
                  .append(processor.getExchangesInflight())
                  .append("\"");
              sb.append(" ").append(stat.substring(7)).append("\n");
            }
          }
          sb.append("      </processorStats>\n");
        }
        sb.append("    </routeStat>\n");
      }
      sb.append("  </routeStats>\n");
    }

    sb.append("</camelContextStat>");
    return sb.toString();
  }
 @Override
 public int compare(ManagedProcessorMBean o1, ManagedProcessorMBean o2) {
   return o1.getIndex().compareTo(o2.getIndex());
 }
Beispiel #3
0
  public String dumpRouteStatsAsXml(boolean fullStats, boolean includeProcessors) throws Exception {
    // in this logic we need to calculate the accumulated processing time for the processor in the
    // route
    // and hence why the logic is a bit more complicated to do this, as we need to calculate that
    // from
    // the bottom -> top of the route but this information is valuable for profiling routes
    StringBuilder sb = new StringBuilder();

    // need to calculate this value first, as we need that value for the route stat
    Long processorAccumulatedTime = 0L;

    // gather all the processors for this route, which requires JMX
    if (includeProcessors) {
      sb.append("  <processorStats>\n");
      MBeanServer server =
          getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
      if (server != null) {
        // get all the processor mbeans and sort them accordingly to their index
        String prefix =
            getContext().getManagementStrategy().getManagementAgent().getIncludeHostName()
                ? "*/"
                : "";
        ObjectName query =
            ObjectName.getInstance(
                "org.apache.camel:context="
                    + prefix
                    + getContext().getManagementName()
                    + ",type=processors,*");
        Set<ObjectName> names = server.queryNames(query, null);
        List<ManagedProcessorMBean> mps = new ArrayList<ManagedProcessorMBean>();
        for (ObjectName on : names) {
          ManagedProcessorMBean processor =
              MBeanServerInvocationHandler.newProxyInstance(
                  server, on, ManagedProcessorMBean.class, true);

          // the processor must belong to this route
          if (getRouteId().equals(processor.getRouteId())) {
            mps.add(processor);
          }
        }
        Collections.sort(mps, new OrderProcessorMBeans());

        // walk the processors in reverse order, and calculate the accumulated total time
        Map<String, Long> accumulatedTimes = new HashMap<String, Long>();
        Collections.reverse(mps);
        for (ManagedProcessorMBean processor : mps) {
          processorAccumulatedTime += processor.getTotalProcessingTime();
          accumulatedTimes.put(processor.getProcessorId(), processorAccumulatedTime);
        }
        // and reverse back again
        Collections.reverse(mps);

        // and now add the sorted list of processors to the xml output
        for (ManagedProcessorMBean processor : mps) {
          sb.append("    <processorStat")
              .append(
                  String.format(
                      " id=\"%s\" index=\"%s\"", processor.getProcessorId(), processor.getIndex()));
          // do we have an accumulated time then append that
          Long accTime = accumulatedTimes.get(processor.getProcessorId());
          if (accTime != null) {
            sb.append(" accumulatedProcessingTime=\"").append(accTime).append("\"");
          }
          // use substring as we only want the attributes
          sb.append(" ").append(processor.dumpStatsAsXml(fullStats).substring(7)).append("\n");
        }
      }
      sb.append("  </processorStats>\n");
    }

    // route self time is route total - processor accumulated total)
    long routeSelfTime = getTotalProcessingTime() - processorAccumulatedTime;
    if (routeSelfTime < 0) {
      // ensure we don't calculate that as negative
      routeSelfTime = 0;
    }

    StringBuilder answer = new StringBuilder();
    answer.append("<routeStat").append(String.format(" id=\"%s\"", route.getId()));
    // use substring as we only want the attributes
    String stat = dumpStatsAsXml(fullStats);
    answer.append(" selfProcessingTime=\"").append(routeSelfTime).append("\"");
    answer.append(" ").append(stat.substring(7, stat.length() - 2)).append(">\n");

    if (includeProcessors) {
      answer.append(sb);
    }

    answer.append("</routeStat>");
    return answer.toString();
  }