Search in sources :

Example 1 with ManagementAgent

use of org.apache.camel.spi.ManagementAgent in project camel by apache.

the class MetricsMessageHistoryService method doStart.

@Override
protected void doStart() throws Exception {
    if (metricsRegistry == null) {
        Registry camelRegistry = getCamelContext().getRegistry();
        metricsRegistry = camelRegistry.lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class);
        // create a new metricsRegistry by default
        if (metricsRegistry == null) {
            metricsRegistry = new MetricRegistry();
        }
    }
    if (useJmx) {
        ManagementAgent agent = getCamelContext().getManagementStrategy().getManagementAgent();
        if (agent != null) {
            MBeanServer server = agent.getMBeanServer();
            if (server != null) {
                reporter = JmxReporter.forRegistry(metricsRegistry).registerWith(server).inDomain(jmxDomain).build();
                reporter.start();
            }
        } else {
            throw new IllegalStateException("CamelContext has not enabled JMX");
        }
    }
    // json mapper
    this.mapper = new ObjectMapper().registerModule(new MetricsModule(getRateUnit(), getDurationUnit(), false));
    if (getRateUnit() == TimeUnit.SECONDS && getDurationUnit() == TimeUnit.SECONDS) {
        // they both use same units so reuse
        this.secondsMapper = this.mapper;
    } else {
        this.secondsMapper = new ObjectMapper().registerModule(new MetricsModule(TimeUnit.SECONDS, TimeUnit.SECONDS, false));
    }
}
Also used : ManagementAgent(org.apache.camel.spi.ManagementAgent) MetricRegistry(com.codahale.metrics.MetricRegistry) MetricsModule(com.codahale.metrics.json.MetricsModule) MetricRegistry(com.codahale.metrics.MetricRegistry) Registry(org.apache.camel.spi.Registry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MBeanServer(javax.management.MBeanServer)

Example 2 with ManagementAgent

use of org.apache.camel.spi.ManagementAgent in project camel by apache.

the class JMXAgentPropertiesTest method testEnableUseHostIPAddress.

public void testEnableUseHostIPAddress() throws Exception {
    CamelContext ctx = createCamelContext();
    ManagementAgent agent = ctx.getManagementStrategy().getManagementAgent();
    agent.start();
    assertTrue(agent.getUseHostIPAddress());
}
Also used : CamelContext(org.apache.camel.CamelContext) ManagementAgent(org.apache.camel.spi.ManagementAgent)

Example 3 with ManagementAgent

use of org.apache.camel.spi.ManagementAgent in project camel by apache.

the class AbstractLocalCamelController method getRouteStatsAsXml.

@Override
public String getRouteStatsAsXml(String routeId, String camelContextName, boolean fullStats, boolean includeProcessors) throws Exception {
    CamelContext context = this.getLocalCamelContext(camelContextName);
    if (context == null) {
        return null;
    }
    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;
            }
        }
    }
    return null;
}
Also used : CamelContext(org.apache.camel.CamelContext) ManagementAgent(org.apache.camel.spi.ManagementAgent) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 4 with ManagementAgent

use of org.apache.camel.spi.ManagementAgent in project camel by apache.

the class AbstractLocalCamelController method getCamelContextStatsAsXml.

public String getCamelContextStatsAsXml(String camelContextName, boolean fullStats, boolean includeProcessors) throws Exception {
    CamelContext context = this.getLocalCamelContext(camelContextName);
    if (context == null) {
        return null;
    }
    ManagementAgent agent = context.getManagementStrategy().getManagementAgent();
    if (agent != null) {
        MBeanServer mBeanServer = agent.getMBeanServer();
        ObjectName query = ObjectName.getInstance(agent.getMBeanObjectDomainName() + ":type=context,*");
        Set<ObjectName> set = mBeanServer.queryNames(query, null);
        for (ObjectName contextMBean : set) {
            String camelId = (String) mBeanServer.getAttribute(contextMBean, "CamelId");
            if (camelId != null && camelId.equals(context.getName())) {
                String xml = (String) mBeanServer.invoke(contextMBean, "dumpRoutesStatsAsXml", new Object[] { fullStats, includeProcessors }, new String[] { "boolean", "boolean" });
                return xml;
            }
        }
    }
    return null;
}
Also used : CamelContext(org.apache.camel.CamelContext) ManagementAgent(org.apache.camel.spi.ManagementAgent) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 5 with ManagementAgent

