use of javax.management.MBeanServer in project groovy by apache.
the class MBeanTest method testGetProperty.
public void testGetProperty() throws Exception {
MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("groovy.test:role=TestMBean,type=Dummy");
// use Class.forName instead of new Dummy() to allow separate compilation
mbeanServer.registerMBean(Class.forName("groovy.util.Dummy").newInstance(), name);
assertEquals("JMX value of Name", "James", mbeanServer.getAttribute(name, "Name"));
GroovyMBean object = new GroovyMBean(mbeanServer, name);
Object value = object.getProperty("Name");
assertEquals("Name property", "James", value);
object.setProperty("Name", "Bob");
assertEquals("Name property", "Bob", object.getProperty("Name"));
// now let's look up the name via JMX to check
assertEquals("JMX value of Name", "Bob", mbeanServer.getAttribute(name, "Name"));
assertEquals("Location : London|Name : Bob|Size : 12", join(sort(object.listAttributeValues()), "|"));
assertEquals("start|stop", join(sort(object.listOperationNames()), "|"));
assertEquals("void start()", join(sort(object.describeOperation("start")), "|"));
assertEquals("(rw) java.lang.String Location", object.describeAttribute("Location"));
}
use of javax.management.MBeanServer in project hadoop by apache.
the class MBeans method unregister.
public static void unregister(ObjectName mbeanName) {
LOG.debug("Unregistering " + mbeanName);
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
if (mbeanName == null) {
LOG.debug("Stacktrace: ", new Throwable());
return;
}
try {
mbs.unregisterMBean(mbeanName);
} catch (Exception e) {
LOG.warn("Error unregistering " + mbeanName, e);
}
DefaultMetricsSystem.removeMBeanName(mbeanName);
}
use of javax.management.MBeanServer in project camel by apache.
the class MetricsMessageHistoryService method doStart.
@Override
protected void doStart() throws Exception {
if (metricsRegistry == null) {
Registry camelRegistry = getCamelContext().getRegistry();
metricsRegistry = camelRegistry.lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class);
// create a new metricsRegistry by default
if (metricsRegistry == null) {
metricsRegistry = new MetricRegistry();
}
}
if (useJmx) {
ManagementAgent agent = getCamelContext().getManagementStrategy().getManagementAgent();
if (agent != null) {
MBeanServer server = agent.getMBeanServer();
if (server != null) {
reporter = JmxReporter.forRegistry(metricsRegistry).registerWith(server).inDomain(jmxDomain).build();
reporter.start();
}
} else {
throw new IllegalStateException("CamelContext has not enabled JMX");
}
}
// json mapper
this.mapper = new ObjectMapper().registerModule(new MetricsModule(getRateUnit(), getDurationUnit(), false));
if (getRateUnit() == TimeUnit.SECONDS && getDurationUnit() == TimeUnit.SECONDS) {
// they both use same units so reuse
this.secondsMapper = this.mapper;
} else {
this.secondsMapper = new ObjectMapper().registerModule(new MetricsModule(TimeUnit.SECONDS, TimeUnit.SECONDS, false));
}
}
use of javax.management.MBeanServer in project flink by apache.
the class TaskExecutorMetricsInitializer method instantiateMemoryMetrics.
private static void instantiateMemoryMetrics(MetricGroup metrics) {
final MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean();
MetricGroup heap = metrics.addGroup("Heap");
heap.<Long, Gauge<Long>>gauge("Used", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getUsed();
}
});
heap.<Long, Gauge<Long>>gauge("Committed", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getCommitted();
}
});
heap.<Long, Gauge<Long>>gauge("Max", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getMax();
}
});
MetricGroup nonHeap = metrics.addGroup("NonHeap");
nonHeap.<Long, Gauge<Long>>gauge("Used", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getUsed();
}
});
nonHeap.<Long, Gauge<Long>>gauge("Committed", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getCommitted();
}
});
nonHeap.<Long, Gauge<Long>>gauge("Max", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getMax();
}
});
final MBeanServer con = ManagementFactory.getPlatformMBeanServer();
final String directBufferPoolName = "java.nio:type=BufferPool,name=direct";
try {
final ObjectName directObjectName = new ObjectName(directBufferPoolName);
MetricGroup direct = metrics.addGroup("Direct");
direct.<Long, Gauge<Long>>gauge("Count", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "Count", -1L));
direct.<Long, Gauge<Long>>gauge("MemoryUsed", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "MemoryUsed", -1L));
direct.<Long, Gauge<Long>>gauge("TotalCapacity", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "TotalCapacity", -1L));
} catch (MalformedObjectNameException e) {
LOG.warn("Could not create object name {}.", directBufferPoolName, e);
}
final String mappedBufferPoolName = "java.nio:type=BufferPool,name=mapped";
try {
final ObjectName mappedObjectName = new ObjectName(mappedBufferPoolName);
MetricGroup mapped = metrics.addGroup("Mapped");
mapped.<Long, Gauge<Long>>gauge("Count", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "Count", -1L));
mapped.<Long, Gauge<Long>>gauge("MemoryUsed", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "MemoryUsed", -1L));
mapped.<Long, Gauge<Long>>gauge("TotalCapacity", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "TotalCapacity", -1L));
} catch (MalformedObjectNameException e) {
LOG.warn("Could not create object name {}.", mappedBufferPoolName, e);
}
}
use of javax.management.MBeanServer in project hadoop by apache.
the class TestFSNamesystemMBean method test.
@Test
public void test() throws Exception {
Configuration conf = new Configuration();
MiniDFSCluster cluster = null;
try {
cluster = new MiniDFSCluster.Builder(conf).build();
cluster.waitActive();
FSNamesystem fsn = cluster.getNameNode().namesystem;
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName mxbeanName = new ObjectName("Hadoop:service=NameNode,name=FSNamesystemState");
String snapshotStats = (String) (mbs.getAttribute(mxbeanName, "SnapshotStats"));
@SuppressWarnings("unchecked") Map<String, Object> stat = (Map<String, Object>) JSON.parse(snapshotStats);
assertTrue(stat.containsKey("SnapshottableDirectories") && (Long) stat.get("SnapshottableDirectories") == fsn.getNumSnapshottableDirs());
assertTrue(stat.containsKey("Snapshots") && (Long) stat.get("Snapshots") == fsn.getNumSnapshots());
Object pendingDeletionBlocks = mbs.getAttribute(mxbeanName, "PendingDeletionBlocks");
assertNotNull(pendingDeletionBlocks);
assertTrue(pendingDeletionBlocks instanceof Long);
Object encryptionZones = mbs.getAttribute(mxbeanName, "NumEncryptionZones");
assertNotNull(encryptionZones);
assertTrue(encryptionZones instanceof Integer);
} finally {
if (cluster != null) {
cluster.shutdown();
}
}
}
Aggregations