Example usage for javax.management.remote JMXConnector getMBeanServerConnection

List of usage examples for javax.management.remote JMXConnector getMBeanServerConnection

Introduction

In this page you can find the example usage for javax.management.remote JMXConnector getMBeanServerConnection.

Prototype

public MBeanServerConnection getMBeanServerConnection() throws IOException;

Source Link

Document

Returns an MBeanServerConnection object representing a remote MBean server.

Usage

From source file:org.wso2.dss.integration.test.datasource.DataSourceInitializationAtStartUpTestCase.java

@Test(dependsOnMethods = { "isServiceExistAfterRestarting" }, enabled = false)
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
public void testMBeanForDatasource() throws AxisFault {
    Map<String, String[]> env = new HashMap<String, String[]>();
    String[] credentials = { "admin", "admin" };
    env.put(JMXConnector.CREDENTIALS, credentials);
    try {/*from w w  w  . ja v  a 2 s .  c  om*/
        String url = "service:jmx:rmi://localhost:11111/jndi/rmi://localhost:9999/jmxrmi";
        JMXServiceURL jmxUrl = new JMXServiceURL(url);
        JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxUrl, env);
        MBeanServerConnection mBeanServer = jmxConnector.getMBeanServerConnection();
        ObjectName mbeanObject = new ObjectName(dataSourceName + ",-1234:type=DataSource");
        MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(mbeanObject);
        Assert.assertNotNull(mBeanInfo, "Data Source is registered in the MBean server");
    } catch (MalformedURLException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    } catch (IOException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    } catch (MalformedObjectNameException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    } catch (IntrospectionException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    } catch (ReflectionException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    } catch (InstanceNotFoundException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    }

}

From source file:org.wso2.ei.dataservice.integration.test.datasource.DataSourceInitializationAtStartUpTestCase.java

@Test(dependsOnMethods = { "isServiceExistAfterRestarting" }, enabled = false)
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
public void testMBeanForDatasource() throws AxisFault {
    Map<String, String[]> env = new HashMap<String, String[]>();
    String[] credentials = { "admin", "admin" };
    env.put(JMXConnector.CREDENTIALS, credentials);
    try {//from w ww .j  a  v a  2s  .c  o m
        String url = "service:jmx:rmi://localhost:11111/jndi/rmi://localhost:9999/jmxrmi";
        JMXServiceURL jmxUrl = new JMXServiceURL(url);
        JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxUrl, env);
        MBeanServerConnection mBeanServer = jmxConnector.getMBeanServerConnection();
        ObjectName mbeanObject = new ObjectName(dataSourceName + ",-1234:type=DataSource");
        MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(mbeanObject);
        Assert.assertNotNull(mBeanInfo, "Datasource is registered in the MBean server");
    } catch (MalformedURLException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    } catch (IOException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    } catch (MalformedObjectNameException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    } catch (IntrospectionException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    } catch (ReflectionException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    } catch (InstanceNotFoundException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    }

}

From source file:org.jboss.wsf.test.JBossWSTestHelper.java

private static MBeanServerConnection getServerConnection(String jmxServiceUrl) {
    final String urlString = System.getProperty("jmx.service.url", jmxServiceUrl);
    JMXServiceURL serviceURL = null;
    JMXConnector connector = null;
    try {/*from   www . jav  a  2 s  . c o m*/
        serviceURL = new JMXServiceURL(urlString);
    } catch (MalformedURLException e1) {
        throw new IllegalStateException(e1);
    }
    //add more tries to get the connection. Workaround to fix some test failures caused by connection is not established in 5 seconds
    for (int i = 0; i < AS_SERVER_CONN_RETRIEVAL_ATTEMPTS && connector == null; i++) {
        try {
            connector = JMXConnectorFactory.connect(serviceURL, null);
        } catch (IOException ex) {
            throw new IllegalStateException("Cannot obtain MBeanServerConnection to: " + urlString, ex);
        } catch (RuntimeException e) {
            if (e.getMessage().contains("WAITING") && i < AS_SERVER_CONN_RETRIEVAL_ATTEMPTS - 1) {
                continue;
            } else {
                throw e;
            }
        }
    }

    try {
        return connector.getMBeanServerConnection();
    } catch (Exception e) {
        throw new IllegalStateException("Cannot obtain MBeanServerConnection to: " + urlString, e);
    }
}

