Search in sources :

Example 11 with ManagementAgent

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

the class DefaultManagementLifecycleStrategy method shouldRegister.

/**
     * Whether or not to register the mbean.
     * <p/>
     * The {@link ManagementAgent} has options which controls when to register.
     * This allows us to only register mbeans accordingly. For example by default any
     * dynamic endpoints is not registered. This avoids to register excessive mbeans, which
     * most often is not desired.
     *
     * @param service the object to register
     * @param route   an optional route the mbean is associated with, can be <tt>null</tt>
     * @return <tt>true</tt> to register, <tt>false</tt> to skip registering
     */
protected boolean shouldRegister(Object service, Route route) {
    // the agent hasn't been started
    if (!initialized) {
        return false;
    }
    LOG.trace("Checking whether to register {} from route: {}", service, route);
    ManagementAgent agent = getManagementStrategy().getManagementAgent();
    if (agent == null) {
        // do not register if no agent
        return false;
    }
    // always register if we are starting CamelContext
    if (getCamelContext().getStatus().isStarting()) {
        return true;
    }
    // always register if we are setting up routes
    if (getCamelContext().isSetupRoutes()) {
        return true;
    }
    // register if always is enabled
    if (agent.getRegisterAlways()) {
        return true;
    }
    // is it a known route then always accept
    if (route != null && knowRouteIds.contains(route.getId())) {
        return true;
    }
    // only register if we are starting a new route, and current thread is in starting routes mode
    if (agent.getRegisterNewRoutes()) {
        // which is kept as state on the camel context
        return getCamelContext().isStartingRoutes();
    }
    return false;
}
Also used : ManagementAgent(org.apache.camel.spi.ManagementAgent)

Example 12 with ManagementAgent

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

the class DefaultManagementAgentMockTest method testObjectNameModification.

@Test
public void testObjectNameModification() throws JMException {
    MBeanServer mbeanServer = createStrictMock(MBeanServer.class);
    ObjectInstance instance = createStrictMock(ObjectInstance.class);
    ManagementAgent agent = new DefaultManagementAgent();
    agent.setMBeanServer(mbeanServer);
    Object object = "object";
    ObjectName sourceObjectName = new ObjectName("domain", "key", "value");
    ObjectName registeredObjectName = new ObjectName("domain", "key", "otherValue");
    // Register MBean and return different ObjectName
    expect(mbeanServer.isRegistered(sourceObjectName)).andReturn(false);
    expect(mbeanServer.registerMBean(object, sourceObjectName)).andReturn(instance);
    expect(instance.getObjectName()).andReturn(registeredObjectName);
    expect(mbeanServer.isRegistered(registeredObjectName)).andReturn(true);
    replay(mbeanServer, instance);
    agent.register(object, sourceObjectName);
    assertTrue(agent.isRegistered(sourceObjectName));
    verify(mbeanServer, instance);
    reset(mbeanServer, instance);
    // ... and unregister it again
    expect(mbeanServer.isRegistered(registeredObjectName)).andReturn(true);
    mbeanServer.unregisterMBean(registeredObjectName);
    expect(mbeanServer.isRegistered(sourceObjectName)).andReturn(false);
    replay(mbeanServer);
    agent.unregister(sourceObjectName);
    assertFalse(agent.isRegistered(sourceObjectName));
    verify(mbeanServer);
}
Also used : ManagementAgent(org.apache.camel.spi.ManagementAgent) ObjectInstance(javax.management.ObjectInstance) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 13 with ManagementAgent

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

the class DefaultManagementAgentMockTest method shouldUseHostNameWhenFlagisFalse.

@Test
public void shouldUseHostNameWhenFlagisFalse() throws Exception {
    System.setProperty(JmxSystemPropertyKeys.USE_HOST_IP_ADDRESS, "false");
    System.setProperty(JmxSystemPropertyKeys.CREATE_CONNECTOR, "true");
    CamelContext ctx = new DefaultCamelContext();
    ManagementAgent agent = new DefaultManagementAgent(ctx);
    agent.start();
    assertFalse(agent.getUseHostIPAddress());
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ManagementAgent(org.apache.camel.spi.ManagementAgent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 14 with ManagementAgent

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

the class DefaultManagementAgentMockTest method testShouldUseHostIPAddressWhenFlagisTrue.

@Test
public void testShouldUseHostIPAddressWhenFlagisTrue() throws Exception {
    System.setProperty(JmxSystemPropertyKeys.USE_HOST_IP_ADDRESS, "true");
    System.setProperty(JmxSystemPropertyKeys.CREATE_CONNECTOR, "true");
    CamelContext ctx = new DefaultCamelContext();
    ManagementAgent agent = new DefaultManagementAgent(ctx);
    agent.start();
    assertTrue(agent.getUseHostIPAddress());
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ManagementAgent(org.apache.camel.spi.ManagementAgent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) 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