Search in sources :

Example 96 with MBeanServerConnection

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

the class ExceptionTest method run.

public void run(Map<String, Object> args) {
    System.out.println("ExceptionTest::run: Start");
    int errorCount = 0;
    JMXConnectorServer cs = null;
    JMXConnector cc = null;
    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);
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();
        // ----
        ObjectName objName = new ObjectName(ExceptionThrower.EXCEPTION_THROWER_NAME);
        System.out.println("ExceptionTest::run: Create and register MBean " + objName);
        mbsc.createMBean("ExceptionThrower", objName);
        System.out.println("---- OK\n");
        // ----
        System.out.println("ExceptionTest::run: Ask for exception(s)");
        Object[] throwExceptionParam = new Object[1];
        String[] throwExceptionSig = new String[] { "int" };
        for (int i = 0; i < ExceptionFactory.exceptions.size(); i++) {
            throwExceptionParam[0] = new Integer(i);
            Exception ex = (Exception) mbsc.invoke(objName, "throwException", throwExceptionParam, throwExceptionSig);
            if (!matches(ex, ExceptionFactory.exceptions.get(i))) {
                errorCount++;
                System.out.println("ExceptionTest::run: (ERROR) Received \n[" + ex + "]\nin place of\n[" + ExceptionFactory.exceptions.get(i) + "]");
            } else {
                System.out.println("OK [" + ex + "]");
            }
        }
        System.out.println("---- DONE\n");
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        throw new RuntimeException();
    } finally {
        try {
            // Close JMX Connector Client
            cc.close();
            // Stop connertor server
            cs.stop();
        } catch (Exception e) {
            Utils.printThrowable(e, true);
            throw new RuntimeException("Unable to either close connector client or stop connector server");
        }
    }
    if (errorCount == 0) {
        System.out.println("ExceptionTest::run: Done without any error");
    } else {
        System.out.println("ExceptionTest::run: Done with " + errorCount + " error(s)");
        throw new RuntimeException("errorCount = " + errorCount);
    }
    System.out.println("ExceptionTest::run: Done");
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 97 with MBeanServerConnection

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

the class RMIPasswdAuthTest method main.