use of org.apache.camel.spi.ManagementAgent in project camel by apache.

the class ManagedResourceTest method testManagedResource.

@Test
public void testManagedResource() throws Exception {
    // JMX tests dont work well on AIX CI servers (hangs them)
    if (isPlatform("aix")) {
        return;
    }
    final ManagementAgent managementAgent = context.getManagementStrategy().getManagementAgent();
    TestCase.assertNotNull(managementAgent);
    final MBeanServer mBeanServer = managementAgent.getMBeanServer();
    TestCase.assertNotNull(mBeanServer);
    final String mBeanServerDefaultDomain = managementAgent.getMBeanServerDefaultDomain();
    TestCase.assertEquals("org.apache.camel", mBeanServerDefaultDomain);
    final String managementName = context.getManagementName();
    TestCase.assertNotNull("CamelContext should have a management name if JMX is enabled", managementName);
    LOG.info("managementName = {}", managementName);
    // Get the Camel Context MBean
    ObjectName onContext = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=" + managementName + ",type=context,name=\"" + context.getName() + "\"");
    TestCase.assertTrue("Should be registered", mBeanServer.isRegistered(onContext));
    // Get myManagedBean
    ObjectName onManagedBean = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=" + managementName + ",type=processors,name=\"myManagedBean\"");
    LOG.info("Canonical Name = {}", onManagedBean.getCanonicalName());
    TestCase.assertTrue("Should be registered", mBeanServer.isRegistered(onManagedBean));
    // Send a couple of messages to get some route statistics
    template.sendBody("direct:start", "Hello Camel");
    template.sendBody("direct:start", "Camel Rocks!");
    // Get MBean attribute
    int camelsSeenCount = (Integer) mBeanServer.getAttribute(onManagedBean, "CamelsSeenCount");
    TestCase.assertEquals(2, camelsSeenCount);
    // Stop the route via JMX
    mBeanServer.invoke(onManagedBean, "resetCamelsSeenCount", null, null);
    camelsSeenCount = (Integer) mBeanServer.getAttribute(onManagedBean, "CamelsSeenCount");
    TestCase.assertEquals(0, camelsSeenCount);
    String camelId = (String) mBeanServer.getAttribute(onManagedBean, "CamelId");
    assertEquals(context.getName(), camelId);
    String state = (String) mBeanServer.getAttribute(onManagedBean, "State");
    assertEquals("Started", state);
    String fqn = (String) mBeanServer.getAttribute(onManagedBean, "BeanClassName");
    assertEquals(MyManagedBean.class.getCanonicalName(), fqn);
}
Also used : ManagementAgent(org.apache.camel.spi.ManagementAgent) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

ManagementAgent (org.apache.camel.spi.ManagementAgent)14 MBeanServer (javax.management.MBeanServer)9 CamelContext (org.apache.camel.CamelContext)7 ObjectName (javax.management.ObjectName)6 Test (org.junit.Test)4 MetricRegistry (com.codahale.metrics.MetricRegistry)2 MetricsModule (com.codahale.metrics.json.MetricsModule)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 Registry (org.apache.camel.spi.Registry)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ObjectInstance (javax.management.ObjectInstance)1 CompositeData (javax.management.openmbean.CompositeData)1 TabularData (javax.management.openmbean.TabularData)1 DefaultManagementAgent (org.apache.camel.management.DefaultManagementAgent)1 ManagementStrategy (org.apache.camel.spi.ManagementStrategy)1