Search in sources :

Example 46 with ObjectInstance

use of javax.management.ObjectInstance 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 47 with ObjectInstance

use of javax.management.ObjectInstance in project presto by prestodb.

the class RebindSafeMBeanServer method registerMBean.

/**
     * Delegates to the wrapped mbean server, but if a mbean is already registered
     * with the specified name, the existing instance is returned.
     */
@Override
public ObjectInstance registerMBean(Object object, ObjectName name) throws MBeanRegistrationException, NotCompliantMBeanException {
    while (true) {
        try {
            // try to register the mbean
            return mbeanServer.registerMBean(object, name);
        } catch (InstanceAlreadyExistsException ignored) {
        }
        try {
            // a mbean is already installed, try to return the already registered instance
            ObjectInstance objectInstance = mbeanServer.getObjectInstance(name);
            log.debug("%s already bound to %s", name, objectInstance);
            return objectInstance;
        } catch (InstanceNotFoundException ignored) {
        // the mbean was removed before we could get the reference
        // start the whole process over again
        }
    }
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ObjectInstance(javax.management.ObjectInstance)

Example 48 with ObjectInstance

use of javax.management.ObjectInstance in project presto by prestodb.

the class RebindSafeMBeanServer method registerMBean.

/**
     * Delegates to the wrapped mbean server, but if a mbean is already registered
     * with the specified name, the existing instance is returned.
     */
@Override
public ObjectInstance registerMBean(Object object, ObjectName name) throws MBeanRegistrationException, NotCompliantMBeanException {
    while (true) {
        try {
            // try to register the mbean
            return mbeanServer.registerMBean(object, name);
        } catch (InstanceAlreadyExistsException ignored) {
        }
        try {
            // a mbean is already installed, try to return the already registered instance
            ObjectInstance objectInstance = mbeanServer.getObjectInstance(name);
            log.debug("%s already bound to %s", name, objectInstance);
            return objectInstance;
        } catch (InstanceNotFoundException ignored) {
        // the mbean was removed before we could get the reference
        // start the whole process over again
        }
    }
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ObjectInstance(javax.management.ObjectInstance)

Example 49 with ObjectInstance

use of javax.management.ObjectInstance in project spring-boot by spring-projects.

the class InfinispanCacheStatisticsProvider method getObjectName.

@Override
protected ObjectName getObjectName(SpringCache cache) throws MalformedObjectNameException {
    ObjectName name = new ObjectName("org.infinispan:component=Statistics,type=Cache,name=\"" + cache.getName() + "(local)\",*");
    Set<ObjectInstance> instances = getMBeanServer().queryMBeans(name, null);
    if (instances.size() == 1) {
        return instances.iterator().next().getObjectName();
    }
    // None or more than one
    return null;
}
Also used : ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName)

Example 50 with ObjectInstance

use of javax.management.ObjectInstance in project spring-boot by spring-projects.

the class SpringApplicationAdminJmxAutoConfigurationTests method registeredWithProperty.

@Test
public void registeredWithProperty() throws Exception {
    load(ENABLE_ADMIN_PROP);
    ObjectName objectName = createDefaultObjectName();
    ObjectInstance objectInstance = this.mBeanServer.getObjectInstance(objectName);
    assertThat(objectInstance).as("Lifecycle bean should have been registered").isNotNull();
}
Also used : ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

ObjectInstance (javax.management.ObjectInstance)75 ObjectName (javax.management.ObjectName)36 Test (org.junit.Test)27 InstanceNotFoundException (javax.management.InstanceNotFoundException)15 MBeanServer (javax.management.MBeanServer)12 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)10 JmxResultProcessor (com.googlecode.jmxtrans.model.JmxResultProcessor)9 Result (com.googlecode.jmxtrans.model.Result)9 HashSet (java.util.HashSet)7 Attribute (javax.management.Attribute)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 AttributeNotFoundException (javax.management.AttributeNotFoundException)5 MBeanRegistrationException (javax.management.MBeanRegistrationException)5 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)5 HashMap (java.util.HashMap)4 AttributeList (javax.management.AttributeList)4 MBeanException (javax.management.MBeanException)4 MBeanServerConnection (javax.management.MBeanServerConnection)4 ReflectionException (javax.management.ReflectionException)4