- 所有已知的实现类:
GarbageCollectionNotificationInfo
,GcInfo
public interface CompositeDataView
Java 类可以实现此接口以指示它如何被 MXBean 框架转换为 CompositeData
。
使用此类的典型方法是,除了在 MXBean 框架提供的 CompositeType
中声明的项目之外,还向 CompositeData
添加额外的项目。为此,您必须创建另一个 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; } // Can't be called getValue(), because Number is not a valid type // in an MXBean, so the implied "value" property would be rejected. 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); // check we've done it right return cd; } catch (Exception e) { throw new RuntimeException(e); } } }
对于这种类型的属性或操作,将出现在 Descriptor
的 openType
字段中的 CompositeType
将仅显示 units
项,但生成的实际 CompositeData
将同时包含 units
和 value
。
- 自从:
- 1.6
- 参见:
-
方法总结
-
方法详情
-
toCompositeData
返回与该对象中的值对应的
CompositeData
。返回值通常应该是CompositeDataSupport
的一个实例,或者是一个通过writeReplace
方法序列化为CompositeDataSupport
的类。否则,接收对象的远程客户端可能无法重建它。- 参数:
ct
- 返回值的预期CompositeType
。如果返回值为cd
,则cd.getCompositeType().equals(ct)
应该为真。这通常是因为cd
是一个CompositeDataSupport
以ct
作为其CompositeType
构造的。- 返回:
CompositeData
。
-