javax.management.MBeanAttributeInfo Java Examples

The following examples show how to use javax.management.MBeanAttributeInfo. 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: JMXGet.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * print all attributes' values
 */
public void printAllValues() throws Exception {
  err("List of all the available keys:");

  Object val = null;

  for (ObjectName oname : hadoopObjectNames) {
    err(">>>>>>>>jmx name: " + oname.getCanonicalKeyPropertyListString());
    MBeanInfo mbinfo = mbsc.getMBeanInfo(oname);
    MBeanAttributeInfo[] mbinfos = mbinfo.getAttributes();

    for (MBeanAttributeInfo mb : mbinfos) {
      val = mbsc.getAttribute(oname, mb.getName());
      System.out.format(format, mb.getName(), (val==null)?"":val.toString());
    }
  }
}
 
Example #2
Source File: JmxManagementInterface.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private ModelNode getInfo(ObjectName objectName) {
    MBeanServerConnection connection = getConnection();
    ModelNode attributes = null;
    ModelNode headers = null;
    Exception exception = null;
    try {
        MBeanInfo mBeanInfo = connection.getMBeanInfo(objectName);
        MBeanAttributeInfo[] attributeInfos = mBeanInfo.getAttributes();
        ModelNode[] data = modelNodeAttributesInfo(attributeInfos, objectName);
        attributes = data[0];
        headers = data[1];
    } catch (Exception e) {
        if (e instanceof JMException || e instanceof JMRuntimeException) {
            exception = e;
        } else {
            throw new RuntimeException(e);
        }
    }
    return modelNodeResult(attributes, exception, headers);
}
 
Example #3
Source File: DynamicMBeanFactoryAttributesTest.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@Test
public void returnsInfoAboutWritableAttributesInMBeanInfo() {
	MBeanWithSettableAttributes settableMBean = new MBeanWithSettableAttributes(10, 20, "data");
	DynamicMBean mbean = createDynamicMBeanFor(settableMBean);

	MBeanInfo mBeanInfo = mbean.getMBeanInfo();

	MBeanAttributeInfo[] attributesInfoArr = mBeanInfo.getAttributes();
	Map<String, MBeanAttributeInfo> nameToAttr = nameToAttribute(attributesInfoArr);

	assertEquals(3, nameToAttr.size());

	assertTrue(nameToAttr.containsKey("notSettableInt"));
	assertFalse(nameToAttr.get("notSettableInt").isWritable());

	assertTrue(nameToAttr.containsKey("settableInt"));
	assertTrue(nameToAttr.get("settableInt").isWritable());

	assertTrue(nameToAttr.containsKey("settableStr"));
	assertTrue(nameToAttr.get("settableStr").isWritable());
}
 
Example #4
Source File: DefaultManagementMBeanAssemblerTest.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testHappyPath() throws MalformedObjectNameException, JMException {
  TestMbean testMbean = new TestMbean();
  ModelMBean mbean = defaultManagementMBeanAssembler.assemble(testMbean, new ObjectName("org.activiti.jmx.Mbeans:type=something"));
  assertNotNull(mbean);
  assertNotNull(mbean.getMBeanInfo());
  assertNotNull(mbean.getMBeanInfo().getAttributes());
  MBeanAttributeInfo[] attributes = mbean.getMBeanInfo().getAttributes();
  assertEquals(2, attributes.length);
  assertTrue((attributes[0].getName().equals("TestAttributeString") && attributes[1].getName().equals("TestAttributeBoolean") || (attributes[1].getName().equals("TestAttributeString") && attributes[0]
      .getName().equals("TestAttributeBoolean"))));
  assertNotNull(mbean.getMBeanInfo().getOperations());
  MBeanOperationInfo[] operations = mbean.getMBeanInfo().getOperations();
  assertNotNull(operations);
  assertEquals(3, operations.length);

}
 