public static void main(String[] args) {
    try {
        // Set the default password file
        //
        final String passwordFile = System.getProperty("test.src") + File.separator + "jmxremote.password";
        System.out.println("Password file = " + passwordFile);
        // Create an RMI registry
        //
        System.out.println("Start RMI registry...");
        Registry reg = null;
        int port = 5800;
        while (port++ < 6000) {
            try {
                reg = LocateRegistry.createRegistry(port);
                System.out.println("RMI registry running on port " + port);
                break;
            } catch (RemoteException e) {
                // Failed to create RMI registry...
                System.out.println("Failed to create RMI registry " + "on port " + port);
            }
        }
        if (reg == null) {
            System.exit(1);
        }
        // Instantiate the MBean server
        //
        System.out.println("Create the MBean server");
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
        // Register the ClassPathClassLoaderMBean
        //
        System.out.println("Create ClassPathClassLoader MBean");
        ObjectName cpcl = new ObjectName("ClassLoader:name=ClassPathClassLoader");
        mbs.createMBean("javax.management.loading.MLet", cpcl);
        // Register the SimpleStandardMBean
        //
        System.out.println("Create SimpleStandard MBean");
        mbs.createMBean("SimpleStandard", new ObjectName("MBeans:name=SimpleStandard"));
        // Create Properties containing the location of the password file
        //
        Properties props = new Properties();
        props.setProperty("jmx.remote.x.password.file", passwordFile);
        // Initialize environment map to be passed to the connector server
        //
        System.out.println("Initialize environment map");
        HashMap env = new HashMap();
        env.put("jmx.remote.authenticator", new JMXPluggableAuthenticator(props));
        // Create an RMI connector server
        //
        System.out.println("Create an RMI connector server");
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0, "/jndi/rmi://:" + port + "/server" + port);
        JMXConnectorServer rcs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        rcs.start();
        // Create an RMI connector client
        //
        System.out.println("Create an RMI connector client");
        HashMap cli_env = new HashMap();
        // These credentials must match those in the supplied password file
        //
        String[] credentials = new String[] { "monitorRole", "QED" };
        cli_env.put("jmx.remote.credentials", credentials);
        JMXConnector jmxc = JMXConnectorFactory.connect(url, cli_env);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        // Get domains from MBeanServer
        //
        System.out.println("Domains:");
        String[] domains = mbsc.getDomains();
        for (int i = 0; i < domains.length; i++) {
            System.out.println("\tDomain[" + i + "] = " + domains[i]);
        }
        // Get MBean count
        //
        System.out.println("MBean count = " + mbsc.getMBeanCount());
        // Get State attribute
        //
        String oldState = (String) mbsc.getAttribute(new ObjectName("MBeans:name=SimpleStandard"), "State");
        System.out.println("Old State = \"" + oldState + "\"");
        // Set State attribute
        //
        System.out.println("Set State to \"changed state\"");
        mbsc.setAttribute(new ObjectName("MBeans:name=SimpleStandard"), new Attribute("State", "changed state"));
        // Get State attribute
        //
        String newState = (String) mbsc.getAttribute(new ObjectName("MBeans:name=SimpleStandard"), "State");
        System.out.println("New State = \"" + newState + "\"");
        if (!newState.equals("changed state")) {
            System.out.println("Invalid State = \"" + newState + "\"");
            System.exit(1);
        }
        // Add notification listener on SimpleStandard MBean
        //
        System.out.println("Add notification listener...");
        mbsc.addNotificationListener(new ObjectName("MBeans:name=SimpleStandard"), new NotificationListener() {

            public void handleNotification(Notification notification, Object handback) {
                System.out.println("Received notification: " + notification);
            }
        }, null, null);
        // Unregister SimpleStandard MBean
        //
        System.out.println("Unregister SimpleStandard MBean...");
        mbsc.unregisterMBean(new ObjectName("MBeans:name=SimpleStandard"));
        // Close MBeanServer connection
        //
        jmxc.close();
        System.out.println("Bye! Bye!");
    } catch (Exception e) {
        System.out.println("Unexpected exception caught = " + e);
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXPluggableAuthenticator(com.sun.jmx.remote.security.JMXPluggableAuthenticator) HashMap(java.util.HashMap) Attribute(javax.management.Attribute) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) Properties(java.util.Properties) Notification(javax.management.Notification) RemoteException(java.rmi.RemoteException) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) RemoteException(java.rmi.RemoteException) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 98 with MBeanServerConnection

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

the class ProviderTest method dotest.

