use of javax.management.AttributeList in project jmxtrans by jmxtrans.
the class JmxResultProcessorTest method canReadTabularData.
@Test
public void canReadTabularData() throws MalformedObjectNameException, AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException {
ObjectInstance runtime = getRuntime();
AttributeList attr = ManagementFactory.getPlatformMBeanServer().getAttributes(runtime.getObjectName(), new String[] { "SystemProperties" });
List<Result> results = new JmxResultProcessor(dummyQueryWithResultAlias(), runtime, attr.asList(), runtime.getClassName(), TEST_DOMAIN_NAME).getResults();
assertThat(results.size()).isGreaterThan(2);
Optional<Result> result = from(results).firstMatch(new ByAttributeName("SystemProperties.java.version"));
assertThat(result.isPresent()).isTrue();
assertThat(result.get().getAttributeName()).isEqualTo("SystemProperties.java.version");
Map<String, Object> values = result.get().getValues();
assertThat(values).hasSize(2);
Object objectValue = result.get().getValues().get("key");
assertThat(objectValue).isInstanceOf(String.class);
String key = (String) objectValue;
assertThat(key).isEqualTo("java.version");
}
use of javax.management.AttributeList in project jmxtrans by jmxtrans.
the class TreeWalker method walkTree.
public void walkTree(MBeanServerConnection connection) throws Exception {
// key here is null, null returns everything!
Set<ObjectName> mbeans = connection.queryNames(null, null);
for (ObjectName name : mbeans) {
MBeanInfo info = connection.getMBeanInfo(name);
MBeanAttributeInfo[] attrs = info.getAttributes();
String[] attrNames = new String[attrs.length];
for (int i = 0; i < attrs.length; i++) {
attrNames[i] = attrs[i].getName();
}
try {
AttributeList attributes = connection.getAttributes(name, attrNames);
for (Attribute attribute : attributes.asList()) {
output(name.getCanonicalName() + "%" + attribute.getName(), attribute.getValue());
}
} catch (Exception e) {
log.error("error getting " + name + ":" + e.getMessage(), e);
}
}
}
use of javax.management.AttributeList in project neo4j by neo4j.
the class Neo4jManager method getConfiguration.
public Map<String, Object> getConfiguration() {
final String[] keys;
final AttributeList attributes;
try {
MBeanAttributeInfo[] keyInfo = server.getMBeanInfo(config).getAttributes();
keys = new String[keyInfo.length];
for (int i = 0; i < keys.length; i++) {
keys[i] = keyInfo[i].getName();
}
attributes = server.getAttributes(config, keys);
} catch (Exception e) {
throw new IllegalStateException("Could not access the configuration bean", e);
}
Map<String, Object> configuration = new HashMap<>();
for (int i = 0; i < keys.length; i++) {
configuration.put(keys[i], attributes.get(i));
}
return configuration;
}
use of javax.management.AttributeList in project twitter4j by yusuke.
the class MBeansTest method testAPIStatisticsOpenMBean.
/**
* Tests exposure of API statistics via a dynamic MBean
*/
public void testAPIStatisticsOpenMBean() throws Exception {
APIStatistics stats = new APIStatistics(5);
APIStatisticsOpenMBean openMBean = new APIStatisticsOpenMBean(stats);
// sanity check to ensure metadata accurately describes dynamic attributes
MBeanInfo info = openMBean.getMBeanInfo();
assertEquals(5, info.getAttributes().length);
assertEquals(1, info.getOperations().length);
List<String> attrNames = new ArrayList<String>();
for (MBeanAttributeInfo attr : info.getAttributes()) {
assertNotNull(openMBean.getAttribute(attr.getName()));
attrNames.add(attr.getName());
}
AttributeList attrList = openMBean.getAttributes(attrNames.toArray(new String[attrNames.size()]));
assertNotNull(attrList);
assertEquals(5, attrList.size());
// check stats (empty case)
Long callCount = (Long) openMBean.getAttribute("callCount");
assertEquals(0, callCount.longValue());
Long errorCount = (Long) openMBean.getAttribute("errorCount");
assertEquals(0, callCount.longValue());
Long totalTime = (Long) openMBean.getAttribute("totalTime");
assertEquals(0, totalTime.longValue());
Long averageTime = (Long) openMBean.getAttribute("averageTime");
assertEquals(0, averageTime.longValue());
// check table (empty case)
TabularData table = (TabularData) openMBean.getAttribute("statisticsTable");
assertTrue(table.isEmpty());
stats.methodCalled("foo", 100, true);
// check stats (populated case)
callCount = (Long) openMBean.getAttribute("callCount");
assertEquals(1, callCount.longValue());
errorCount = (Long) openMBean.getAttribute("errorCount");
assertEquals(0, errorCount.longValue());
totalTime = (Long) openMBean.getAttribute("totalTime");
assertEquals(100, totalTime.longValue());
averageTime = (Long) openMBean.getAttribute("averageTime");
assertEquals(100, averageTime.longValue());
// check table (populated case)
table = (TabularData) openMBean.getAttribute("statisticsTable");
assertFalse(table.isEmpty());
assertEquals(1, table.keySet().size());
CompositeData data = table.get(new Object[] { "foo" });
assertNotNull(data);
String[] columnNames = new String[] { "methodName", "callCount", "totalTime", "avgTime" };
Object[] columnValues = data.getAll(columnNames);
assertEquals(columnNames.length, columnValues.length);
assertEquals("foo", columnValues[0]);
assertEquals(1, ((Long) columnValues[1]).longValue());
assertEquals(100, ((Long) columnValues[2]).longValue());
assertEquals(100, ((Long) columnValues[3]).longValue());
// check reset
openMBean.invoke("reset", new Object[0], new String[0]);
checkCalculator(stats, 0, 0, 0, 0);
assertFalse(stats.getInvocationStatistics().iterator().hasNext());
}
use of javax.management.AttributeList in project midpoint by Evolveum.
the class SystemInfoPanel method fillCpuUsage.
private void fillCpuUsage(SystemInfoDto dto) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
AttributeList list = mbs.getAttributes(name, new String[] { "ProcessCpuLoad" });
if (list.isEmpty()) {
dto.cpuUsage = Double.NaN;
return;
}
Attribute att = (Attribute) list.get(0);
Double value = (Double) att.getValue();
if (value == -1.0) {
// usually takes a couple of seconds before we get real values
dto.cpuUsage = Double.NaN;
return;
}
dto.cpuUsage = ((int) (value * 1000) / 10.0);
}
Aggregations