From source file:org.talend.esb.monitoring.hq.AbstractEmbeddedServerDetector.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from   www  .j  av  a2s  . c  o  m*/
public List getServerResources(final ConfigResponse platformConfig) throws PluginException {

    final List<ServerResource> discoveredServers = super.getServerResources(platformConfig);
    final List<ServerResource> serversToReturn = new ArrayList<ServerResource>();

    for (final ServerResource serverRes : discoveredServers) {
        JMXConnector jmxConnector = null;

        try {
            jmxConnector = MxUtil.getMBeanConnector(serverRes.getProductConfig().toProperties());

            Set<ObjectName> objNames = jmxConnector.getMBeanServerConnection().queryNames(targetDomainPattern,
                    null);

            if (objNames.size() > 0) {

                if (absentDomainPattern != null) {
                    objNames = jmxConnector.getMBeanServerConnection().queryNames(absentDomainPattern, null);

                    if (objNames.size() > 0) {
                        continue;
                    }
                }

                final StringBuilder sb = new StringBuilder();

                sb.append(title);
                sb.append("at");
                sb.append(serverRes.getInstallPath());

                serverRes.setIdentifier(sb.toString());
                serversToReturn.add(serverRes);
            }
        } catch (IOException e) {
            log.debug("Unable to check whether destination MBeanServer contains domain "
                    + targetDomainPattern.toString(), e);
        } finally {
            try {
                if (jmxConnector != null) {
                    jmxConnector.close();
                }
            } catch (IOException e) {
                log.debug("Exception during closing the connection to MBeanServer.", e);
            }
        }
    }

    return serversToReturn;
}

From source file:org.hyperic.hq.plugin.activemq.EmbeddedActiveMQServerDetector.java

@Override
protected List discoverServices(ConfigResponse serverConfig) throws PluginException {

    JMXConnector connector = null;
    MBeanServerConnection mServer;

    try {/*from   w ww. ja v a  2s  .c  o m*/
        connector = MxUtil.getMBeanConnector(serverConfig.toProperties());
        mServer = connector.getMBeanServerConnection();
    } catch (Exception e) {
        MxUtil.close(connector);
        throw new PluginException(e.getMessage(), e);
    }

    try {
        return _discoverMxServices(mServer, serverConfig);
    } finally {
        MxUtil.close(connector);
    }
}

From source file:org.apache.helix.tools.JmxDumper.java

public JmxDumper(String jmxService, String domain, String beanClassName, String namePattern, int samplePeriod,
        List<String> fields, List<String> operations, String outputfile, int sampleCount) throws Exception {
    _jmxUrl = jmxService;/* w  w w .  ja v a 2  s  .  com*/
    _domain = domain;
    _beanClassName = beanClassName;
    _samplePeriod = samplePeriod;
    _outputFields.addAll(fields);
    _operations.addAll(operations);
    _outputFileName = outputfile;
    _namePattern = namePattern;
    _targetSamples = sampleCount;

    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + _jmxUrl + "/jmxrmi");
    JMXConnector jmxc = JMXConnectorFactory.connect(url, null);

    _mbeanServer = jmxc.getMBeanServerConnection();
    MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter();
    filter.enableAllObjectNames();
    _mbeanServer.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this, filter, null);
    init();
    _timer = new Timer(true);
    _timer.scheduleAtFixedRate(new SampleTask(), _samplePeriod, _samplePeriod);
}

