Search in sources :

Example 46 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project geode by apache.

the class JMXDataUpdater method connect.

/**
   * Get the jmx connection
   */
public JMXConnector connect(String username, String password) {
    // Reference to repository
    Repository repository = Repository.get();
    try {
        String jmxSerURL = "";
        logger.info("{}:{}", resourceBundle.getString("LOG_MSG_USE_LOCATOR_VALUE"), repository.getJmxUseLocator());
        if (repository.getJmxUseLocator()) {
            JmxManagerInfo jmxManagerInfo = getManagerInfoFromLocator(repository);
            if (jmxManagerInfo.port == 0) {
                logger.info(resourceBundle.getString("LOG_MSG_LOCATOR_COULD_NOT_FIND_MANAGER"));
            } else {
                logger.info("{}: {}={} & {}={}, {}", resourceBundle.getString("LOG_MSG_LOCATOR_FOUND_MANAGER"), resourceBundle.getString("LOG_MSG_HOST"), jmxManagerInfo.host, resourceBundle.getString("LOG_MSG_PORT"), jmxManagerInfo.port, (jmxManagerInfo.ssl ? resourceBundle.getString("LOG_MSG_WITH_SSL") : resourceBundle.getString("LOG_MSG_WITHOUT_SSL")));
                jmxSerURL = formJMXServiceURLString(jmxManagerInfo.host, String.valueOf(jmxManagerInfo.port));
            }
        } else {
            logger.info("{}={} & {}={}", resourceBundle.getString("LOG_MSG_HOST"), this.serverName, resourceBundle.getString("LOG_MSG_PORT"), this.port);
            jmxSerURL = formJMXServiceURLString(this.serverName, this.port);
        }
        if (StringUtils.isNotBlank(jmxSerURL)) {
            JMXServiceURL url = new JMXServiceURL(jmxSerURL);
            String[] creds = { username, password };
            Map<String, Object> env = new HashMap<String, Object>();
            env.put(JMXConnector.CREDENTIALS, creds);
            if (repository.isUseSSLManager()) {
                // use ssl to connect
                env.put("com.sun.jndi.rmi.factory.socket", new SslRMIClientSocketFactory());
            }
            logger.info("Connecting to jmxURL : {}", jmxSerURL);
            this.conn = JMXConnectorFactory.connect(url, env);
            this.mbs = this.conn.getMBeanServerConnection();
            cluster.setConnectedFlag(true);
        }
    } catch (Exception e) {
        cluster.setConnectedFlag(false);
        cluster.setConnectionErrorMsg(e.getMessage());
        logger.fatal(e.getMessage(), e);
        if (this.conn != null) {
            try {
                this.conn.close();
            } catch (Exception e1) {
                logger.fatal(e1.getMessage(), e1);
            }
            this.conn = null;
        }
    }
    return this.conn;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) JmxManagerInfo(org.apache.geode.tools.pulse.internal.data.JmxManagerFinder.JmxManagerInfo) HashMap(java.util.HashMap) AttributeNotFoundException(javax.management.AttributeNotFoundException) IntrospectionException(javax.management.IntrospectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanException(javax.management.MBeanException)

Example 47 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project karaf by apache.

the class DeployMojo method deployWithJmx.

protected void deployWithJmx(List<String> locations) throws MojoExecutionException {
    try {
        JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/" + instance);
        ArrayList<String> list = new ArrayList<>();
        if (user != null)
            list.add(user);
        if (password != null)
            list.add(password);
        HashMap env = new HashMap();
        String[] credentials = list.toArray(new String[list.size()]);
        env.put(JMXConnector.CREDENTIALS, credentials);
        JMXConnector jmxConnector = null;
        if (credentials.length > 0)
            jmxConnector = JMXConnectorFactory.connect(jmxServiceURL, env);
        else
            jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
        MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection();
        for (String location : locations) {
            mBeanServerConnection.invoke(new ObjectName("org.apache.karaf:type=bundle,name=*"), "install", new Object[] { location, true }, new String[] { "java.lang.String", "boolean" });
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Can't deploy using JMX", e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) JMXConnector(javax.management.remote.JMXConnector) ArrayList(java.util.ArrayList) MBeanServerConnection(javax.management.MBeanServerConnection) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ObjectName(javax.management.ObjectName)

Example 48 with JMXServiceURL

use of javax.management.remote.JMXServiceURL 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 49 with JMXServiceURL

use of javax.management.remote.JMXServiceURL 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 50 with JMXServiceURL

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

the class ManagedServiceUrlPathTest method testConnectToJmx.

public void testConnectToJmx() throws Exception {
    // JMX tests dont work well on AIX CI servers (hangs them)
    if (isPlatform("aix")) {
        return;
    }
    clientConnector = JMXConnectorFactory.connect(new JMXServiceURL(JMXSERVICEURL), null);
    MBeanServerConnection mbeanServer = clientConnector.getMBeanServerConnection();
    ObjectName name = ObjectName.getInstance("org.apache.camel:context=camel-1,type=endpoints,name=\"direct://start\"");
    String uri = (String) mbeanServer.getAttribute(name, "EndpointUri");
    assertEquals("direct://start", uri);
    name = ObjectName.getInstance("org.apache.camel:context=camel-1,type=endpoints,name=\"log://foo\"");
    uri = (String) mbeanServer.getAttribute(name, "EndpointUri");
    assertEquals("log://foo", uri);
    name = ObjectName.getInstance("org.apache.camel:context=camel-1,type=endpoints,name=\"mock://result\"");
    uri = (String) mbeanServer.getAttribute(name, "EndpointUri");
    assertEquals("mock://result", uri);
    String id = (String) mbeanServer.getAttribute(name, "CamelId");
    assertEquals("camel-1", id);
    Boolean singleton = (Boolean) mbeanServer.getAttribute(name, "Singleton");
    assertEquals(Boolean.TRUE, singleton);
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName)

Aggregations

JMXServiceURL (javax.management.remote.JMXServiceURL)280 JMXConnector (javax.management.remote.JMXConnector)150 MBeanServerConnection (javax.management.MBeanServerConnection)107 ObjectName (javax.management.ObjectName)90 IOException (java.io.IOException)78 HashMap (java.util.HashMap)75 MBeanServer (javax.management.MBeanServer)71 JMXConnectorServer (javax.management.remote.JMXConnectorServer)64 MalformedURLException (java.net.MalformedURLException)45 RemoteException (java.rmi.RemoteException)22 Test (org.junit.Test)22 Map (java.util.Map)16 UnknownHostException (java.net.UnknownHostException)14 Properties (java.util.Properties)14 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)14 MalformedObjectNameException (javax.management.MalformedObjectNameException)13 LocateRegistry (java.rmi.registry.LocateRegistry)12 Registry (java.rmi.registry.Registry)12 ArrayList (java.util.ArrayList)11