Search in sources :

Example 36 with MBeanServer

use of javax.management.MBeanServer in project opennms by OpenNMS.

the class PlatformMBeanServerConnector method createConnection.

public JmxServerConnectionWrapper createConnection() {
    final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
    final JmxServerConnectionWrapper jmxConnectionWrapper = new JmxServerConnectionWrapper() {

        @Override
        public MBeanServerConnection getMBeanServerConnection() {
            return platformMBeanServer;
        }

        @Override
        public void close() {
        }
    };
    return jmxConnectionWrapper;
}
Also used : JmxServerConnectionWrapper(org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper) MBeanServer(javax.management.MBeanServer)

Example 37 with MBeanServer

use of javax.management.MBeanServer 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 38 with MBeanServer

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

the class RandomMXBeanTest method main.

public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName name = new ObjectName("a:b=c");
    StupidMXBean stupid = new StupidImpl();
    mbs.registerMBean(stupid, name);
    ObjectName referName = new ObjectName("a:c=d");
    mbs.registerMBean(new ReferImpl(stupid), referName);
    System.out.println(mbs.getMBeanInfo(name));
    StupidMXBean stupid2 = (StupidMXBean) Proxy.newProxyInstance(StupidMXBean.class.getClassLoader(), new Class<?>[] { StupidMXBean.class }, new WrapInvocationHandler(stupid));
    ObjectName stupidName2 = new ObjectName("a:d=e");
    mbs.registerMBean(stupid2, stupidName2);
    Field zero = StupidMXBean.class.getField("ZERO");
    System.out.println("Zero field = " + zero.get(null));
    test(mbs, MerlinMXBean.class);
    test(mbs, TigerMXBean.class);
    StupidMXBean proxy = JMX.newMXBeanProxy(mbs, name, StupidMXBean.class);
    System.out.println("Zero = " + proxy.getZero());
    System.out.println("One = " + proxy.identity(1));
    ReferMXBean referProxy = JMX.newMXBeanProxy(mbs, referName, ReferMXBean.class);
    StupidMXBean stupidProxy2 = referProxy.getStupid();
    System.out.println("Same proxy: " + (proxy == stupidProxy2));
    Method[] methods = StupidMXBean.class.getMethods();
    for (Method method : methods) {
        if (method.getParameterTypes().length == 0)
            method.invoke(proxy, new Object[0]);
    }
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 39 with MBeanServer

use of javax.management.MBeanServer 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)

Example 40 with MBeanServer

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

the class SameObjectTwoNamesTest method main.

public static void main(String[] args) throws Exception {
    boolean expectException = (System.getProperty("jmx.mxbean.multiname") == null);
    try {
        ObjectName objectName1 = new ObjectName("test:index=1");
        ObjectName objectName2 = new ObjectName("test:index=2");
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
        MXBC_SimpleClass01 mxBeanObject = new MXBC_SimpleClass01();
        mbs.registerMBean(mxBeanObject, objectName1);
        mbs.registerMBean(mxBeanObject, objectName2);
        if (expectException) {
            throw new Exception("TEST FAILED: " + "InstanceAlreadyExistsException was not thrown");
        } else
            System.out.println("Correctly got no exception with compat property");
    } catch (InstanceAlreadyExistsException e) {
        if (expectException) {
            System.out.println("Got expected InstanceAlreadyExistsException:");
            e.printStackTrace(System.out);
        } else {
            throw new Exception("TEST FAILED: Got exception even though compat property set", e);
        }
    }
    System.out.println("TEST PASSED");
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Aggregations

MBeanServer (javax.management.MBeanServer)1218 ObjectName (javax.management.ObjectName)939 Test (org.junit.Test)214 MalformedObjectNameException (javax.management.MalformedObjectNameException)123 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)94 InstanceNotFoundException (javax.management.InstanceNotFoundException)87 IOException (java.io.IOException)82 JMXServiceURL (javax.management.remote.JMXServiceURL)70 Attribute (javax.management.Attribute)66 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)65 HashMap (java.util.HashMap)63 MBeanRegistrationException (javax.management.MBeanRegistrationException)56 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)54 TabularData (javax.management.openmbean.TabularData)51 ArrayList (java.util.ArrayList)47 JMXConnectorServer (javax.management.remote.JMXConnectorServer)47 JMXConnector (javax.management.remote.JMXConnector)40 Map (java.util.Map)38 JMException (javax.management.JMException)38 Test (org.junit.jupiter.api.Test)36