Example #5
Source File: CacheStatisticsControllerEnricher.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Object> getStatistics() {
    try {
        MBeanInfo mBeanInfo = getConnection().getMBeanInfo(getMbeanName());
        String[] statAttrs = Arrays.asList(mBeanInfo.getAttributes()).stream()
          .filter(MBeanAttributeInfo::isReadable)
          .map(MBeanAttributeInfo::getName)
          .collect(Collectors.toList())
          .toArray(new String[] {});
        return getConnection().getAttributes(getMbeanName(), statAttrs)
          .asList()
          .stream()
          .collect(Collectors.toMap(Attribute::getName, Attribute::getValue));
    } catch (IOException | InstanceNotFoundException | ReflectionException | IntrospectionException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #6
Source File: JMXGet.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * print all attributes' values
 */
public void printAllValues() throws Exception {
  err("List of all the available keys:");

  Object val = null;

  for (ObjectName oname : hadoopObjectNames) {
    err(">>>>>>>>jmx name: " + oname.getCanonicalKeyPropertyListString());
    MBeanInfo mbinfo = mbsc.getMBeanInfo(oname);
    MBeanAttributeInfo[] mbinfos = mbinfo.getAttributes();

    for (MBeanAttributeInfo mb : mbinfos) {
      val = mbsc.getAttribute(oname, mb.getName());
      System.out.format(format, mb.getName(), (val==null)?"":val.toString());
    }
  }
}
 
Example #7
Source File: MustBeValidCommand.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Instantiate the MBean server
    //
    final MBeanAttributeInfo[]    atts   = makeAttInfos(attributes);
    final MBeanConstructorInfo[]  ctors  = makeCtorInfos(constructors);
    final MBeanOperationInfo[]    ops    = makeOpInfos(operations);
    final MBeanNotificationInfo[] notifs =
        makeNotifInfos(notificationclasses);

    for (int i=0; i<mbeanclasses.length;i++) {
        System.out.println("Create an MBeanInfo: " + mbeanclasses[i][0]);
        final MBeanInfo mbi =
            new MBeanInfo(mbeanclasses[i][1],mbeanclasses[i][0],
                          atts, ctors, ops, notifs);
    }

    // Test OK!
    //
    System.out.println("All MBeanInfo successfuly created!");
    System.out.println("Bye! Bye!");
}
 
Example #8
Source File: Registry.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/** Get the type of an attribute of the object, from the metadata.
 *
 * @param oname
 * @param attName
 * @return null if metadata about the attribute is not found
 * @since 1.1
 */
public String getType( ObjectName oname, String attName )
{
    String type=null;
    MBeanInfo info=null;
    try {
        info=server.getMBeanInfo(oname);
    } catch (Exception e) {
        log.info( "Can't find metadata for object" + oname );
        return null;
    }

    MBeanAttributeInfo attInfo[]=info.getAttributes();
    for( int i=0; i<attInfo.length; i++ ) {
        if( attName.equals(attInfo[i].getName())) {
            type=attInfo[i].getType();
            return type;
        }
    }
    return null;
}
 
Example #9
Source File: OldMBeanServerTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void printAttrs(
        MBeanServerConnection mbsc1, Class<? extends Exception> expectX)
throws Exception {
    Set<ObjectName> names = mbsc1.queryNames(null, null);
    for (ObjectName name : names) {
        System.out.println(name + ":");
        MBeanInfo mbi = mbsc1.getMBeanInfo(name);
        MBeanAttributeInfo[] mbais = mbi.getAttributes();
        for (MBeanAttributeInfo mbai : mbais) {
            String attr = mbai.getName();
            Object value;
            try {
                value = mbsc1.getAttribute(name, attr);
            } catch (Exception e) {
                if (expectX != null && expectX.isInstance(e))
                    value = "<" + e + ">";
                else
                    throw e;
            }
            String s = "  " + attr + " = " + value;
            if (s.length() > 80)
                s = s.substring(0, 77) + "...";
            System.out.println(s);
        }
    }
}
 
Example #10
Source File: JmxUtil.java    From ankush with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Gets the attribute name value map.
 * 
 * @param objName
 *            the obj name
 * @return the attribute name value map
 */
public Map<String, Object> getAttributes(ObjectName objName) {
	Map<String, Object> attrMap = null;
	try {
		MBeanInfo mbeanInfo = mbeanServerConnection.getMBeanInfo(objName);
		MBeanAttributeInfo[] mbeanAttributeInfo = mbeanInfo.getAttributes();
		attrMap = new HashMap<String, Object>();
		DecimalFormat df = new DecimalFormat("###.##");
		for (int i = 0; i < mbeanAttributeInfo.length; i++) {
			String attrName = mbeanAttributeInfo[i].getName();
			Object attrValue = getAttribute(objName,
					mbeanAttributeInfo[i].getName());
			if (mbeanAttributeInfo[i].getType().equals("double")) {
				attrValue = df.format((Double) getAttribute(objName,
						mbeanAttributeInfo[i].getName()));
			}
			attrMap.put(attrName, attrValue);
		}
	} catch (Exception e) {
		LOGGER.error(e.getMessage(), e);
	}
	return attrMap;
}
 
Example #11
Source File: MustBeValidCommand.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Instantiate the MBean server
    //
    final MBeanAttributeInfo[]    atts   = makeAttInfos(attributes);
    final MBeanConstructorInfo[]  ctors  = makeCtorInfos(constructors);
    final MBeanOperationInfo[]    ops    = makeOpInfos(operations);
    final MBeanNotificationInfo[] notifs =
        makeNotifInfos(notificationclasses);

    for (int i=0; i<mbeanclasses.length;i++) {
        System.out.println("Create an MBeanInfo: " + mbeanclasses[i][0]);
        final MBeanInfo mbi =
            new MBeanInfo(mbeanclasses[i][1],mbeanclasses[i][0],
                          atts, ctors, ops, notifs);
    }

    // Test OK!
    //
    System.out.println("All MBeanInfo successfuly created!");
    System.out.println("Bye! Bye!");
}
 
