Java Code Examples for javax.management.MBeanServer#getAttributes()

The following examples show how to use javax.management.MBeanServer#getAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimplePerformanceMeter.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public static double getProcessCpuLoadInternal() {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

    try {
        final ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
        final AttributeList list = mbs.getAttributes(name, new String[] { "ProcessCpuLoad" });

        if (list.isEmpty()) {
            return Double.NaN;
        }

        final Attribute att = (Attribute) list.get(0);
        final 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 (MalformedObjectNameException | NullPointerException | InstanceNotFoundException | ReflectionException e) {
        return Double.NaN;
    }
}
 
Example 2
Source File: SyncStats.java    From aion with MIT License 6 votes vote down vote up
private static double getProcessCpuLoad() {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    AttributeList list = null;
    try {
        ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
        list = mbs.getAttributes(name, new String[] {"ProcessCpuLoad"});
    } catch (InstanceNotFoundException | ReflectionException | MalformedObjectNameException e) {
        e.printStackTrace();
    }

    if (Objects.requireNonNull(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);
}
 
Example 3
Source File: CamelContextMetadataMBeanTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuilder() throws Exception {
    CamelContext context = new DefaultCamelContext();

    Properties properties = new Properties();
    properties.setProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, "metadata");

    PropertiesComponent pc  = new PropertiesComponent();
    pc.setInitialProperties(properties);
    context.setPropertiesComponent(pc);

    RuntimeSupport.configureContextCustomizers(context);

    context.start();

    final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    final Set<ObjectInstance> mBeans = mBeanServer.queryMBeans(ObjectName.getInstance("io.syndesis.camel:*"), null);
    assertThat(mBeans).hasSize(1);

    final ObjectName objectName = mBeans.iterator().next().getObjectName();
    final AttributeList attributes = mBeanServer.getAttributes(objectName, ATTRIBUTES);
    assertThat(attributes.asList()).hasSize(ATTRIBUTES.length);

    context.stop();
}
 
Example 4
Source File: JmxInfoProviderSupport.java    From ibm-cos-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public long[] getFileDecriptorInfo() {
    MBeanServer mbsc = MBeans.getMBeanServer();
    AttributeList attributes;
    try {
        attributes = mbsc.getAttributes(
            new ObjectName("java.lang:type=OperatingSystem"), 
            new String[]{"OpenFileDescriptorCount", "MaxFileDescriptorCount"});
        List<Attribute> attrList = attributes.asList();
        long openFdCount = (Long)attrList.get(0).getValue();
        long maxFdCount = (Long)attrList.get(1).getValue();
        long[] fdCounts = { openFdCount, maxFdCount};
        return fdCounts;
    } catch (Exception e) {
        LogFactory.getLog(SdkMBeanRegistrySupport.class).debug(
                "Failed to retrieve file descriptor info", e);
    }
    return null;
}
 
Example 5
Source File: CamelContextMetadataMBeanTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilder() throws Exception {
    final MBeanServer mBeanServer = JmxUtils.locateMBeanServer();
    final Set<ObjectInstance> mBeans = mBeanServer.queryMBeans(ObjectName.getInstance("io.syndesis.camel:*"), null);
    assertThat(mBeans).hasSize(1);

    final ObjectName objectName = mBeans.iterator().next().getObjectName();
    final AttributeList attributes = mBeanServer.getAttributes(objectName, ATTRIBUTES);
    assertThat(attributes.asList()).hasSize(ATTRIBUTES.length);
}
 
Example 6
Source File: SoaBundle.java    From soabase with Apache License 2.0 5 votes vote down vote up
private void addMetrics(Environment environment)
{
    Metric metric = new Gauge<Double>()
    {
        private double lastValue = 0.0;

        @Override
        public Double getValue()
        {
            try
            {
                MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
                ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
                AttributeList list = mbs.getAttributes(name, new String[]{"SystemCpuLoad"});
                if ( (list != null) && (list.size() > 0) )
                {
                    // unfortunately, this bean reports bad values occasionally. Filter them out.
                    Object value = list.asList().get(0).getValue();
                    double d = (value instanceof Number) ? ((Number)value).doubleValue() : 0.0;
                    d = ((d > 0.0) && (d < 1.0)) ? d : lastValue;
                    lastValue = d;
                    return d;
                }
            }
            catch ( Exception ignore )
            {
                // ignore
            }
            return lastValue;
        }
    };
    environment.metrics().register("system.cpu.load", metric);
}
 
Example 7
Source File: CPULoadProfiler.java    From statsd-jvm-profiler with MIT License 5 votes vote down vote up
public CPULoadProfiler(Reporter reporter, Arguments arguments) {
  super(reporter, arguments);
  try {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName os = ObjectName.getInstance("java.lang:type=OperatingSystem");
    list = mbs.getAttributes(os, ATTRIBUTES_MAP.keySet().toArray(new String[ATTRIBUTES_MAP.size()]));
  } catch (InstanceNotFoundException | ReflectionException | MalformedObjectNameException e) {
    list = null;
  }

}
 
Example 8
Source File: JavaMailJMSStatisticsTest.java    From javamail with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAttributes() throws Exception {
    List<String> attrNames = Arrays.asList("countSuccessful", "countFailure");
    MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
    AttributeList attributeList = platformMBeanServer.getAttributes(JavaMailJMSStatistics.JMX_OBJECT_NAME, attrNames.toArray(new String[attrNames.size()]));
    List<Attribute> list = attributeList.asList();
    for(Attribute attribute : list) {
        if (attrNames.contains(attribute.getName())) {
            Object val = attribute.getValue();
            assertTrue(val instanceof Number);
            continue;
        }
        fail("There is more attributes then expected - " + attribute);
    }
}