Search in sources :

Example 1 with ManagedRouteMBean

use of org.apache.camel.api.management.mbean.ManagedRouteMBean in project camel by apache.

the class ManagedRouteStopUsingMBeanAPITest method testStopRoute.

public void testStopRoute() throws Exception {
    // JMX tests dont work well on AIX CI servers (hangs them)
    if (isPlatform("aix")) {
        return;
    }
    // fire a message to get it running
    getMockEndpoint("mock:result").expectedMessageCount(1);
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    MBeanServer mbeanServer = getMBeanServer();
    Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=routes,*"), null);
    assertEquals(1, set.size());
    ObjectName on = set.iterator().next();
    ManagedRouteMBean mbean = context.getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedRouteMBean.class);
    // the route has this starting endpoint uri
    assertEquals("direct://start", mbean.getEndpointUri());
    // should be started
    assertEquals("Should be started", ServiceStatus.Started.name(), mbean.getState());
    mbean.stop();
    // should be stopped
    assertEquals("Should be stopped", ServiceStatus.Stopped.name(), mbean.getState());
}
Also used : ManagedRouteMBean(org.apache.camel.api.management.mbean.ManagedRouteMBean) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 2 with ManagedRouteMBean

use of org.apache.camel.api.management.mbean.ManagedRouteMBean in project camel by apache.

the class CamelTestSupport method logCoverageSummary.

/**
     * Logs route coverage summary:
     * - which routes are uncovered
     * - what is the coverage of each processor in each route
     */
private void logCoverageSummary(ManagedCamelContextMBean managedCamelContext) throws Exception {
    StringBuilder builder = new StringBuilder("\nCoverage summary\n");
    int routes = managedCamelContext.getTotalRoutes();
    long contextExchangesTotal = managedCamelContext.getExchangesTotal();
    List<String> uncoveredRoutes = new ArrayList<>();
    StringBuilder routesSummary = new StringBuilder();
    routesSummary.append("\tProcessor coverage\n");
    MBeanServer server = context.getManagementStrategy().getManagementAgent().getMBeanServer();
    Map<String, List<ManagedProcessorMBean>> processorsForRoute = findProcessorsForEachRoute(server);
    // log processor coverage for each route
    for (Route route : context.getRoutes()) {
        ManagedRouteMBean managedRoute = context.getManagedRoute(route.getId(), ManagedRouteMBean.class);
        if (managedRoute.getExchangesTotal() == 0) {
            uncoveredRoutes.add(route.getId());
        }
        long routeCoveragePercentage = Math.round((double) managedRoute.getExchangesTotal() / contextExchangesTotal * 100);
        routesSummary.append("\t\tRoute ").append(route.getId()).append(" total: ").append(managedRoute.getExchangesTotal()).append(" (").append(routeCoveragePercentage).append("%)\n");
        if (server != null) {
            for (ManagedProcessorMBean managedProcessor : processorsForRoute.get(route.getId())) {
                String processorId = managedProcessor.getProcessorId();
                long processorExchangesTotal = managedProcessor.getExchangesTotal();
                long processorCoveragePercentage = Math.round((double) processorExchangesTotal / contextExchangesTotal * 100);
                routesSummary.append("\t\t\tProcessor ").append(processorId).append(" total: ").append(processorExchangesTotal).append(" (").append(processorCoveragePercentage).append("%)\n");
            }
        }
    }
    int used = routes - uncoveredRoutes.size();
    long contextPercentage = Math.round((double) used / routes * 100);
    builder.append("\tRoute coverage: ").append(used).append(" out of ").append(routes).append(" routes used (").append(contextPercentage).append("%)\n");
    builder.append("\t\tCamelContext (").append(managedCamelContext.getCamelId()).append(") total: ").append(contextExchangesTotal).append("\n");
    if (uncoveredRoutes.size() > 0) {
        builder.append("\t\tUncovered routes: ").append(uncoveredRoutes.stream().collect(Collectors.joining(", "))).append("\n");
    }
    builder.append(routesSummary);
    log.info(builder.toString());
}
Also used : ManagedRouteMBean(org.apache.camel.api.management.mbean.ManagedRouteMBean) ArrayList(java.util.ArrayList) ManagedProcessorMBean(org.apache.camel.api.management.mbean.ManagedProcessorMBean) List(java.util.List) ArrayList(java.util.ArrayList) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Route(org.apache.camel.Route) MBeanServer(javax.management.MBeanServer)