Example #12
Source File: MustBeValidCommand.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Instantiate the MBean server
    //
    final MBeanAttributeInfo[]    atts   = makeAttInfos(attributes);
    final MBeanConstructorInfo[]  ctors  = makeCtorInfos(constructors);
    final MBeanOperationInfo[]    ops    = makeOpInfos(operations);
    final MBeanNotificationInfo[] notifs =
        makeNotifInfos(notificationclasses);

    for (int i=0; i<mbeanclasses.length;i++) {
        System.out.println("Create an MBeanInfo: " + mbeanclasses[i][0]);
        final MBeanInfo mbi =
            new MBeanInfo(mbeanclasses[i][1],mbeanclasses[i][0],
                          atts, ctors, ops, notifs);
    }

    // Test OK!
    //
    System.out.println("All MBeanInfo successfuly created!");
    System.out.println("Bye! Bye!");
}
 
Example #13
Source File: MXBeanInteropTest2.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void printMBeanInfo(MBeanInfo mbInfo) {
    System.out.println("Description " + mbInfo.getDescription());

    for (MBeanConstructorInfo ctor : mbInfo.getConstructors()) {
        System.out.println("Constructor " + ctor.getName());
    }

    for (MBeanAttributeInfo att : mbInfo.getAttributes()) {
        System.out.println("Attribute " + att.getName()
        + " [" + att.getType() + "]");
    }

    for (MBeanOperationInfo oper : mbInfo.getOperations()) {
        System.out.println("Operation " + oper.getName());
    }

    for (MBeanNotificationInfo notif : mbInfo.getNotifications()) {
        System.out.println("Notification " + notif.getName());
    }
}
 
Example #14
Source File: JMXGet.java    From big-c with Apache License 2.0 6 votes vote down vote up
public void printAllMatchedAttributes(String attrRegExp) throws Exception {
  err("List of the keys matching " + attrRegExp + " :");
  Object val = null;
  Pattern p = Pattern.compile(attrRegExp);
  for (ObjectName oname : hadoopObjectNames) {
    err(">>>>>>>>jmx name: " + oname.getCanonicalKeyPropertyListString());
    MBeanInfo mbinfo = mbsc.getMBeanInfo(oname);
    MBeanAttributeInfo[] mbinfos = mbinfo.getAttributes();
    for (MBeanAttributeInfo mb : mbinfos) {
      if (p.matcher(mb.getName()).lookingAt()) {
        val = mbsc.getAttribute(oname, mb.getName());
        System.out.format(format, mb.getName(), (val == null) ? "" : val.toString());
      }
    }
  }
}
 
Example #15
Source File: OldMBeanServerTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static void printAttrs(
        MBeanServerConnection mbsc1, Class<? extends Exception> expectX)
throws Exception {
    Set<ObjectName> names = mbsc1.queryNames(null, null);
    for (ObjectName name : names) {
        System.out.println(name + ":");
        MBeanInfo mbi = mbsc1.getMBeanInfo(name);
        MBeanAttributeInfo[] mbais = mbi.getAttributes();
        for (MBeanAttributeInfo mbai : mbais) {
            String attr = mbai.getName();
            Object value;
            try {
                value = mbsc1.getAttribute(name, attr);
            } catch (Exception e) {
                if (expectX != null && expectX.isInstance(e))
                    value = "<" + e + ">";
                else
                    throw e;
            }
            String s = "  " + attr + " = " + value;
            if (s.length() > 80)
                s = s.substring(0, 77) + "...";
            System.out.println(s);
        }
    }
}
 
