Example usage for java.lang.management ManagementFactory newPlatformMXBeanProxy

List of usage examples for java.lang.management ManagementFactory newPlatformMXBeanProxy

Introduction

In this page you can find the example usage for java.lang.management ManagementFactory newPlatformMXBeanProxy.

Prototype

public static <T> T newPlatformMXBeanProxy(MBeanServerConnection connection, String mxbeanName,
        Class<T> mxbeanInterface) throws java.io.IOException 

Source Link

Document

Returns a proxy for a platform MXBean interface of a given MXBean name that forwards its method calls through the given MBeanServerConnection .

Usage

From source file:org.wso2.carbon.cassandra.cluster.ClusterDataAccessMBeanImplementation.java

/**
 * A helper method to access a MBean of the platform
 *
 * @param name       name of the MBean/*from  w  w w .  j  a v  a 2s. com*/
 * @param mBeanClass MBean Class
 * @param <T>        types of the MBean
 * @return MBean instance with given Type
 * @throws ClusterMBeanDataAccessException
 *          for error during locating the given MBean
 */
private <T> T locateManagementFactoryMBean(String name, Class<T> mBeanClass) {
    try {
        return ManagementFactory.newPlatformMXBeanProxy(mBeanServerConnection, name, mBeanClass);
    } catch (IOException e) {
        throw new ClusterMBeanDataAccessException("Error while creating platform mBean connection.", e, log);
    }
}

From source file:org.opennms.systemreport.AbstractSystemReportPlugin.java

protected <T> T getBean(final String mxBeanName, final List<? extends Class<T>> classes) {
    initializeConnection();//from ww w .j a va 2s  . c o  m
    if (m_connection == null || mxBeanName == null || classes == null || classes.size() == 0) {
        return null;
    }

    T bean = null;
    for (final Class<T> c : classes) {
        try {
            bean = ManagementFactory.newPlatformMXBeanProxy(m_connection, mxBeanName, c);
            break;
        } catch (final Exception e) {
            LOG.info("Unable to get management bean {} for class {}", mxBeanName, c.getName(), e);
        }
    }
    return bean;
}

From source file:org.rhq.plugins.jmx.JMXDiscoveryComponent.java

