- 所有已知的实现类:
-
GarbageCollectionNotificationInfo,GcInfo
public interface CompositeDataView
Java类可以实现此接口,以指示如何通过MXBean框架将其转换为CompositeData。
使用此类的典型方式是在CompositeData中添加额外的项,除了MXBean框架提供的那些声明在CompositeType中的项。为此,您必须创建另一个CompositeType,其中包含所有相同的项,以及您的额外项。
例如,假设您有一个名为Measure的类,由一个名为units的字符串和一个value组成,该值可以是long或double。它可能如下所示:
public class Measure implements CompositeDataView {
private String units;
private Number value; // a Long or a Double
public Measure(String units, Number value) {
this.units = units;
this.value = value;
}
public static Measure from(CompositeData cd) {
return new Measure((String) cd.get("units"),
(Number) cd.get("value"));
}
public String getUnits() {
return units;
}
// 不能称为getValue(),因为Number不是MXBean中的有效类型,因此暗示的"value"属性将被拒绝。
public Number _getValue() {
return value;
}
public CompositeData toCompositeData(CompositeType ct) {
try {
List<String> itemNames = new ArrayList<String>(ct.keySet());
List<String> itemDescriptions = new ArrayList<String>();
List<OpenType<?>> itemTypes = new ArrayList<OpenType<?>>();
for (String item : itemNames) {
itemDescriptions.add(ct.getDescription(item));
itemTypes.add(ct.getType(item));
}
itemNames.add("value");
itemDescriptions.add("long or double value of the measure");
itemTypes.add((value instanceof Long) ? SimpleType.LONG :
SimpleType.DOUBLE);
CompositeType xct =
new CompositeType(ct.getTypeName(),
ct.getDescription(),
itemNames.toArray(new String[0]),
itemDescriptions.toArray(new String[0]),
itemTypes.toArray(new OpenType<?>[0]));
CompositeData cd =
new CompositeDataSupport(xct,
new String[] {"units", "value"},
new Object[] {units, value});
assert ct.isValue(cd); // 检查我们是否做对了
return cd;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
将出现在此类型的属性或操作的openType字段中的CompositeType仅显示units项,但生成的实际CompositeData将同时具有units和value。
- 自:
- 1.6
- 参见:
-
Method Summary
Modifier and TypeMethodDescription返回与此对象中的值对应的CompositeData。
-
Method Details
-
toCompositeData
返回与此对象中的值对应的
CompositeData。返回的值通常应是CompositeDataSupport的实例,或者通过writeReplace方法序列化为CompositeDataSupport的类。否则,接收该对象的远程客户端可能无法重建它。- 参数:
-
ct- 期望的返回值的CompositeType。如果返回值是cd,则cd.getCompositeType().equals(ct)应为true。通常这是因为cd是使用ct作为其CompositeType构造的CompositeDataSupport。 - 返回:
-
CompositeData。
-