Example usage for javax.management MBeanServer getAttribute

List of usage examples for javax.management MBeanServer getAttribute

Introduction

In this page you can find the example usage for javax.management MBeanServer getAttribute.

Prototype

public Object getAttribute(ObjectName name, String attribute)
        throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException;

Source Link

Usage

From source file:org.apache.hadoop.hdfs.server.namenode.TestNameNodeMXBean.java

@Test(timeout = 120000)
public void testTopUsersDisabled() throws Exception {
    final Configuration conf = new Configuration();
    // Disable nntop
    conf.setBoolean(DFSConfigKeys.NNTOP_ENABLED_KEY, false);
    MiniDFSCluster cluster = null;/*from   w  w  w.  j a v a 2s .c o m*/
    try {
        cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build();
        cluster.waitActive();
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName mxbeanNameFsns = new ObjectName("Hadoop:service=NameNode,name=FSNamesystemState");
        FileSystem fs = cluster.getFileSystem();
        final Path path = new Path("/");
        final int NUM_OPS = 10;
        for (int i = 0; i < NUM_OPS; i++) {
            fs.listStatus(path);
            fs.setTimes(path, 0, 1);
        }
        String topUsers = (String) (mbs.getAttribute(mxbeanNameFsns, "TopUserOpCounts"));
        assertNull("Did not expect to find TopUserOpCounts bean!", topUsers);
    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestNameNodeMXBean.java

@Test(timeout = 120000)
public void testTopUsersNoPeriods() throws Exception {
    final Configuration conf = new Configuration();
    conf.setBoolean(DFSConfigKeys.NNTOP_ENABLED_KEY, true);
    conf.set(DFSConfigKeys.NNTOP_WINDOWS_MINUTES_KEY, "");
    MiniDFSCluster cluster = null;//from   w w w.java 2s  .c  o  m
    try {
        cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build();
        cluster.waitActive();
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName mxbeanNameFsns = new ObjectName("Hadoop:service=NameNode,name=FSNamesystemState");
        FileSystem fs = cluster.getFileSystem();
        final Path path = new Path("/");
        final int NUM_OPS = 10;
        for (int i = 0; i < NUM_OPS; i++) {
            fs.listStatus(path);
            fs.setTimes(path, 0, 1);
        }
        String topUsers = (String) (mbs.getAttribute(mxbeanNameFsns, "TopUserOpCounts"));
        assertNotNull("Expected TopUserOpCounts bean!", topUsers);
    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}

From source file:com.googlecode.psiprobe.beans.JBossResourceResolverBean.java

public List getApplicationResources() throws NamingException {

    List resources = new ArrayList();

    MBeanServer server = getMBeanServer();
    if (server != null) {
        try {//from w  w  w  .  j av a 2s . c om
            Set dsNames = server.queryNames(new ObjectName("jboss.jca:service=ManagedConnectionPool,*"), null);
            for (Iterator it = dsNames.iterator(); it.hasNext();) {
                ObjectName managedConnectionPoolOName = (ObjectName) it.next();

                ApplicationResource resource = new ApplicationResource();
                resource.setName(managedConnectionPoolOName.getKeyProperty("name"));
                resource.setType("jboss");
                String criteria = (String) server.getAttribute(managedConnectionPoolOName, "Criteria");
                if ("ByApplication".equals(criteria)) {
                    resource.setAuth("Application");
                } else if ("ByContainerAndApplication".equals(criteria)) {
                    resource.setAuth("Both");
                } else {
                    resource.setAuth("Container");
                }
                DataSourceInfo dsInfo = new DataSourceInfo();
                dsInfo.setMaxConnections(
                        ((Integer) server.getAttribute(managedConnectionPoolOName, "MaxSize")).intValue());
                dsInfo.setEstablishedConnections(
                        ((Integer) server.getAttribute(managedConnectionPoolOName, "ConnectionCount"))
                                .intValue());
                dsInfo.setBusyConnections(
                        ((Long) server.getAttribute(managedConnectionPoolOName, "InUseConnectionCount"))
                                .intValue());

                ObjectName connectionFactoryOName = new ObjectName(
                        "jboss.jca:service=ManagedConnectionFactory,name=" + resource.getName());
                Element elm = (Element) server.getAttribute(connectionFactoryOName,
                        "ManagedConnectionFactoryProperties");

                if (elm != null) {
                    NodeList nl = elm.getChildNodes();
                    for (int i = 0; i < nl.getLength(); i++) {
                        Node n = nl.item(i);
                        Node na = n.getAttributes().getNamedItem("name");
                        if (na != null) {
                            if ("ConnectionURL".equals(na.getNodeValue())) {
                                dsInfo.setJdbcURL(n.getFirstChild().getNodeValue());
                            }

                            if ("UserName".equals(na.getNodeValue())) {
                                dsInfo.setUsername(n.getFirstChild().getNodeValue());
                            }

                            //
                            // JMS datasource
                            //
                            if ("JmsProviderAdapterJNDI".equals(na.getNodeValue())) {
                                dsInfo.setJdbcURL(n.getFirstChild().getNodeValue());
                                resource.setType("jms");
                            }
                        }
                    }
                }

                dsInfo.setResettable(true);

                resource.setDataSourceInfo(dsInfo);
                resources.add(resource);
            }
        } catch (Exception e) {
            //
            logger.fatal("There was an error querying JBoss JMX server:", e);
        }
    }
    return resources;
}

From source file:org.apache.streams.jackson.DatumStatusCounterDeserializer.java

@Override
public DatumStatusCounterBroadcast deserialize(JsonParser jsonParser,
        DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    try {/*from w ww  .ja  v  a 2 s  . co  m*/
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        DatumStatusCounterBroadcast datumStatusCounterBroadcast = new DatumStatusCounterBroadcast();
        JsonNode attributes = jsonParser.getCodec().readTree(jsonParser);

        ObjectName name = new ObjectName(attributes.get("canonicalName").asText());
        MBeanInfo info = server.getMBeanInfo(name);
        datumStatusCounterBroadcast.setName(name.toString());

        for (MBeanAttributeInfo attribute : Arrays.asList(info.getAttributes())) {
            try {
                switch (attribute.getName()) {
                case "Failed":
                    datumStatusCounterBroadcast
                            .setFailed((boolean) server.getAttribute(name, attribute.getName()));
                    break;
                case "Passed":
                    datumStatusCounterBroadcast
                            .setPassed((boolean) server.getAttribute(name, attribute.getName()));
                    break;
                }
            } catch (Exception e) {
                LOGGER.error("Exception trying to deserialize DatumStatusCounterBroadcast object: {}", e);
            }
        }

        return datumStatusCounterBroadcast;
    } catch (Exception e) {
        LOGGER.error("Exception trying to deserialize DatumStatusCounterBroadcast object: {}", e);
        return null;
    }
}

From source file:psiprobe.beans.JvmMemoryInfoAccessorBean.java

/**
 * Gets the pools./*from w ww.  j  av  a 2s.co  m*/
 *
 * @return the pools
 * @throws Exception the exception
 */
public List<MemoryPool> getPools() throws Exception {

    List<MemoryPool> memoryPools = new LinkedList<>();
    MBeanServer mbeanServer = new Registry().getMBeanServer();
    Set<ObjectInstance> memoryOPools = mbeanServer.queryMBeans(new ObjectName("java.lang:type=MemoryPool,*"),
            null);

    // totals
    long totalInit = 0;
    long totalMax = 0;
    long totalUsed = 0;
    long totalCommitted = 0;

    for (ObjectInstance oi : memoryOPools) {
        ObjectName objName = oi.getObjectName();
        MemoryPool memoryPool = new MemoryPool();
        memoryPool.setName(JmxTools.getStringAttr(mbeanServer, objName, "Name"));
        memoryPool.setType(JmxTools.getStringAttr(mbeanServer, objName, "Type"));

        CompositeDataSupport cd = (CompositeDataSupport) mbeanServer.getAttribute(objName, "Usage");
        /*
         * It seems that "Usage" attribute of one of the pools may turn into null intermittently. We
         * better have a dip in the graph then an NPE though.
         */
        if (cd != null) {
            memoryPool.setMax(JmxTools.getLongAttr(cd, "max"));
            memoryPool.setUsed(JmxTools.getLongAttr(cd, "used"));
            memoryPool.setInit(JmxTools.getLongAttr(cd, "init"));
            memoryPool.setCommitted(JmxTools.getLongAttr(cd, "committed"));
        } else {
            logger.error("Oops, JVM problem? {} 'Usage' attribute is NULL!", objName);
        }

        totalInit += memoryPool.getInit();
        totalMax += memoryPool.getMax();
        totalUsed += memoryPool.getUsed();
        totalCommitted += memoryPool.getCommitted();

        memoryPools.add(memoryPool);
    }

    if (!memoryPools.isEmpty()) {
        MemoryPool pool = new MemoryPool();
        pool.setName("Total");
        pool.setType("TOTAL");
        pool.setInit(totalInit);
        pool.setUsed(totalUsed);
        pool.setMax(totalMax);
        pool.setCommitted(totalCommitted);
        memoryPools.add(pool);
    }

    return memoryPools;

}

From source file:org.apache.hadoop.hdfs.server.namenode.TestNameNodeMXBean.java

@Test(timeout = 120000)
@SuppressWarnings("unchecked")
public void testTopUsers() throws Exception {
    final Configuration conf = new Configuration();
    MiniDFSCluster cluster = null;/*from   www.ja  v  a2 s. c o  m*/
    try {
        cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build();
        cluster.waitActive();
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName mxbeanNameFsns = new ObjectName("Hadoop:service=NameNode,name=FSNamesystemState");
        FileSystem fs = cluster.getFileSystem();
        final Path path = new Path("/");
        final int NUM_OPS = 10;
        for (int i = 0; i < NUM_OPS; i++) {
            fs.listStatus(path);
            fs.setTimes(path, 0, 1);
        }
        String topUsers = (String) (mbs.getAttribute(mxbeanNameFsns, "TopUserOpCounts"));
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = mapper.readValue(topUsers, Map.class);
        assertTrue("Could not find map key timestamp", map.containsKey("timestamp"));
        assertTrue("Could not find map key windows", map.containsKey("windows"));
        List<Map<String, List<Map<String, Object>>>> windows = (List<Map<String, List<Map<String, Object>>>>) map
                .get("windows");
        assertEquals("Unexpected num windows", 3, windows.size());
        for (Map<String, List<Map<String, Object>>> window : windows) {
            final List<Map<String, Object>> ops = window.get("ops");
            assertEquals("Unexpected num ops", 3, ops.size());
            for (Map<String, Object> op : ops) {
                final long count = Long.parseLong(op.get("totalCount").toString());
                final String opType = op.get("opType").toString();
                final int expected;
                if (opType.equals(TopConf.ALL_CMDS)) {
                    expected = 2 * NUM_OPS;
                } else {
                    expected = NUM_OPS;
                }
                assertEquals("Unexpected total count", expected, count);
            }
        }
    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}

From source file:org.apache.streams.jackson.ThroughputQueueDeserializer.java

@Override
public ThroughputQueueBroadcast deserialize(JsonParser jsonParser,
        DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    try {/*  w  w w. j av a2  s  .co  m*/
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        ThroughputQueueBroadcast throughputQueueBroadcast = new ThroughputQueueBroadcast();
        JsonNode attributes = jsonParser.getCodec().readTree(jsonParser);

        ObjectName name = new ObjectName(attributes.get("canonicalName").asText());
        MBeanInfo info = server.getMBeanInfo(name);
        throughputQueueBroadcast.setName(name.toString());

        for (MBeanAttributeInfo attribute : Arrays.asList(info.getAttributes())) {
            try {
                switch (attribute.getName()) {
                case "CurrentSize":
                    throughputQueueBroadcast
                            .setCurrentSize((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "AvgWait":
                    throughputQueueBroadcast
                            .setAvgWait((double) server.getAttribute(name, attribute.getName()));
                    break;
                case "MaxWait":
                    throughputQueueBroadcast.setMaxWait((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "Removed":
                    throughputQueueBroadcast.setRemoved((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "Added":
                    throughputQueueBroadcast.setAdded((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "Throughput":
                    throughputQueueBroadcast
                            .setThroughput((double) server.getAttribute(name, attribute.getName()));
                    break;
                }
            } catch (Exception e) {
                LOGGER.error("Exception while trying to deserialize ThroughputQueueBroadcast object: {}", e);
            }
        }

        return throughputQueueBroadcast;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.TestSpaceReservation.java

@Test(timeout = 30000)
public void testReservedSpaceInJMXBean() throws Exception {

    final short replication = 1;
    startCluster(BLOCK_SIZE, replication, -1);

    final String methodName = GenericTestUtils.getMethodName();
    final Path file = new Path("/" + methodName + ".01.dat");

    try (FSDataOutputStream os = fs.create(file, replication)) {
        // Write 1 byte to the file
        os.write(new byte[1]);
        os.hsync();/*from w  ww  .  j a  v  a2s.  c o  m*/

        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        final ObjectName mxbeanName = new ObjectName("Hadoop:service=DataNode,name=DataNodeInfo");
        final String volumeInfo = (String) mbs.getAttribute(mxbeanName, "VolumeInfo");

        // verify reserved space for Replicas in JMX bean volume info
        assertTrue(volumeInfo.contains("reservedSpaceForReplicas"));
    }
}

From source file:org.apache.streams.jackson.StreamsTaskCounterDeserializer.java

@Override
public StreamsTaskCounterBroadcast deserialize(JsonParser jsonParser,
        DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    try {//  w ww .  j a v a 2s.c  om
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        StreamsTaskCounterBroadcast streamsTaskCounterBroadcast = new StreamsTaskCounterBroadcast();
        JsonNode attributes = jsonParser.getCodec().readTree(jsonParser);

        ObjectName name = new ObjectName(attributes.get("canonicalName").asText());
        MBeanInfo info = server.getMBeanInfo(name);
        streamsTaskCounterBroadcast.setName(name.toString());

        for (MBeanAttributeInfo attribute : Arrays.asList(info.getAttributes())) {
            try {
                switch (attribute.getName()) {
                case "ErrorRate":
                    streamsTaskCounterBroadcast
                            .setErrorRate((double) server.getAttribute(name, attribute.getName()));
                    break;
                case "NumEmitted":
                    streamsTaskCounterBroadcast
                            .setNumEmitted((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "NumReceived":
                    streamsTaskCounterBroadcast
                            .setNumReceived((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "NumUnhandledErrors":
                    streamsTaskCounterBroadcast
                            .setNumUnhandledErrors((long) server.getAttribute(name, attribute.getName()));
                    break;
                case "AvgTime":
                    streamsTaskCounterBroadcast
                            .setAvgTime((double) server.getAttribute(name, attribute.getName()));
                    break;
                case "MaxTime":
                    streamsTaskCounterBroadcast
                            .setMaxTime((long) server.getAttribute(name, attribute.getName()));
                    break;
                }
            } catch (Exception e) {
                LOGGER.error("Exception while trying to deserialize StreamsTaskCounterBroadcast object: {}", e);
            }
        }

        return streamsTaskCounterBroadcast;
    } catch (Exception e) {
        LOGGER.error("Exception while trying to deserialize StreamsTaskCounterBroadcast object: {}", e);
        return null;
    }
}

From source file:com.googlecode.psiprobe.beans.JvmMemoryInfoAccessorBean.java

public List getPools() throws Exception {

    List memoryPools = new LinkedList();
    MBeanServer mBeanServer = new Registry().getMBeanServer();
    Set memoryOPools = mBeanServer.queryMBeans(new ObjectName("java.lang:type=MemoryPool,*"), null);

    ///*from  w  w w  .ja v  a  2s  .co  m*/
    // totals
    //
    long totalInit = 0;
    long totalMax = 0;
    long totalUsed = 0;
    long totalCommitted = 0;

    for (Iterator it = memoryOPools.iterator(); it.hasNext();) {
        ObjectInstance oi = (ObjectInstance) it.next();
        ObjectName oName = oi.getObjectName();
        MemoryPool memoryPool = new MemoryPool();
        memoryPool.setName(JmxTools.getStringAttr(mBeanServer, oName, "Name"));
        memoryPool.setType(JmxTools.getStringAttr(mBeanServer, oName, "Type"));

        CompositeDataSupport cd = (CompositeDataSupport) mBeanServer.getAttribute(oName, "Usage");
        //
        // It seems that "Usage" attribute of one of the pools may turn into null intermittently. We better have a
        // dip in the graph then an NPE though.
        //
        if (cd != null) {
            memoryPool.setMax(JmxTools.getLongAttr(cd, "max"));
            memoryPool.setUsed(JmxTools.getLongAttr(cd, "used"));
            memoryPool.setInit(JmxTools.getLongAttr(cd, "init"));
            memoryPool.setCommitted(JmxTools.getLongAttr(cd, "committed"));
        } else {
            logger.error("Oops, JVM problem? " + oName.toString() + " \"Usage\" attribute is NULL!");
        }

        totalInit += memoryPool.getInit();
        totalMax += memoryPool.getMax();
        totalUsed += memoryPool.getUsed();
        totalCommitted += memoryPool.getCommitted();

        memoryPools.add(memoryPool);
    }

    if (!memoryPools.isEmpty()) {
        MemoryPool pool = new MemoryPool();
        pool.setName("Total");
        pool.setType("TOTAL");
        pool.setInit(totalInit);
        pool.setUsed(totalUsed);
        pool.setMax(totalMax);
        pool.setCommitted(totalCommitted);
        memoryPools.add(pool);
    }

    return memoryPools;

}