Search in sources :

Example 6 with MBeanServer

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"));
}
Also used : MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 7 with MBeanServer

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);
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanServer(javax.management.MBeanServer)

Example 8 with MBeanServer

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));
    }
}
Also used : ManagementAgent(org.apache.camel.spi.ManagementAgent) MetricRegistry(com.codahale.metrics.MetricRegistry) MetricsModule(com.codahale.metrics.json.MetricsModule) MetricRegistry(com.codahale.metrics.MetricRegistry) Registry(org.apache.camel.spi.Registry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MBeanServer(javax.management.MBeanServer)

Example 9 with MBeanServer

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);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MemoryMXBean(java.lang.management.MemoryMXBean) MetricGroup(org.apache.flink.metrics.MetricGroup) Gauge(org.apache.flink.metrics.Gauge) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 10 with MBeanServer

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();
        }
    }
}
Also used : MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) Configuration(org.apache.hadoop.conf.Configuration) ConfigBuilder(org.apache.hadoop.metrics2.impl.ConfigBuilder) ObjectName(javax.management.ObjectName) Map(java.util.Map) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Aggregations

MBeanServer (javax.management.MBeanServer)1218 ObjectName (javax.management.ObjectName)939 Test (org.junit.Test)214 MalformedObjectNameException (javax.management.MalformedObjectNameException)123 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)94 InstanceNotFoundException (javax.management.InstanceNotFoundException)87 IOException (java.io.IOException)82 JMXServiceURL (javax.management.remote.JMXServiceURL)70 Attribute (javax.management.Attribute)66 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)65 HashMap (java.util.HashMap)63 MBeanRegistrationException (javax.management.MBeanRegistrationException)56 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)54 TabularData (javax.management.openmbean.TabularData)51 ArrayList (java.util.ArrayList)47 JMXConnectorServer (javax.management.remote.JMXConnectorServer)47 JMXConnector (javax.management.remote.JMXConnector)40 Map (java.util.Map)38 JMException (javax.management.JMException)38 Test (org.junit.jupiter.api.Test)36