Example 3 with ManagedRouteMBean

use of org.apache.camel.api.management.mbean.ManagedRouteMBean in project camel by apache.

the class CdiOsgiIT method testExchangesCompleted.

@Test
public void testExchangesCompleted() throws Exception {
    ManagedRouteMBean route = context.getManagedRoute(context.getRoute("consumer-route").getId(), ManagedRouteMBean.class);
    assertThat("Number of exchanges completed is incorrect!", route.getExchangesCompleted(), equalTo(1L));
}
Also used : ManagedRouteMBean(org.apache.camel.api.management.mbean.ManagedRouteMBean) Test(org.junit.Test)

Example 4 with ManagedRouteMBean

use of org.apache.camel.api.management.mbean.ManagedRouteMBean in project camel by apache.

the class AbstractLocalCamelController method getRoutes.

public List<Map<String, String>> getRoutes(String camelContextName, String filter) throws Exception {
    List<Map<String, String>> answer = new ArrayList<Map<String, String>>();
    if (camelContextName != null) {
        CamelContext context = this.getLocalCamelContext(camelContextName);
        if (context != null) {
            for (Route route : context.getRoutes()) {
                if (filter == null || route.getId().matches(filter)) {
                    Map<String, String> row = new LinkedHashMap<String, String>();
                    row.put("camelContextName", context.getName());
                    row.put("routeId", route.getId());
                    row.put("state", getRouteState(route));
                    row.put("uptime", route.getUptime());
                    ManagedRouteMBean mr = context.getManagedRoute(route.getId(), ManagedRouteMBean.class);
                    if (mr != null) {
                        row.put("exchangesTotal", "" + mr.getExchangesTotal());
                        row.put("exchangesInflight", "" + mr.getExchangesInflight());
                        row.put("exchangesFailed", "" + mr.getExchangesFailed());
                    } else {
                        row.put("exchangesTotal", "0");
                        row.put("exchangesInflight", "0");
                        row.put("exchangesFailed", "0");
                    }
                    answer.add(row);
                }
            }
        }
    } else {
        List<Map<String, String>> camelContexts = this.getCamelContexts();
        for (Map<String, String> row : camelContexts) {
            List<Map<String, String>> routes = getRoutes(row.get("name"), filter);
            answer.addAll(routes);
        }
    }
    // sort the list
    Collections.sort(answer, new Comparator<Map<String, String>>() {

        @Override
        public int compare(Map<String, String> o1, Map<String, String> o2) {
            // group by camel context first, then by route name
            String c1 = o1.get("camelContextName");
            String c2 = o2.get("camelContextName");
            int answer = c1.compareTo(c2);
            if (answer == 0) {
                // okay from same camel context, then sort by route id
                answer = o1.get("routeId").compareTo(o2.get("routeId"));
            }
            return answer;
        }
    });
    return answer;
}
Also used : CamelContext(org.apache.camel.CamelContext) ManagedRouteMBean(org.apache.camel.api.management.mbean.ManagedRouteMBean) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Route(org.apache.camel.Route) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with ManagedRouteMBean

use of org.apache.camel.api.management.mbean.ManagedRouteMBean in project camel by apache.

the class ManagedCamelContext method dumpRoutesStatsAsXml.

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(jmxDomain + ":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(jmxDomain + ":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);
            }
        }
        processors.sort(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();
}
Also used : ManagedRouteMBean(org.apache.camel.api.management.mbean.ManagedRouteMBean) ArrayList(java.util.ArrayList) ManagedProcessorMBean(org.apache.camel.api.management.mbean.ManagedProcessorMBean) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Aggregations

ManagedRouteMBean (org.apache.camel.api.management.mbean.ManagedRouteMBean)6 ArrayList (java.util.ArrayList)3 MBeanServer (javax.management.MBeanServer)3 ManagedProcessorMBean (org.apache.camel.api.management.mbean.ManagedProcessorMBean)3 ObjectName (javax.management.ObjectName)2 Route (org.apache.camel.Route)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Stack (java.util.Stack)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 CamelContext (org.apache.camel.CamelContext)1 Endpoint (org.apache.camel.Endpoint)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 Test (org.junit.Test)1 Document (org.w3c.dom.Document)1