Search in sources :

Example 1 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project neo4j by neo4j.

the class HotspotManagementSupport method getJMXServiceURL.

@Override
protected JMXServiceURL getJMXServiceURL(KernelData kernel) {
    JMXServiceURL url = null;
    Log log = kernel.graphDatabase().getDependencyResolver().resolveDependency(LogService.class).getInternalLog(HotspotManagementSupport.class);
    try {
        Class<?> cal = Class.forName("sun.management.ConnectorAddressLink");
        try {
            Method importRemoteFrom = cal.getMethod("importRemoteFrom", int.class);
            @SuppressWarnings("unchecked") Map<String, String> remote = (Map<String, String>) importRemoteFrom.invoke(null, 0);
            url = getUrlFrom(remote);
        } catch (NoSuchMethodException ex) {
        // handled by null check
        }
        if (url == null) {
            Method importFrom = cal.getMethod("importFrom", int.class);
            url = getUrlFrom((String) importFrom.invoke(null, 0));
        }
    } catch (InvocationTargetException e) {
        log.warn("Failed to load local JMX connection URL.", e.getTargetException());
    } catch (LinkageError | Exception e) {
        log.warn("Failed to load local JMX connection URL.", e);
    }
    // No previous connection server -- create one!
    if (url == null) {
        int port = 0;
        try {
            port = kernel.getConfig().get(setting("jmx.port", INTEGER, "0"));
        } catch (NumberFormatException ok) {
        // handled by 0-check
        }
        if (port > 0) {
            boolean useSSL = kernel.getConfig().get(setting("jmx.use_ssl", BOOLEAN, "false"));
            log.debug("Creating new MBean server on port %s%s", port, useSSL ? " using ssl" : "");
            JMXConnectorServer server = createServer(port, useSSL, log);
            if (server != null) {
                try {
                    server.start();
                } catch (IOException e) {
                    log.warn("Failed to start MBean server", e);
                    server = null;
                }
                if (server != null) {
                    try {
                        server.getMBeanServer().registerMBean(server, KernelProxy.createObjectName(kernel.instanceId(), "JMX Server"));
                    } catch (Exception e) {
                        log.warn("Failed to register MBean server as JMX bean", e);
                    }
                    url = server.getAddress();
                }
            }
        }
    }
    return url;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) Log(org.neo4j.logging.Log) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) HashMap(java.util.HashMap) Map(java.util.Map) LogService(org.neo4j.kernel.impl.logging.LogService)

Example 2 with JMXConnectorServer

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

the class MBeanClientInterceptorTests method testTestLazyConnectionToRemote.

@Test
public void testTestLazyConnectionToRemote() throws Exception {
    assumeTrue(runTests);
    Assume.group(TestGroup.JMXMP);
    final int port = SocketUtils.findAvailableTcpPort();
    JMXServiceURL url = new JMXServiceURL("service:jmx:jmxmp://localhost:" + port);
    JMXConnectorServer connector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, getServer());
    MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean();
    factory.setServiceUrl(url.toString());
    factory.setProxyInterface(IJmxTestBean.class);
    factory.setObjectName(OBJECT_NAME);
    factory.setConnectOnStartup(false);
    factory.setRefreshOnConnectFailure(true);
    // should skip connection to the server
    factory.afterPropertiesSet();
    IJmxTestBean bean = (IJmxTestBean) factory.getObject();
    // now start the connector
    try {
        connector.start();
    } catch (BindException ex) {
        System.out.println("Skipping remainder of JMX LazyConnectionToRemote test because binding to local port [" + port + "] failed: " + ex.getMessage());
        return;
    }
    // should now be able to access data via the lazy proxy
    try {
        assertEquals("Rob Harrop", bean.getName());
        assertEquals(100, bean.getAge());
    } finally {
        connector.stop();
    }
    try {
        bean.getName();
    } catch (JmxException ex) {
    // expected
    }
    connector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, getServer());
    connector.start();
    // should now be able to access data via the lazy proxy
    try {
        assertEquals("Rob Harrop", bean.getName());
        assertEquals(100, bean.getAge());
    } finally {
        connector.stop();
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JmxException(org.springframework.jmx.JmxException) BindException(java.net.BindException) IJmxTestBean(org.springframework.jmx.IJmxTestBean) JMXConnectorServer(javax.management.remote.JMXConnectorServer) Test(org.junit.Test)

Example 3 with JMXConnectorServer

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

the class MBeanServerConnectionFactoryBeanTests method testTestValidConnection.

@Test
public void testTestValidConnection() throws Exception {
    Assume.group(TestGroup.JMXMP);
    JMXConnectorServer connectorServer = getConnectorServer();
    connectorServer.start();
    try {
        MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
        bean.setServiceUrl(serviceUrl);
        bean.afterPropertiesSet();
        try {
            MBeanServerConnection connection = bean.getObject();
            assertNotNull("Connection should not be null", connection);
            // perform simple MBean count test
            assertEquals("MBean count should be the same", getServer().getMBeanCount(), connection.getMBeanCount());
        } finally {
            bean.destroy();
        }
    } finally {
        connectorServer.stop();
    }
}
Also used : MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer) Test(org.junit.Test)

Example 4 with JMXConnectorServer

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

the class MBeanServerConnectionFactoryBeanTests method testTestWithLazyConnection.

@Test
public void testTestWithLazyConnection() throws Exception {
    Assume.group(TestGroup.JMXMP);
    MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
    bean.setServiceUrl(serviceUrl);
    bean.setConnectOnStartup(false);
    bean.afterPropertiesSet();
    MBeanServerConnection connection = bean.getObject();
    assertTrue(AopUtils.isAopProxy(connection));
    JMXConnectorServer connector = null;
    try {
        connector = getConnectorServer();
        connector.start();
        assertEquals("Incorrect MBean count", getServer().getMBeanCount(), connection.getMBeanCount());
    } finally {
        bean.destroy();
        if (connector != null) {
            connector.stop();
        }
    }
}
Also used : MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer) Test(org.junit.Test)

Example 5 with JMXConnectorServer

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

Aggregations

JMXConnectorServer (javax.management.remote.JMXConnectorServer)63 JMXServiceURL (javax.management.remote.JMXServiceURL)57 JMXConnector (javax.management.remote.JMXConnector)47 MBeanServer (javax.management.MBeanServer)44 MBeanServerConnection (javax.management.MBeanServerConnection)38 ObjectName (javax.management.ObjectName)31 HashMap (java.util.HashMap)24 IOException (java.io.IOException)13 MalformedURLException (java.net.MalformedURLException)13 RemoteException (java.rmi.RemoteException)13 NotificationListener (javax.management.NotificationListener)12 Notification (javax.management.Notification)11 Attribute (javax.management.Attribute)9 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7 Map (java.util.Map)7 Properties (java.util.Properties)7 JMXPluggableAuthenticator (com.sun.jmx.remote.security.JMXPluggableAuthenticator)5 UnknownHostException (java.net.UnknownHostException)5 Test (org.junit.Test)5