Example #16
Source File: MustBeValidCommand.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Instantiate the MBean server
    //
    final MBeanAttributeInfo[]    atts   = makeAttInfos(attributes);
    final MBeanConstructorInfo[]  ctors  = makeCtorInfos(constructors);
    final MBeanOperationInfo[]    ops    = makeOpInfos(operations);
    final MBeanNotificationInfo[] notifs =
        makeNotifInfos(notificationclasses);

    for (int i=0; i<mbeanclasses.length;i++) {
        System.out.println("Create an MBeanInfo: " + mbeanclasses[i][0]);
        final MBeanInfo mbi =
            new MBeanInfo(mbeanclasses[i][1],mbeanclasses[i][0],
                          atts, ctors, ops, notifs);
    }

    // Test OK!
    //
    System.out.println("All MBeanInfo successfuly created!");
    System.out.println("Bye! Bye!");
}
 
Example #17
Source File: PerfCounters.java    From java-svc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private MBeanInfo createMBeanInfo() {
	Collection<Counter> counters = counterMap.values();
	List<MBeanAttributeInfo> attributes = new ArrayList<>(counters.size());
	for (Counter c : counters) {
		if (!c.isVector()) {
			String typeName = "java.lang.String";
			synchronized (c) {
				Object value = c.getValue();
				if (value != null) {
					typeName = value.getClass().getName();
				}
			}
			attributes.add(new MBeanAttributeInfo(c.getName(), typeName,
					String.format("%s [%s,%s]", c.getName(), c.getUnits(), c.getVariability()), true, false,
					false));
		}
	}
	MBeanAttributeInfo[] attributesArray = attributes.toArray(new MBeanAttributeInfo[attributes.size()]);
	return new MBeanInfo(this.getClass().getName(),
			"An MBean exposing the available JVM Performance Counters as attributes.", attributesArray, null, null,
			null);
}
 
Example #18
Source File: DefaultManagementMBeanAssemblerTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testHappyPath() throws MalformedObjectNameException, JMException {
    TestMbean testMbean = new TestMbean();
    ModelMBean mbean = defaultManagementMBeanAssembler.assemble(testMbean, new ObjectName("org.flowable.jmx.Mbeans:type=something"));
    assertNotNull(mbean);
    assertNotNull(mbean.getMBeanInfo());
    assertNotNull(mbean.getMBeanInfo().getAttributes());
    MBeanAttributeInfo[] attributes = mbean.getMBeanInfo().getAttributes();
    assertEquals(2, attributes.length);
    assertTrue((attributes[0].getName().equals("TestAttributeString") && attributes[1].getName().equals("TestAttributeBoolean") || (attributes[1].getName().equals("TestAttributeString") && attributes[0]
            .getName().equals("TestAttributeBoolean"))));
    assertNotNull(mbean.getMBeanInfo().getOperations());
    MBeanOperationInfo[] operations = mbean.getMBeanInfo().getOperations();
    assertNotNull(operations);
    assertEquals(3, operations.length);

}
 
Example #19
Source File: JmxClient.java    From simplejmx with ISC License 5 votes vote down vote up
private MBeanAttributeInfo getAttrInfo(ObjectName objectName, String attrName) throws JMException {
	MBeanAttributeInfo[] attributes;
	try {
		attributes = mbeanConn.getMBeanInfo(objectName).getAttributes();
	} catch (Exception e) {
		throw createJmException("Cannot get attribute info from " + objectName, e);
	}
	for (MBeanAttributeInfo info : attributes) {
		if (info.getName().equals(attrName)) {
			return info;
		}
	}
	return null;
}
 
