Search in sources :

Example 11 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 12 with ObjectInstance

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

the class MBeanRegistrationSupport method doRegister.

/**
	 * Actually register the MBean with the server. The behavior when encountering
	 * an existing MBean can be configured using {@link #setRegistrationPolicy}.
	 * @param mbean the MBean instance
	 * @param objectName the suggested ObjectName for the MBean
	 * @throws JMException if the registration failed
	 */
protected void doRegister(Object mbean, ObjectName objectName) throws JMException {
    ObjectName actualObjectName;
    synchronized (this.registeredBeans) {
        ObjectInstance registeredBean = null;
        try {
            registeredBean = this.server.registerMBean(mbean, objectName);
        } catch (InstanceAlreadyExistsException ex) {
            if (this.registrationPolicy == RegistrationPolicy.IGNORE_EXISTING) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Ignoring existing MBean at [" + objectName + "]");
                }
            } else if (this.registrationPolicy == RegistrationPolicy.REPLACE_EXISTING) {
                try {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Replacing existing MBean at [" + objectName + "]");
                    }
                    this.server.unregisterMBean(objectName);
                    registeredBean = this.server.registerMBean(mbean, objectName);
                } catch (InstanceNotFoundException ex2) {
                    logger.error("Unable to replace existing MBean at [" + objectName + "]", ex2);
                    throw ex;
                }
            } else {
                throw ex;
            }
        }
        // Track registration and notify listeners.
        actualObjectName = (registeredBean != null ? registeredBean.getObjectName() : null);
        if (actualObjectName == null) {
            actualObjectName = objectName;
        }
        this.registeredBeans.add(actualObjectName);
    }
    onRegister(actualObjectName, mbean);
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName)

Example 13 with ObjectInstance

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

the class MBeanExporterTests method testAutodetectMBeans.

@Test
public void testAutodetectMBeans() throws Exception {
    ConfigurableApplicationContext ctx = load("autodetectMBeans.xml");
    try {
        ctx.getBean("exporter");
        MBeanServer server = ctx.getBean("server", MBeanServer.class);
        ObjectInstance instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=true"));
        assertNotNull(instance);
        instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean2=true"));
        assertNotNull(instance);
        instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean3=true"));
        assertNotNull(instance);
    } finally {
        ctx.close();
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ObjectInstance(javax.management.ObjectInstance) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 14 with ObjectInstance

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

the class MBeanExporterTests method testRegisterIgnoreExisting.

@Test
public void testRegisterIgnoreExisting() throws Exception {
    ObjectName objectName = ObjectNameManager.getInstance(OBJECT_NAME);
    Person preRegistered = new Person();
    preRegistered.setName("Rob Harrop");
    server.registerMBean(preRegistered, objectName);
    Person springRegistered = new Person();
    springRegistered.setName("Sally Greenwood");
    String objectName2 = "spring:test=equalBean";
    Map<String, Object> beans = new HashMap<>();
    beans.put(objectName.toString(), springRegistered);
    beans.put(objectName2, springRegistered);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setRegistrationPolicy(RegistrationPolicy.IGNORE_EXISTING);
    start(exporter);
    ObjectInstance instance = server.getObjectInstance(objectName);
    assertNotNull(instance);
    ObjectInstance instance2 = server.getObjectInstance(new ObjectName(objectName2));
    assertNotNull(instance2);
    // should still be the first bean with name Rob Harrop
    assertEquals("Rob Harrop", server.getAttribute(objectName, "Name"));
}
Also used : HashMap(java.util.HashMap) ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 15 with ObjectInstance

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

the class MBeanExporterTests method testSelfNaming.

@Test
public void testSelfNaming() throws Exception {
    ObjectName objectName = ObjectNameManager.getInstance(OBJECT_NAME);
    SelfNamingTestBean testBean = new SelfNamingTestBean();
    testBean.setObjectName(objectName);
    Map<String, Object> beans = new HashMap<>();
    beans.put("foo", testBean);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    start(exporter);
    ObjectInstance instance = server.getObjectInstance(objectName);
    assertNotNull(instance);
}
Also used : HashMap(java.util.HashMap) 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