Search in sources :

Example 56 with Attribute

use of javax.management.Attribute in project jackrabbit-oak by apache.

the class OakOSGiRepositoryFactory method registerMBeanServer.

/**
     * Registers the Platform MBeanServer as OSGi service. This would enable
     * Aries JMX Whitboard support to then register the JMX MBean which are registered as OSGi service
     * to be registered against the MBean server
     */
private static void registerMBeanServer(PojoServiceRegistry registry) {
    MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
    Hashtable<String, Object> mbeanProps = new Hashtable<String, Object>();
    try {
        ObjectName beanName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
        AttributeList attrs = platformMBeanServer.getAttributes(beanName, new String[] { "MBeanServerId", "SpecificationName", "SpecificationVersion", "SpecificationVendor", "ImplementationName", "ImplementationVersion", "ImplementationVendor" });
        for (Object object : attrs) {
            Attribute attr = (Attribute) object;
            if (attr.getValue() != null) {
                mbeanProps.put(attr.getName(), attr.getValue().toString());
            }
        }
    } catch (Exception je) {
        log.info("Cannot set service properties of Platform MBeanServer service, registering without", je);
    }
    registry.registerService(MBeanServer.class.getName(), platformMBeanServer, mbeanProps);
}
Also used : Attribute(javax.management.Attribute) Hashtable(java.util.Hashtable) AttributeList(javax.management.AttributeList) TimeoutException(java.util.concurrent.TimeoutException) RepositoryException(javax.jcr.RepositoryException) BundleException(org.osgi.framework.BundleException) ExecutionException(java.util.concurrent.ExecutionException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 57 with Attribute

use of javax.management.Attribute in project karaf by apache.

the class LogTest method setDebugViaMBean.

@Test
public void setDebugViaMBean() throws Exception {
    assertSetLevel("INFO");
    MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = new ObjectName("org.apache.karaf:type=log,name=root");
    Attribute attribute = new Attribute("Level", "DEBUG");
    mbeanServer.setAttribute(name, attribute);
    String logLevel = (String) mbeanServer.getAttribute(name, "Level");
    assertEquals("DEBUG", logLevel);
}
Also used : Attribute(javax.management.Attribute) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 58 with Attribute

use of javax.management.Attribute in project samza by apache.

the class TestJmxAppender method testJmxAppender.

@Test
public void testJmxAppender() throws Exception {
    MBeanServerConnection mbserver = JMXConnectorFactory.connect(URL).getMBeanServerConnection();
    ObjectName objectName = new ObjectName(JmxAppender.JMX_OBJECT_DOMAIN + ":type=" + JmxAppender.JMX_OBJECT_TYPE + ",name=" + JmxAppender.JMX_OBJECT_NAME);
    String level = null;
    MockAppender mockAppender = new MockAppender();
    Logger.getRootLogger().addAppender(mockAppender);
    // Check INFO is set (from log4j.xml).
    level = (String) mbserver.getAttribute(objectName, "Level");
    assertEquals("INFO", level);
    log.info("info1");
    log.debug("debug1");
    // Set to debug.
    mbserver.setAttribute(objectName, new Attribute("Level", "debug"));
    // Check DEBUG is set.
    level = (String) mbserver.getAttribute(objectName, "Level");
    assertEquals("DEBUG", level);
    log.info("info2");
    log.debug("debug2");
    List<LoggingEvent> logLines = mockAppender.getLogLines();
    // Should not have debug1 because log level is info at first.
    Iterator<LoggingEvent> logLineIterator = logLines.iterator();
    assertEquals(3, logLines.size());
    assertEquals("info1", logLineIterator.next().getMessage());
    assertEquals("info2", logLineIterator.next().getMessage());
    assertEquals("debug2", logLineIterator.next().getMessage());
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Attribute(javax.management.Attribute) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 59 with Attribute

use of javax.management.Attribute in project sling by apache.

the class HealthCheckMBean method getAttribute.

@Override
public Object getAttribute(final String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
    // we should call getAttributes - and not vice versa to have the result
    // of a single check call - and not do a check call for each attribute
    final AttributeList result = this.getAttributes(new String[] { attribute });
    if (result.size() == 0) {
        throw new AttributeNotFoundException(attribute);
    }
    final Attribute attr = (Attribute) result.get(0);
    return attr.getValue();
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList)

Example 60 with Attribute

use of javax.management.Attribute in project intellij-community by JetBrains.

the class CpuTimings method getProcessCpuLoad.

public static double getProcessCpuLoad() {
    try {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
        AttributeList list = mbs.getAttributes(name, new String[] { "ProcessCpuLoad" });
        if (list.isEmpty())
            return Double.NaN;
        Attribute att = (Attribute) list.get(0);
        Double value = (Double) att.getValue();
        // usually takes a couple of seconds before we get real values
        if (value == -1.0)
            return Double.NaN;
        // returns a percentage value with 1 decimal point precision
        return ((int) (value * 1000) / 10.0);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Aggregations

Attribute (javax.management.Attribute)157 ObjectName (javax.management.ObjectName)94 MBeanServer (javax.management.MBeanServer)56 AttributeList (javax.management.AttributeList)46 Test (org.junit.Test)38 ReflectionException (javax.management.ReflectionException)29 MBeanException (javax.management.MBeanException)25 HashMap (java.util.HashMap)23 InstanceNotFoundException (javax.management.InstanceNotFoundException)22 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)21 AttributeNotFoundException (javax.management.AttributeNotFoundException)20 MBeanServerConnection (javax.management.MBeanServerConnection)15 MBeanInfo (javax.management.MBeanInfo)14 JMXConnector (javax.management.remote.JMXConnector)13 RuntimeOperationsException (javax.management.RuntimeOperationsException)12 List (java.util.List)10 BacklogTracerEventMessage (org.apache.camel.api.management.mbean.BacklogTracerEventMessage)10 IOException (java.io.IOException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 ArrayList (java.util.ArrayList)9