@Override
public ResourceUpgradeReport upgrade(ResourceUpgradeContext inventoriedResource) {
    JvmResourceKey oldKey = JvmResourceKey.valueOf(inventoriedResource.getResourceKey());
    JvmResourceKey.Type oldKeyType = oldKey.getType();
    if (oldKeyType == JvmResourceKey.Type.Legacy || oldKeyType == JvmResourceKey.Type.JmxRemotingPort) {
        if (!inventoriedResource.getSystemInformation().isNative()) {
            log.warn("Cannot attempt to upgrade Resource key [" + inventoriedResource.getResourceKey()
                    + "] of JVM Resource, because this Agent is not running with native system info support (i.e. SIGAR).");
            return null;
        }//from w  w w . j  a v a2  s.  co  m

        Configuration pluginConfig = inventoriedResource.getPluginConfiguration();
        String connectorAddress = pluginConfig.getSimpleValue(CONNECTOR_ADDRESS_CONFIG_PROPERTY, null);
        JMXServiceURL jmxServiceURL;
        try {
            jmxServiceURL = new JMXServiceURL(connectorAddress);
        } catch (MalformedURLException e) {
            throw new RuntimeException("Failed to parse connector address: " + connectorAddress, e);
        }

        Long pid;
        try {
            JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
            MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();
            RuntimeMXBean runtimeMXBean = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConnection,
                    ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
            pid = getJvmPid(runtimeMXBean);
            if (pid == null) {
                throw new RuntimeException("Failed to determine JVM pid by parsing JVM name.");
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to determine JVM pid.", e);
        }

        List<ProcessInfo> processes = inventoriedResource.getSystemInformation()
                .getProcesses("process|pid|match=" + pid);
        if (processes.size() != 1) {
            throw new IllegalStateException("Failed to find process with PID [" + pid + "].");
        }
        ProcessInfo process = processes.get(0);
        String mainClassName = getJavaMainClassName(process);
        String explicitKeyValue = getSystemPropertyValue(process, SYSPROP_RHQ_RESOURCE_KEY);
        if (oldKeyType == JvmResourceKey.Type.Legacy || explicitKeyValue != null) {
            // We need to upgrade the key.
            JvmResourceKey newKey;
            if (explicitKeyValue != null) {
                newKey = JvmResourceKey.fromExplicitValue(mainClassName, explicitKeyValue);
            } else {
                newKey = JvmResourceKey.fromJmxRemotingPort(mainClassName, oldKey.getJmxRemotingPort());
            }

            ResourceUpgradeReport resourceUpgradeReport = new ResourceUpgradeReport();
            resourceUpgradeReport.setNewResourceKey(newKey.toString());
            return resourceUpgradeReport;
        }
    }

    return null;
}

From source file:org.rhq.plugins.jmx.JMXDiscoveryComponent.java

protected DiscoveredResourceDetails buildResourceDetails(ResourceDiscoveryContext context, ProcessInfo process,
        JMXServiceURL jmxServiceURL, Integer jmxRemotingPort) {
    JvmResourceKey key;//  w ww . ja v a  2s.c  o  m
    String mainClassName = getJavaMainClassName(process);
    String keyString = getSystemPropertyValue(process, SYSPROP_RHQ_RESOURCE_KEY);
    if (keyString != null && !keyString.equals("")) {
        log.debug("Using explicitly specified Resource key: [" + keyString + "]...");
        key = JvmResourceKey.fromExplicitValue(mainClassName, keyString);
    } else {
        if (jmxRemotingPort != null) {
            log.debug("Using JMX remoting port [" + jmxRemotingPort + "] as Resource key...");
            key = JvmResourceKey.fromJmxRemotingPort(mainClassName, jmxRemotingPort);
        } else {
            log.debug("Process [" + process.getPid() + "] with command line ["
                    + Arrays.asList(process.getCommandLine())
                    + "] cannot be discovered, because it does not specify either of the following system properties: "
                    + "-D" + SYSPROP_JMXREMOTE_PORT + "=12345, -D" + SYSPROP_RHQ_RESOURCE_KEY + "=UNIQUE_KEY");
            return null;
        }
    }

    String name = buildResourceName(key);

    String version;
    try {
        JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
        MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();
        RuntimeMXBean runtimeMXBean = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConnection,
                ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
        version = runtimeMXBean.getSystemProperties().get(SYSPROP_JAVA_VERSION);
        if (version == null) {
            throw new IllegalStateException("System property [" + SYSPROP_JAVA_VERSION + "] is not defined.");
        }
    } catch (Exception e) {
        log.error("Failed to determine JVM version for process [" + process.getPid() + "] with command line ["
                + Arrays.asList(process.getCommandLine()) + "].", e);
        version = null;
    }

    String description = "JVM, monitored via "
            + ((jmxRemotingPort != null) ? "JMX Remoting" : "Sun JVM Attach API");

    Configuration pluginConfig = context.getDefaultPluginConfiguration();
    pluginConfig.put(new PropertySimple(CONNECTION_TYPE, J2SE5ConnectionTypeDescriptor.class.getName()));
    if (jmxRemotingPort != null) {
        pluginConfig.put(new PropertySimple(CONNECTOR_ADDRESS_CONFIG_PROPERTY, jmxServiceURL));
    }

    return new DiscoveredResourceDetails(context.getResourceType(), key.toString(), name, version, description,
            pluginConfig, process);
}

From source file:org.kuali.test.runner.execution.TestExecutionContext.java

private void writePerformanceData(TestOperation op, long operationElaspedTime) {
    if (isPerformanceDataRequired()) {

        if (performanceData == null) {
            performanceData = new ArrayList<String[]>();
        }//from w  w w.  j a  v a 2 s.  c  o  m

        String submitElementName = getLastHttpSubmitElementName();
        ;

        if (op.getOperationType().equals(TestOperationType.HTTP_REQUEST)) {
            String[] rec = getInitializedPerformanceDataRecord();
            rec[8] = Constants.PERFORMANCE_ATTRIBUTE_TYPE_CLIENT;
            rec[9] = Constants.CLIENT_PERFORMANCE_ATTRIBUTE_HTTP_RESPONSE_TIME;
            rec[10] = Constants.PRIMITIVE_LONG_TYPE;
            rec[11] = "" + operationElaspedTime;
            rec[12] = submitElementName;
            rec[13] = (op.getOperation().getHtmlRequestOperation().getMethod() + ": "
                    + op.getOperation().getHtmlRequestOperation().getUrl());
            performanceData.add(rec);
        }

        JmxConnection jmxconn = Utils.findJmxConnection(configuration, platform.getJmxConnectionName());

        if ((jmxconn != null) && (jmxconn.getPerformanceMonitoringAttributes() != null)) {
            JMXConnector conn = null;
            try {
                conn = Utils.getJMXConnector(configuration, jmxconn);
                if (conn != null) {
                    MBeanServerConnection mbeanconn = conn.getMBeanServerConnection();
                    for (PerformanceMonitoringAttribute att : jmxconn.getPerformanceMonitoringAttributes()
                            .getPerformanceMonitoringAttributeArray()) {

                        Object value = "";
                        if (ManagementFactory.MEMORY_MXBEAN_NAME.equals(att.getType())) {
                            MemoryMXBean mbean = ManagementFactory.newPlatformMXBeanProxy(mbeanconn,
                                    ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
                            value = getValueFromMXBeanObject(att.getName(), mbean);
                        } else if (ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME.equals(att.getType())) {
                            OperatingSystemMXBean mbean = ManagementFactory.newPlatformMXBeanProxy(mbeanconn,
                                    att.getType(), OperatingSystemMXBean.class);
                            value = getValueFromMXBeanObject(att.getName(), mbean);
                        } else if (ManagementFactory.THREAD_MXBEAN_NAME.equals(att.getType())) {
                            ThreadMXBean mbean = ManagementFactory.newPlatformMXBeanProxy(mbeanconn,
                                    att.getType(), ThreadMXBean.class);
                            value = getValueFromMXBeanObject(att.getName(), mbean);
                        }

                        if ((value != null) && StringUtils.isNotBlank(value.toString())) {
                            String[] rec = getInitializedPerformanceDataRecord();

                            int pos = att.getType().indexOf(Constants.SEPARATOR_EQUALS);

                            if (pos > -1) {
                                rec[8] = att.getType().substring(pos + 1);
                            } else {
                                rec[8] = att.getType();
                            }
                            rec[9] = att.getName();
                            rec[10] = att.getType();
                            rec[11] = value.toString();
                            rec[12] = submitElementName;
                            rec[13] = att.getDescription();

                            performanceData.add(rec);
                        }
                    }
                }
            }

            catch (Exception ex) {
                LOG.error(ex.toString(), ex);
            }

            finally {
                if (conn != null) {
                    try {
                        conn.close();
                    }

                    catch (Exception ex) {
                    }
                    ;
                }
            }
        }
    }
}