From source file:org.hyperic.hq.plugin.activemq.EmbeddedActiveMQServerDetector.java

@Override
public List getServerResources(ConfigResponse platformConfig) throws PluginException {
    log.debug("[getServerResources] platformConfig=" + platformConfig);
    List<ServerResource> servers = super.getServerResources(platformConfig);
    List<ServerResource> serversOK = new ArrayList();

    for (ServerResource server : servers) {
        JMXConnector connector = null;
        MBeanServerConnection mServer;
        try {/* ww  w . j  a  v  a2 s .co  m*/
            connector = MxUtil.getMBeanConnector(server.getProductConfig().toProperties());
            mServer = connector.getMBeanServerConnection();
            Set<ObjectName> objs = mServer.queryNames(new ObjectName("org.apache.activemq:*"), null);
            log.debug("[getServerResources] objs.size=" + objs.size());
            if (objs.size() > 0) { // Only discover servers with jmx obects on activeMQ domain.
                serversOK.add(server);
            }
        } catch (Exception e) {
            log.debug(e.getMessage(), e);
        } finally {
            try {
                if (connector != null) {
                    connector.close();
                }
            } catch (IOException e) {
                throw new PluginException(e.getMessage(), e);
            }
        }

    }
    return serversOK;
}

From source file:org.jumpmind.symmetric.JmxCommand.java

protected <T> T execute(IJmxTemplate<T> template) throws Exception {
    String host = "localhost";
    String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + System.getProperty("jmx.agent.port", "31418")
            + "/jmxrmi";
    JMXServiceURL serviceUrl = new JMXServiceURL(url);
    JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);
    TypedProperties properties = getTypedProperties();
    String engineName = properties.get(ParameterConstants.ENGINE_NAME, "unknown");
    try {//from w  w w.  j a  v  a  2s .  c o  m
        MBeanServerConnection mbeanConn = jmxConnector.getMBeanServerConnection();
        return template.execute(engineName, mbeanConn);
    } finally {
        jmxConnector.close();
    }
}

From source file:org.zenoss.jmxnl.NotificationListener.java

@SuppressWarnings("unchecked")
private void connect() throws IOException {
    log.info(url + ": Attempting connection (timeout in 180 seconds)");
    JMXConnector connector = JMXConnectorFactory.connect(url, environment);
    connector.addConnectionNotificationListener(this, null, "placeholder");
    MBeanServerConnection connection = connector.getMBeanServerConnection();

    log.info(url + ": Connected.");
    sendConnectionEvent("0", "JMX connection has been restored");

    Set<ObjectName> results = connection.queryNames(scope, null);
    java.util.Iterator<ObjectName> iter = results.iterator();
    while (iter.hasNext()) {
        ObjectName objName = (ObjectName) iter.next();
        String type = objName.getKeyProperty("type");
        if (type == null || !type.equals("alias")) {
            try {
                connection.addNotificationListener(objName, this, attributeFilter, zenossDevice);
                log.debug("Added notification listener: " + objName);
            } catch (IllegalArgumentException e) {
                log.debug("Can't listen to " + objName + " because it is not a notification broadcaster.");
            } catch (InstanceNotFoundException e) {
                log.debug("Can't listen to " + objName + " because it was not found on the server.");
            }//from w  w  w.jav  a2  s.  co m
        }

        // There can be a lot of listeners to add. Give other threads a
        // chance to get work done while this happens.
        Thread.yield();
    }
}

From source file:org.opennms.features.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggenerator.java

public MBeanServerConnection createMBeanServerConnection(JMXConnector jmxConnector) throws IOException {
    MBeanServerConnection jmxServerConnection = jmxConnector.getMBeanServerConnection();
    logger.debug("jmxServerConnection: '{}'", jmxServerConnection);
    logger.debug("count: " + jmxServerConnection.getMBeanCount());
    return jmxServerConnection;
}