Example #20
Source File: ReflectionMBeanAttribute.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public ReflectionMBeanAttribute build() {
  checkState(name != null);
  checkState(target != null);
  checkState(getter != null || setter != null);

  Descriptor getterDescriptor = null;
  if (getter != null) {
    getterDescriptor = DescriptorHelper.build(getter);
  }
  Descriptor setterDescriptor = null;
  if (setter != null) {
    setterDescriptor = DescriptorHelper.build(setter);
  }

  MBeanAttributeInfo info = new MBeanAttributeInfo(
      name,
      attributeType(getter, setter).getName(),
      description,
      getter != null, // readable
      setter != null, // writable
      isIs(getter),
      ImmutableDescriptor.union(getterDescriptor, setterDescriptor)
  );

  log.trace("Building attribute with info: {}", info);
  return new ReflectionMBeanAttribute(info, target, getter, setter);
}
 
Example #21
Source File: AbstractJmxAssemblerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testGetMBeanAttributeInfo() throws Exception {
	ModelMBeanInfo info = getMBeanInfoFromAssembler();
	MBeanAttributeInfo[] inf = info.getAttributes();
	assertEquals("Invalid number of Attributes returned",
			getExpectedAttributeCount(), inf.length);

	for (int x = 0; x < inf.length; x++) {
		assertNotNull("MBeanAttributeInfo should not be null", inf[x]);
		assertNotNull(
				"Description for MBeanAttributeInfo should not be null",
				inf[x].getDescription());
	}
}
 
Example #22
Source File: ModelMBeanInfoSupport.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Deserializes a {@link ModelMBeanInfoSupport} from an {@link ObjectInputStream}.
 */
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
    if (compat) {
        // Read an object serialized in the old serial form
        //
        ObjectInputStream.GetField fields = in.readFields();
        modelMBeanDescriptor =
                (Descriptor) fields.get("modelMBeanDescriptor", null);
        if (fields.defaulted("modelMBeanDescriptor")) {
            throw new NullPointerException("modelMBeanDescriptor");
        }
        modelMBeanAttributes =
                (MBeanAttributeInfo[]) fields.get("mmbAttributes", null);
        if (fields.defaulted("mmbAttributes")) {
            throw new NullPointerException("mmbAttributes");
        }
        modelMBeanConstructors =
                (MBeanConstructorInfo[]) fields.get("mmbConstructors", null);
        if (fields.defaulted("mmbConstructors")) {
            throw new NullPointerException("mmbConstructors");
        }
        modelMBeanNotifications =
                (MBeanNotificationInfo[]) fields.get("mmbNotifications", null);
        if (fields.defaulted("mmbNotifications")) {
            throw new NullPointerException("mmbNotifications");
        }
        modelMBeanOperations =
                (MBeanOperationInfo[]) fields.get("mmbOperations", null);
        if (fields.defaulted("mmbOperations")) {
            throw new NullPointerException("mmbOperations");
        }
    } else {
        // Read an object serialized in the new serial form
        //
        in.defaultReadObject();
    }
}
 
Example #23
Source File: JmxDisruptorTest.java    From disruptor-spring-manager with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	mockDisruptorConfig = createMock(DefaultDisruptorConfig.class);
	mockMBeanInfo = createMock(MBeanInfo.class);
	mockMBeanAttribute = createMock(MBeanAttributeInfo.class);
	mockMBeanOperation = createMock(MBeanOperationInfo.class);
	mockMBeanParameterInfo = createMock(MBeanParameterInfo.class);
	
	jmxDisruptor = new JmxDisruptor(mockDisruptorConfig, "disruptorBean");
	assertNotNull(mockDisruptorConfig);
}
 
Example #24
Source File: ArtemisMBeanServerGuard.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void handleSetAttribute(MBeanServer proxy, ObjectName objectName, Attribute attribute) throws JMException, IOException {
   String dataType = null;
   MBeanInfo info = proxy.getMBeanInfo(objectName);
   for (MBeanAttributeInfo attr : info.getAttributes()) {
      if (attr.getName().equals(attribute.getName())) {
         dataType = attr.getType();
         break;
      }
   }

   if (dataType == null)
      throw new IllegalStateException("Attribute data type can not be found");

   handleInvoke(objectName, "set" + attribute.getName(), new Object[]{attribute.getValue()}, new String[]{dataType});
}
 
Example #25
Source File: MustBeValidCommand.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static private MBeanAttributeInfo[] makeAttInfos(String[][] spec) {
    final MBeanAttributeInfo[] result =
        new MBeanAttributeInfo[spec.length];
    for (int i=0;i<result.length;i++) {
        System.out.println("\tCreate an MBeanAttributeInfo: " +
                           spec[i][0]);
        final MBeanAttributeInfo item =
            new MBeanAttributeInfo(spec[i][2],spec[i][1],spec[i][0],
                                   true,true,false);
        result[i]=item;
    }
    return result;
}
 
