Example usage for java.lang.management ManagementFactory getPlatformMBeanServer

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

Introduction

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

Prototype

public static synchronized MBeanServer getPlatformMBeanServer() 

Source Link

Document

Returns the platform javax.management.MBeanServer MBeanServer .

Usage

From source file:com.taobao.tddl.common.util.TDDLMBeanServer.java

private TDDLMBeanServer() {
    // MBServer/*from   www  .ja  v  a2s.  c o m*/
    String hostName = null;
    try {
        InetAddress addr = InetAddress.getLocalHost();

        hostName = addr.getHostName();
    } catch (IOException e) {
        log.error(LogPrefix + "Get HostName Error", e);
        hostName = "localhost";
    }
    String host = System.getProperty("hostName", hostName);
    try {
        boolean useJmx = Boolean.parseBoolean(System.getProperty("tddl.useJMX", "true"));
        if (useJmx) {
            mbs = ManagementFactory.getPlatformMBeanServer();
            int port = Integer.parseInt(System.getProperty("tddl.rmi.port", "6679"));
            String rmiName = System.getProperty("tddl.rmi.name", "tddlJmxServer");
            Registry reg = null;
            try {
                reg = LocateRegistry.getRegistry(port);
                reg.list();
            } catch (Exception e) {
                reg = null;
            }
            if (null == reg) {
                reg = LocateRegistry.createRegistry(port);
            }
            reg.list();
            String serverURL = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/" + rmiName;
            JMXServiceURL url = new JMXServiceURL(serverURL);
            final JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url,
                    null, mbs);
            connectorServer.start();
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    try {
                        System.err.println("JMXConnector stop");
                        connectorServer.stop();
                    } catch (IOException e) {
                        log.error(LogPrefix + e);
                    }
                }
            });
            log.warn(LogPrefix + "jmx url: " + serverURL);
        }
    } catch (Exception e) {
        log.error(LogPrefix + "create MBServer error", e);
    }
}

From source file:co.runrightfast.core.utils.JmxUtils.java

static void registerApplicationMBean(final String domain, @NonNull final Object mbean,
        final Class<?> mbeanType) {
    final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    try {//w  w w  .  j  av a  2s  . c om
        mbeanServer.registerMBean(mbean, applicationMBeanObjectName(domain, mbeanType));
    } catch (final InstanceAlreadyExistsException | MBeanRegistrationException
            | NotCompliantMBeanException ex) {
        throw new ApplicationException(
                String.format("registerApplicationMBean() failed for: %s", mbean.getClass().getName()), ex);
    }
}

From source file:org.onehippo.forge.camel.util.CamelContextUtils.java

/**
 * Finds the {@code ManagedCamelContextMBean} having {@code camelContextId} from the platform MBean server and invokes the {@code operation}
 * with {@code params} of which signature is like {@code signature} on the found {@code ManagedCamelContext} MBean.
 * If a blank {@code camelContextId} provided, then it invokes the operation on the first found {@code ManagedCamelContextMBean} MBean.
 * <p>Example code:</p>/*from  www . j  a  v a2 s  . c om*/
 * <pre>
 * CamelContextUtils.invokeManagedCamelContextMBean("camel-1", "sendBody", new Object [] { "direct:test", body }, new String {} { String.class.getName(), Object.class.getName() });;
 * </pre>
 * @param camelContextId camel context ID
 * @param operation operation name
 * @param params parameters array
 * @param signature signature of parameters
 * @return operation return
 */
public static Object invokeManagedCamelContextMBean(final String camelContextId, final String operation,
        Object[] params, String[] signature) {
    Object ret = null;

    try {
        QueryExp expr = Query.isInstanceOf(new StringValueExp(CAMEL_CONTEXT_MBEAN_INSTANCE_TYPE));
        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();

        ObjectName objectName;
        if (StringUtils.isNotBlank(camelContextId)) {
            objectName = new ObjectName("org.apache.camel:name=" + camelContextId);
        } else {
            objectName = new ObjectName("org.apache.camel:*");
        }

        Set<ObjectName> objectNames = mbeanServer.queryNames(objectName, expr);

        if (!objectNames.isEmpty()) {
            final ObjectName firstObjectName = objectNames.iterator().next();
            ret = mbeanServer.invoke(firstObjectName, operation, params, signature);
        } else {
            throw new RuntimeException("No CamelContext found by name: " + objectName);
        }
    } catch (MalformedObjectNameException e) {
        throw new RuntimeException("MalformedObjectNameException: " + e, e);
    } catch (InstanceNotFoundException e) {
        throw new RuntimeException("InstanceNotFoundException: " + e, e);
    } catch (ReflectionException e) {
        throw new RuntimeException("ReflectionException: " + e, e);
    } catch (MBeanException e) {
        throw new RuntimeException("MBeanException: " + e, e);
    }

    return ret;
}

From source file:org.rhq.core.pc.PluginContainerMBeanImpl.java

