Search in sources :

Example 16 with JMXConnector

use of javax.management.remote.JMXConnector in project camel by apache.

the class CamelKarafTestSupport method getJMXConnector.

public JMXConnector getJMXConnector(String userName, String passWord) throws Exception {
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root");
    Hashtable<String, Object> env = new Hashtable<>();
    String[] credentials = new String[] { userName, passWord };
    env.put("jmx.remote.credentials", credentials);
    JMXConnector connector = JMXConnectorFactory.connect(url, env);
    return connector;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) Hashtable(java.util.Hashtable) JMXConnector(javax.management.remote.JMXConnector)

Example 17 with JMXConnector

use of javax.management.remote.JMXConnector in project jdk8u_jdk by JetBrains.

the class StartManagementAgent method tryConnect.

private static void tryConnect(int port, boolean shouldSucceed) throws Exception {
    String jmxUrlStr = String.format("service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi", port);
    JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
    HashMap<String, ?> env = new HashMap<>();
    boolean succeeded;
    try {
        JMXConnector c = JMXConnectorFactory.connect(url, env);
        c.getMBeanServerConnection();
        succeeded = true;
    } catch (Exception ex) {
        succeeded = false;
    }
    if (succeeded && !shouldSucceed) {
        throw new Exception("Could connect to agent, but should not have been possible");
    }
    if (!succeeded && shouldSucceed) {
        throw new Exception("Could not connect to agent");
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) JMXConnector(javax.management.remote.JMXConnector) AttachOperationFailedException(com.sun.tools.attach.AttachOperationFailedException)

Example 18 with JMXConnector

use of javax.management.remote.JMXConnector in project jdk8u_jdk by JetBrains.

the class JMXServerErrorTest method test.

public void test(String url) throws Exception {
    final JMXServiceURL jurl = new JMXServiceURL(url);
    final ObjectName kname = new ObjectName(":type=Kaeffer");
    final MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    final String that = "that";
    mbs.registerMBean(new Kaeffer(that), kname);
    final MBeanServer kbs = new MBeanServerKaeffer(mbs);
    final JMXConnectorServer cs;
    try {
        cs = JMXConnectorServerFactory.newJMXConnectorServer(jurl, null, kbs);
    } catch (MalformedURLException m) {
        if ("jmxmp".equals(jurl.getProtocol()) || "iiop".equals(jurl.getProtocol())) {
            // OK, we may not have this in the classpath...
            System.out.println("WARNING: Skipping protocol: " + jurl);
            return;
        }
        throw m;
    }
    final ObjectName cname = new ObjectName(":type=JMXConnectorServer");
    mbs.registerMBean(cs, cname);
    cs.start();
    JMXConnector c = null;
    try {
        c = JMXConnectorFactory.connect(cs.getAddress(), null);
        final MBeanServerConnection mbsc = c.getMBeanServerConnection();
        final KaefferMBean kaeffer = (KaefferMBean) MBeanServerInvocationHandler.newProxyInstance(mbsc, kname, KaefferMBean.class, false);
        final String that1 = kaeffer.getThis();
        if (!that.equals(that1))
            throw new Exception("Unexpected string returned by " + kname + ": " + that1);
        try {
            kaeffer.setThis("but not that");
            throw new Exception("Expected JMXServerErrorException" + " not thrown" + " for setAttribute \"This\" ");
        } catch (JMXServerErrorException jsee) {
            if (!(jsee.getCause() instanceof KaefferError)) {
                final Exception e = new Exception("Expected JMXServerErrorException" + " is not an instance of " + KaefferError.class.getName() + ": " + jsee.getCause());
                e.initCause(jsee);
                throw e;
            }
            System.out.println("Got expected error: " + jsee);
        }
        try {
            kaeffer.doThis("but not that");
            throw new Exception("Expected JMXServerErrorException" + " not thrown" + " for invoke \"doThis\" ");
        } catch (JMXServerErrorException jsee) {
            if (!(jsee.getCause() instanceof KaefferError)) {
                final Exception e = new Exception("Expected JMXServerErrorException" + " is not an instance of " + KaefferError.class.getName() + ": " + jsee.getCause());
                e.initCause(jsee);
                throw e;
            }
            System.out.println("Got expected error: " + jsee);
        }
    } finally {
        if (c != null)
            try {
                c.close();
            } catch (Exception x) {
                System.err.println("Failed to close client: " + x);
                throw x;
            }
        try {
            cs.stop();
        } catch (Exception x) {
            System.err.println("Failed to stop server: " + x);
            throw x;
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) JMXServerErrorException(javax.management.remote.JMXServerErrorException) MalformedURLException(java.net.MalformedURLException) JMXServerErrorException(javax.management.remote.JMXServerErrorException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 19 with JMXConnector

use of javax.management.remote.JMXConnector in project jdk8u_jdk by JetBrains.

the class RMISocketFactoriesTest method main.

public static void main(String[] args) {
    System.out.println("Test RMI factories : " + args[0]);
    try {
        // 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();
        // Initialize environment map to be passed to the connector server
        //
        System.out.println("Initialize environment map");
        HashMap env = new HashMap();
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new RMIServerFactory(args[0]));
        env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new RMIClientFactory(args[0]));
        // 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");
        JMXConnector jmxc = JMXConnectorFactory.connect(url, new HashMap());
        // If this line is executed then the test failed
        //
        System.exit(1);
    } catch (Exception e) {
        if (e.getMessage().equals(args[0])) {
            System.out.println("Expected exception caught = " + e);
            System.out.println("Bye! Bye!");
        } else {
            System.out.println("Unexpected exception caught = " + e);
            e.printStackTrace();
            System.exit(1);
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) JMXConnector(javax.management.remote.JMXConnector) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) RemoteException(java.rmi.RemoteException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) MBeanServer(javax.management.MBeanServer) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 20 with JMXConnector

use of javax.management.remote.JMXConnector in project jdk8u_jdk by JetBrains.

the class ConcurrentModificationTest method test.

private static void test(String proto) throws Exception {
    JMXServiceURL u = new JMXServiceURL(proto, null, 0);
    JMXConnectorServer server;
    JMXConnector client;
    try {
        server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
        server.start();
        JMXServiceURL addr = server.getAddress();
        client = JMXConnectorFactory.connect(addr, null);
    } catch (MalformedURLException e) {
        System.out.println(">>> not support: " + proto);
        return;
    }
    final MBeanServerConnection mserver = client.getMBeanServerConnection();
    int count = 0;
    boolean adding = true;
    while (uncaughtException == null && count++ < 10) {
        for (int i = 0; i < number; i++) {
            listenerOp(mserver, listeners[i], adding);
            mbeanOp(mserver, timerNames[i], adding);
        }
        adding = !adding;
    }
    if (uncaughtException != null) {
        // clean
        for (int i = 0; i < number; i++) {
            try {
                mbeanOp(mserver, timerNames[i], false);
            } catch (Exception e) {
            }
        }
    }
    client.close();
    server.stop();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MalformedURLException(java.net.MalformedURLException) ConcurrentModificationException(java.util.ConcurrentModificationException) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Aggregations

JMXConnector (javax.management.remote.JMXConnector)118 MBeanServerConnection (javax.management.MBeanServerConnection)85 JMXServiceURL (javax.management.remote.JMXServiceURL)78 ObjectName (javax.management.ObjectName)54 JMXConnectorServer (javax.management.remote.JMXConnectorServer)47 MBeanServer (javax.management.MBeanServer)37 IOException (java.io.IOException)35 HashMap (java.util.HashMap)27 Test (org.junit.Test)22 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)14 Attribute (javax.management.Attribute)13 MalformedURLException (java.net.MalformedURLException)12 RemoteException (java.rmi.RemoteException)11 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 MalformedObjectNameException (javax.management.MalformedObjectNameException)9 LocateRegistry (java.rmi.registry.LocateRegistry)8 Registry (java.rmi.registry.Registry)8 Properties (java.util.Properties)7