Example #26
Source File: MBeanIntrospector.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void visitAttribute(String attributeName,
        M getter,
        M setter) {
    MBeanAttributeInfo mbai =
            getMBeanAttributeInfo(attributeName, getter, setter);

    attrs.add(mbai);
}
 
Example #27
Source File: MBean.java    From selenium with Apache License 2.0 5 votes vote down vote up
MBeanAttributeInfo getMBeanAttributeInfo() {
  try {
    return new MBeanAttributeInfo(name, description, getter, setter);
  } catch (IntrospectionException e) {
    e.printStackTrace();
    return null;
  }
}
 
Example #28
Source File: MethodExclusionMBeanInfoAssemblerNotMappedTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNickNameIsExposed() throws Exception {
	ModelMBeanInfo inf = (ModelMBeanInfo) getMBeanInfo();
	MBeanAttributeInfo attr = inf.getAttribute("NickName");
	assertNotNull("Nick Name should not be null", attr);
	assertTrue("Nick Name should be writable", attr.isWritable());
	assertTrue("Nick Name should be readable", attr.isReadable());
}
 
Example #29
Source File: MetricsProxy.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
private List<JmxStat> scrapeBean(ObjectName mbeanName) {
  List<JmxStat> attributeList = Lists.newArrayList();
  try {
    Map<String, MBeanAttributeInfo> name2AttrInfo = new LinkedHashMap<>();
    MBeanInfo info = proxy.getMBeanServerConnection().getMBeanInfo(mbeanName);
    for (MBeanAttributeInfo attrInfo : info.getAttributes()) {
      MBeanAttributeInfo attr = attrInfo;
      if (!attr.isReadable()) {
        LOG.warn("{}.{} not readable", mbeanName, attr);
      } else {
        name2AttrInfo.put(attr.getName(), attr);
      }
    }
    proxy.getMBeanServerConnection().getAttributes(mbeanName, name2AttrInfo.keySet().toArray(new String[0]))
        .asList()
        .forEach((attribute) -> {
          Object value = attribute.getValue();
          JmxStat.Builder jmxStatBuilder = JmxStat.builder()
              .withAttribute(attribute.getName())
              .withName(mbeanName.getKeyProperty("name"))
              .withScope(mbeanName.getKeyProperty("scope"))
              .withDomain(mbeanName.getDomain())
              .withType(mbeanName.getKeyProperty("type"))
              .withMbeanName(mbeanName.toString());
          if (null == value) {
            attributeList.add(jmxStatBuilder.withValue(0.0).build());
          } else if (value instanceof Number) {
            attributeList.add(jmxStatBuilder.withValue(((Number) value).doubleValue()).build());
          }
        });
  } catch (JMException | IOException e) {
    LOG.error("Fail getting mbeanInfo or grabbing attributes for mbean {} ", mbeanName, e);
  }
  return attributeList;
}
 
Example #30
Source File: MBeanInfoAssemblerTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadAtributeInfoHappyPath() throws JMException {
  ModelMBeanInfo beanInfo = mbeanInfoAssembler.getMBeanInfo(testMbean, null, "someName");
  assertNotNull(beanInfo);

  assertEquals("test description", beanInfo.getDescription());
  MBeanAttributeInfo[] testAttributes = beanInfo.getAttributes();
  assertNotNull(testAttributes);
  assertEquals(2, testAttributes.length);

  int counter = 0;
  for (MBeanAttributeInfo info : testAttributes) {
    if (info.getName().equals("TestAttributeBoolean")) {
      counter++;
      assertEquals("test attribute Boolean description", info.getDescription());
      assertEquals("java.lang.Boolean", info.getType());
      assertTrue(info.isReadable());
      assertFalse(info.isWritable());
    } else if (info.getName().equals("TestAttributeString")) {
      counter++;
      assertEquals("test attribute String description", info.getDescription());
      assertEquals("java.lang.String", info.getType());
      assertTrue(info.isReadable());
      assertFalse(info.isWritable());
    }
  }
  assertEquals(2, counter);

  // check the single operation

  assertNotNull(beanInfo.getOperations());
  assertEquals(3, beanInfo.getOperations().length);
}