下面列出了怎么用javax.management.MBeanFeatureInfo的API类实例代码及写法,或者点击链接到github查看源代码。
public void setAttributesFilter(String exclude, String include) {
if (include == null) {
include = "";
}
if (exclude == null) {
exclude = "";
}
includes = Pattern.compile(include);
excludes = Pattern.compile(exclude);
try {
// Set the current value as the description
final Field field = MBeanFeatureInfo.class.getDeclaredField("description");
field.setAccessible(true);
field.set(includeInfo, "\"" + includes.pattern() + "\"");
field.set(excludeInfo, "\"" + excludes.pattern() + "\"");
} catch (final Exception e) {
// Oh well, we tried
}
}
protected static MBeanFeatureInfo getFeatureInfo(MBeanFeatureInfo[] infos, String cmd) {
// Cmd may be carrying arguments. Don't count them in the compare.
int index = cmd.indexOf('=');
String name = (index > 0) ? cmd.substring(0, index) : cmd;
for (int i = 0; i < infos.length; i++) {
if (infos[i].getName().equals(name)) {
return infos[i];
}
}
return null;
}
protected static MBeanFeatureInfo getFeatureInfo(MBeanFeatureInfo[] infos, String cmd) {
// Cmd may be carrying arguments. Don't count them in the compare.
int index = cmd.indexOf('=');
String name = (index > 0) ? cmd.substring(0, index) : cmd;
for (int i = 0; i < infos.length; i++) {
if (infos[i].getName().equals(name)) {
return infos[i];
}
}
return null;
}
private boolean areFlightMethodsAvailable() {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
try {
ObjectName objectName = ObjectName.getInstance(DIAGNOSTIC_BEAN);
MBeanInfo beanInfo = beanServer.getMBeanInfo(objectName);
return Arrays.stream(beanInfo.getOperations())
.map(MBeanFeatureInfo::getName)
.anyMatch(op -> op.contains("jfr"));
} catch (JMException instanceNotFoundEx) {
return false;
}
}
/** */
@Test
public void testSysJmxMetrics() throws Exception {
DynamicMBean sysMBean = metricRegistry(ignite.name(), null, SYS_METRICS);
Set<String> res = stream(sysMBean.getMBeanInfo().getAttributes())
.map(MBeanFeatureInfo::getName)
.collect(toSet());
assertTrue(res.contains(CPU_LOAD));
assertTrue(res.contains(GC_CPU_LOAD));
assertTrue(res.contains(metricName("memory", "heap", "init")));
assertTrue(res.contains(metricName("memory", "heap", "used")));
assertTrue(res.contains(metricName("memory", "nonheap", "committed")));
assertTrue(res.contains(metricName("memory", "nonheap", "max")));
Optional<MBeanAttributeInfo> cpuLoad = stream(sysMBean.getMBeanInfo().getAttributes())
.filter(a -> a.getName().equals(CPU_LOAD))
.findFirst();
assertTrue(cpuLoad.isPresent());
assertEquals(CPU_LOAD_DESCRIPTION, cpuLoad.get().getDescription());
Optional<MBeanAttributeInfo> gcCpuLoad = stream(sysMBean.getMBeanInfo().getAttributes())
.filter(a -> a.getName().equals(GC_CPU_LOAD))
.findFirst();
assertTrue(gcCpuLoad.isPresent());
assertEquals(GC_CPU_LOAD_DESCRIPTION, gcCpuLoad.get().getDescription());
}
/** */
@Test
public void testDataRegionJmxMetrics() throws Exception {
DynamicMBean dataRegionMBean = metricRegistry(ignite.name(), "io", "dataregion.default");
Set<String> res = stream(dataRegionMBean.getMBeanInfo().getAttributes())
.map(MBeanFeatureInfo::getName)
.collect(toSet());
assertTrue(res.containsAll(EXPECTED_ATTRIBUTES));
for (String metricName : res)
assertNotNull(metricName, dataRegionMBean.getAttribute(metricName));
}
private boolean areFlightMethodsAvailable() {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
try {
ObjectName objectName = ObjectName.getInstance(DIAGNOSTIC_BEAN);
MBeanInfo beanInfo = beanServer.getMBeanInfo(objectName);
return Arrays.stream(beanInfo.getOperations())
.map(MBeanFeatureInfo::getName)
.anyMatch(op -> op.contains("jfr"));
} catch (JMException instanceNotFoundEx) {
return false;
}
}
protected static boolean isFeatureInfo(MBeanFeatureInfo[] infos, String cmd) {
return getFeatureInfo(infos, cmd) != null;
}
protected static boolean isFeatureInfo(MBeanFeatureInfo[] infos, String cmd) {
return getFeatureInfo(infos, cmd) != null;
}
@Override
public int compare(final MBeanFeatureInfo o1, final MBeanFeatureInfo o2) {
return o1.getName().compareTo(o2.getName());
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Retrieves and deserializes the object stored at the given key.
* @return The deserialized object.
* @throws IOException if the object cannot be deserialized.
* @throws ClassNotFoundException if the class of the serialized object
* cannot be loaded.
**/
public static MBeanFeatureInfo get(String name)
throws IOException, ClassNotFoundException {
final byte[] bytes = map.get(name);
final Object obj = deserialize(bytes);
return (MBeanFeatureInfo)obj;
}
/**
* Feature information.
*/
MBeanFeatureInfo getInfo();
/**
* Returns the localized description for the given constructor, attribute or operation.
*
* @return a localized description for the given attribute or operation.
*/
@Override
protected String getDescription(final MBeanFeatureInfo info) {
return getDescription(info.getName());
}