public void register() {
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    try {/*from www. j a v a  2 s  . c om*/
        server.registerMBean(this, new ObjectName(OBJECT_NAME));
    } catch (Exception e) {
        log.error("Unable to register PluginContainerMBean", e);
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.TestDataNodeMXBean.java

@Test
public void testDataNodeMXBean() throws Exception {
    Configuration conf = new Configuration();
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).build();

    try {//from   w w w  .j  a  v a 2 s . c o m
        List<DataNode> datanodes = cluster.getDataNodes();
        Assert.assertEquals(datanodes.size(), 1);
        DataNode datanode = datanodes.get(0);

        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName mxbeanName = new ObjectName("Hadoop:service=DataNode,name=DataNodeInfo");
        // get attribute "ClusterId"
        String clusterId = (String) mbs.getAttribute(mxbeanName, "ClusterId");
        Assert.assertEquals(datanode.getClusterId(), clusterId);
        // get attribute "Version"
        String version = (String) mbs.getAttribute(mxbeanName, "Version");
        Assert.assertEquals(datanode.getVersion(), version);
        // get attribute "SotfwareVersion"
        String softwareVersion = (String) mbs.getAttribute(mxbeanName, "SoftwareVersion");
        Assert.assertEquals(datanode.getSoftwareVersion(), softwareVersion);
        Assert.assertEquals(version, softwareVersion + ", r" + datanode.getRevision());
        // get attribute "RpcPort"
        String rpcPort = (String) mbs.getAttribute(mxbeanName, "RpcPort");
        Assert.assertEquals(datanode.getRpcPort(), rpcPort);
        // get attribute "HttpPort"
        String httpPort = (String) mbs.getAttribute(mxbeanName, "HttpPort");
        Assert.assertEquals(datanode.getHttpPort(), httpPort);
        // get attribute "NamenodeAddresses"
        String namenodeAddresses = (String) mbs.getAttribute(mxbeanName, "NamenodeAddresses");
        Assert.assertEquals(datanode.getNamenodeAddresses(), namenodeAddresses);
        // get attribute "getVolumeInfo"
        String volumeInfo = (String) mbs.getAttribute(mxbeanName, "VolumeInfo");
        Assert.assertEquals(replaceDigits(datanode.getVolumeInfo()), replaceDigits(volumeInfo));
        // Ensure mxbean's XceiverCount is same as the DataNode's
        // live value.
        int xceiverCount = (Integer) mbs.getAttribute(mxbeanName, "XceiverCount");
        Assert.assertEquals(datanode.getXceiverCount(), xceiverCount);

        String bpActorInfo = (String) mbs.getAttribute(mxbeanName, "BPServiceActorInfo");
        Assert.assertEquals(datanode.getBPServiceActorInfo(), bpActorInfo);
    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}

From source file:org.sakaiproject.kernel.loader.server.test.DummySharedClassLoaderContainer.java

/**
 * Create a shared classloader object.//from   w  ww.j  a v  a  2 s.co  m
 * 
 * @param kernel
 *          the kernel to connect to.
 * @param shutdownService
 *          the shutdown service.
 * @throws JMRuntimeException
 * @throws JMException
 * @throws InvalidTargetObjectTypeException
 */
public DummySharedClassLoaderContainer(ClassLoader classLoader)
        throws JMRuntimeException, JMException, InvalidTargetObjectTypeException {
    this.sharedClassLoader = classLoader;
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    RequiredModelMBean model = new RequiredModelMBean(createMBeanInfo());
    model.setManagedResource(this, "objectReference");
    ObjectName common = new ObjectName(CommonObject.MBEAN_COMMON + ".sharedclassloader");
    mbs.registerMBean(model, common);
}

From source file:gov.nih.nci.cabig.caaers.tools.JMXBeansRegistrar.java

public void destroy() throws Exception {
    try {/*from  ww w  . j ava 2s  . c  om*/
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        for (SessionFactory sf : sessionFactories) {
            ObjectName on = prepareObjectName(sf);
            server.unregisterMBean(on);
        }
    } catch (Exception e) {
        log.error("Unable to unregister MBeans");
        log.error(ExceptionUtils.getFullStackTrace(e));
    }
}

From source file:org.openmrs.module.jmx.impl.JMXServiceImpl.java

/**
 * @see org.openmrs.module.jmx.JMXService#unregisterBean(String)
 *//*from   www.j av a  2s .  c  o m*/
@Override
public void unregisterMBean(String folder, String name) {
    try {
        ObjectName objName = getObjectName(folder, name);
        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
        if (beanServer.isRegistered(objName))
            beanServer.unregisterMBean(objName);

        log.debug("Unregistered MBean: " + objName.toString());

    } catch (MalformedObjectNameException e) {
        log.error("Invalid MBean name", e);
    } catch (Exception e) {
        log.error("Unable to unregister MBean", e);
    }
}

From source file:com.linuxbox.util.MBeanUtils.java

public static void unregisterMBean(ObjectName name) {
    if (name == null) {
        return;/*  w w  w .j  av  a2  s.c  o m*/
    }

    try {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        server.unregisterMBean(name);
    } catch (MBeanRegistrationException e) {
        LOGGER.error("could not unregister mbean " + name, e);
    } catch (InstanceNotFoundException e) {
        LOGGER.error("could not find mbean " + name, e);
    }
}

From source file:com.zenoss.jmx.JmxClient.java

/**
 * Creates a JmxClient for interacting with the local Platform MBeanServer
 * 
 */
public JmxClient() {
    _server = ManagementFactory.getPlatformMBeanServer();
    _connected = true;
}