Search in sources :

Example 16 with ObjectInstance

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

the class ConnectorServerFactoryBeanTests method registerWithMBeanServer.

@Test
public void registerWithMBeanServer() throws Exception {
    //Added a brief snooze here - seems to fix occasional
    //java.net.BindException: Address already in use errors
    Thread.sleep(1);
    ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean();
    bean.setObjectName(OBJECT_NAME);
    bean.afterPropertiesSet();
    try {
        // Try to get the connector bean.
        ObjectInstance instance = getServer().getObjectInstance(ObjectName.getInstance(OBJECT_NAME));
        assertNotNull("ObjectInstance should not be null", instance);
    } finally {
        bean.destroy();
    }
}
Also used : ObjectInstance(javax.management.ObjectInstance) Test(org.junit.Test)

Example 17 with ObjectInstance

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

the class JCacheCacheStatisticsProvider method getObjectName.

@Override
protected ObjectName getObjectName(JCacheCache cache) throws MalformedObjectNameException {
    ObjectName name = new ObjectName("javax.cache:type=CacheStatistics,Cache=" + cache.getName() + ",*");
    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 18 with ObjectInstance

use of javax.management.ObjectInstance in project camel by apache.

the class ThreadPoolTest method getMbeanCount.

private int getMbeanCount(final String name) throws MalformedObjectNameException {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectInstance> mbeans = mbs.queryMBeans(new ObjectName("org.apache.camel:type=threadpools,*"), null);
    LOGGER.debug("mbeans size: " + mbeans.size());
    int count = 0;
    for (ObjectInstance mbean : mbeans) {
        LOGGER.debug("mbean: {}", mbean);
        if (mbean.getObjectName().getKeyProperty("name").startsWith(name)) {
            count++;
        }
    }
    return count;
}
Also used : ObjectInstance(javax.management.ObjectInstance) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 19 with ObjectInstance

use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.

the class PreRegisterNameTest method main.

public static void main(String[] args) throws Exception {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    for (Class<?> c : new Class<?>[] { Spume.class, Thing.class, XSpume.class, XThing.class }) {
        for (ObjectName n : new ObjectName[] { null, new ObjectName("a:b=c") }) {
            System.out.println("Class " + c.getName() + " with name " + n + "...");
            ObjectName realName = new ObjectName("a:type=" + c.getName());
            Constructor<?> constr = c.getConstructor(ObjectName.class);
            Object mbean = constr.newInstance(realName);
            ObjectInstance oi;
            String what = "Registering MBean of type " + c.getName() + " under name " + "<" + n + ">: ";
            try {
                oi = mbs.registerMBean(mbean, n);
            } catch (Exception e) {
                e.printStackTrace();
                fail(what + " got " + e);
                continue;
            }
            ObjectName registeredName = oi.getObjectName();
            if (!registeredName.equals(realName))
                fail(what + " registered as " + registeredName);
            if (!mbs.isRegistered(realName)) {
                fail(what + " not registered as expected");
            }
            mbs.unregisterMBean(registeredName);
        }
    }
    System.err.flush();
    if (failures == 0)
        System.out.println("TEST PASSED");
    else
        throw new Exception("TEST FAILED: " + failure);
}
Also used : ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Example 20 with ObjectInstance

use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.

the class SupportedQueryTypesTest method run.

public void run(Map<String, Object> args) {
    int errorCount = 0;
    ObjectName on = null;
    ObjectName serverDelegateObjectName = null;
    JMXConnectorServer cs = null;
    JMXConnector cc = null;
    System.out.println("SupportedQueryTypesTest::run: Start");
    try {
        // JMX MbeanServer used inside single VM as if remote.
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
        cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        cc = JMXConnectorFactory.connect(addr);
        mbsc = cc.getMBeanServerConnection();
        // Create and register the ServerDelegate MBean on the remote MBeanServer
        String serverDelegateClassName = ServerDelegate.class.getName();
        serverDelegateObjectName = new ObjectName("defaultDomain:class=" + serverDelegateClassName);
        mbsc.createMBean(serverDelegateClassName, serverDelegateObjectName);
        // Retrieve the MBean class name
        mbeanClassName = (String) args.get("-mbeanClassName");
        on = new ObjectName("defaultDomain:class=" + mbeanClassName);
        // Create and register the MBean on the remote MBeanServer
        System.out.println("SupportedQueryTypesTest::run: CREATE " + mbeanClassName + " on the remote MBeanServer with name " + on);
        mbsc.createMBean(mbeanClassName, on);
        // Create a QueryFactory and setup which query we'll use.
        QueryFactory queries = new QueryFactory(mbeanClassName);
        queries.buildQueries();
        int maxIndex = queries.getSize();
        int minIndex = 1;
        // Create a reference Set<ObjectName> to check later on
        // the queryNames() results
        Set<ObjectName> referenceNameSet = new HashSet<ObjectName>();
        referenceNameSet.add(on);
        // Create a reference Set<ObjectInstance> to check later on
        // the queryMBeans() results
        ObjectInstance oi = new ObjectInstance(on, mbeanClassName);
        Set<ObjectInstance> referenceInstanceSet = new HashSet<ObjectInstance>();
        referenceInstanceSet.add(oi);
        // Perform the queryNames and queryMBeans requests
        for (int i = minIndex; i <= maxIndex; i++) {
            QueryExp query = queries.getQuery(i);
            System.out.println("----");
            System.out.println("SupportedQueryTypesTest::run: Query # " + i);
            System.out.println("query " + query);
            errorCount += doQueryNames(query, referenceNameSet);
            errorCount += doQueryMBeans(query, referenceInstanceSet);
        }
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        errorCount++;
    } finally {
        // Do unregister the MBean
        try {
            if (mbsc.isRegistered(on)) {
                mbsc.unregisterMBean(on);
            }
            if (mbsc.isRegistered(serverDelegateObjectName)) {
                mbsc.unregisterMBean(serverDelegateObjectName);
            }
        } catch (Exception e) {
            Utils.printThrowable(e, true);
            errorCount++;
        }
        try {
            // Close JMX Connector Client
            cc.close();
            // Stop connertor server
            cs.stop();
        } catch (Exception e) {
            Utils.printThrowable(e, true);
            errorCount++;
        }
    }
    System.out.println("");
    System.out.println("SupportedQueryTypesTest::run: Done");
    // Handle result
    if (errorCount == 0) {
        System.out.println("SupportedQueryTypesTest::run: (OK)");
    } else {
        String message = "SupportedQueryTypesTest::run: (ERROR) Got " + +errorCount + " error(s)";
        System.out.println(message);
        throw new RuntimeException(message);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) QueryExp(javax.management.QueryExp) ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) MBeanServer(javax.management.MBeanServer) HashSet(java.util.HashSet)

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