Java – how to represent polymorphism in JMX?

I have this type:

public interface Numbering {
    List<NumberingComponent> getComponents();
}

public interface NumberingComponent {
    Object getValue();
}

public interface StringNumberingComponent extends NumberingComponent {
    String getValue();
}

public interface IntegerNumberingComponent extends NumberingComponent {
    Integer getValue();
}

All this is fine unless you try to register an mxbean that happens to use this type, and you get:

...top of exception chain omitted ...
Caused by: javax.management.openmbean.OpenDataException: Cannot convert type: class com.acme.NumberingComponent
    at com.sun.jmx.mbeanserver.OpenConverter.openDataException(OpenConverter.java:1411)
    at com.sun.jmx.mbeanserver.OpenConverter.toConverter(OpenConverter.java:264)
    at com.sun.jmx.mbeanserver.OpenConverter.makeArrayOrCollectionConverter(OpenConverter.java:315)
    at com.sun.jmx.mbeanserver.OpenConverter.makeParameterizedConverter(OpenConverter.java:393)
    at com.sun.jmx.mbeanserver.OpenConverter.makeConverter(OpenConverter.java:296)
    at com.sun.jmx.mbeanserver.OpenConverter.toConverter(OpenConverter.java:262)
    ... 57 more
Caused by: javax.management.openmbean.OpenDataException: Cannot convert type: interface java.io.Serializable
    at com.sun.jmx.mbeanserver.OpenConverter.openDataException(OpenConverter.java:1411)
    at com.sun.jmx.mbeanserver.OpenConverter.toConverter(OpenConverter.java:264)
    at com.sun.jmx.mbeanserver.OpenConverter.makeCompositeConverter(OpenConverter.java:467)
    at com.sun.jmx.mbeanserver.OpenConverter.makeConverter(OpenConverter.java:293)
    at com.sun.jmx.mbeanserver.OpenConverter.toConverter(OpenConverter.java:262)
    ... 61 more
Caused by: javax.management.openmbean.OpenDataException: Can't map java.io.Serializable to an open data type
    at com.sun.jmx.mbeanserver.OpenConverter.makeCompositeConverter(OpenConverter.java:454)
    at com.sun.jmx.mbeanserver.OpenConverter.makeConverter(OpenConverter.java:293)
    at com.sun.jmx.mbeanserver.OpenConverter.toConverter(OpenConverter.java:262)
    ... 64 more

Strings and integers are representable in JMX, but object is not, because at least one getter in a class is required to recognize it as an available type I know that any attempt to add an abstraction layer will not help, because the numbering component itself is already such a layer The original version of the interface also had generics, but I deleted them to make them simpler, and they all failed in the same way whether they were present or not

Is there any other way to map it to a composite type? I used Google to search for abnormal messages and basically didn't click (!!)

Solution

You can also extend the stringnumberingcomponent and integernumbering interfaces to compositedataview Assuming that the mbeaninfo of the MBean indicates the correct compositetype of the attribute, the mbeanserver will "render" the attribute as an instance of compositedata that will be properly serialized

The specific implementation method is quite simple, because your compositetype has only one field:

public CompositeData toCompositeData(CompositeType ct)

There is a good example in Javadoc

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>