private static void dotest(JMXServiceURL url, MBeanServer mbs) throws Exception {
    JMXConnectorServer server = null;
    JMXConnector client = null;
    server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    server.start();
    JMXServiceURL outputAddr = server.getAddress();
    System.out.println("Server started [" + outputAddr + "]");
    client = JMXConnectorFactory.newJMXConnector(outputAddr, null);
    client.connect();
    System.out.println("Client connected");
    MBeanServerConnection connection = client.getMBeanServerConnection();
    System.out.println(connection.getDefaultDomain());
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 99 with MBeanServerConnection

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

the class ListenerScaleTest method test.

private static void test(MBeanServer mbs, JMXConnectorServer cs, JMXConnector cc) throws Exception {
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();
    mbsc.addNotificationListener(testObjectName, timingListener, null, null);
    long singleMBeanTime = 0;
    for (int i = 0; i < WARMUP_WITH_ONE_MBEAN; i++) singleMBeanTime = timeNotif(mbs);
    if (singleMBeanTime == 0)
        singleMBeanTime = 1;
    System.out.println("Time with a single MBean: " + singleMBeanTime + "ns");
    System.out.println("Now registering " + EXTRA_MBEANS + " MBeans");
    for (int i = 0; i < EXTRA_MBEANS; i++) {
        ObjectName on = new ObjectName("test:type=Sender,number=" + i);
        mbs.registerMBean(new Sender(), on);
        if (i % 1000 == 999) {
            System.out.print("..." + (i + 1));
            System.out.flush();
        }
    }
    System.out.println();
    System.out.println("Now registering " + EXTRA_MBEANS + " listeners");
    for (int i = 0; i < EXTRA_MBEANS; i++) {
        ObjectName on = new ObjectName("test:type=Sender,number=" + i);
        mbsc.addNotificationListener(on, nullListener, null, null);
        if (i % 1000 == 999) {
            System.out.print("..." + (i + 1));
            System.out.flush();
        }
    }
    System.out.println();
    System.out.println("Timing a notification send now");
    long manyMBeansTime = timeNotif(mbs);
    System.out.println("Time with many MBeans: " + manyMBeansTime + "ns");
    double ratio = (double) manyMBeansTime / singleMBeanTime;
    if (ratio > 500.0)
        throw new Exception("Failed: ratio=" + ratio);
    System.out.println("Test passed: ratio=" + ratio);
}
Also used : MBeanServerConnection(javax.management.MBeanServerConnection) MalformedObjectNameException(javax.management.MalformedObjectNameException) ObjectName(javax.management.ObjectName)

Example 100 with MBeanServerConnection

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

the class NotSerializableNotifTest method test.

private static void test(String proto) throws Exception {
    System.out.println("\n>>> Test for protocol " + proto);
    JMXServiceURL url = new JMXServiceURL(proto, null, 0);
    System.out.println(">>> Create a server: " + url);
    JMXConnectorServer server = null;
    try {
        server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
    } catch (MalformedURLException e) {
        System.out.println("System does not recognize URL: " + url + "; ignoring");
        return;
    }
    server.start();
    url = server.getAddress();
    System.out.println(">>> Creating a client connectint to: " + url);
    JMXConnector conn = JMXConnectorFactory.connect(url, null);
    MBeanServerConnection client = conn.getMBeanServerConnection();
    // add listener from the client side
    Listener listener = new Listener();
    client.addNotificationListener(emitter, listener, null, null);
    // ask to send one not serializable notif
    Object[] params = new Object[] { new Integer(1) };
    String[] signatures = new String[] { "java.lang.Integer" };
    client.invoke(emitter, "sendNotserializableNotifs", params, signatures);
    // listener clean
    client.removeNotificationListener(emitter, listener);
    listener = new Listener();
    client.addNotificationListener(emitter, listener, null, null);
    //ask to send serializable notifs
    params = new Object[] { new Integer(sentNotifs) };
    client.invoke(emitter, "sendNotifications", params, signatures);
    // waiting ...
    synchronized (listener) {
        while (listener.received() < sentNotifs) {
            // either pass or test timeout (killed by test harness)
            listener.wait();
        }
    }
    // clean
    client.removeNotificationListener(emitter, listener);
    conn.close();
    server.stop();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) NotificationListener(javax.management.NotificationListener) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Aggregations

MBeanServerConnection (javax.management.MBeanServerConnection)125 JMXConnector (javax.management.remote.JMXConnector)84 ObjectName (javax.management.ObjectName)73 JMXServiceURL (javax.management.remote.JMXServiceURL)59 JMXConnectorServer (javax.management.remote.JMXConnectorServer)38 Test (org.junit.Test)35 IOException (java.io.IOException)31 MBeanServer (javax.management.MBeanServer)28 HashMap (java.util.HashMap)23 Attribute (javax.management.Attribute)15 NotificationListener (javax.management.NotificationListener)13 MalformedURLException (java.net.MalformedURLException)12 ArrayList (java.util.ArrayList)12 Notification (javax.management.Notification)12 MalformedObjectNameException (javax.management.MalformedObjectNameException)11 Map (java.util.Map)10 List (java.util.List)8 RemoteException (java.rmi.RemoteException)7 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7