Search code examples
jmxmbeans

Cannot create nested javax.management.openmbean.CompositeData object


I know I can have SimpleType values like SimpleType.INTEGER, SimpleType.STRING in a CompositeData. But I wonder how do I get another instance of CompositeData into CompositeData. E. g.:

CompositeType type = new CompositeType("My Type", "My Type", new String[]{"item1", "item2"}, new String[]{"item1", "item2"}, new OpenType[]{SimpleType.STRING, SimpleType.STRING});
CompositeData data = new CompositeDataSupport(type, new String[]{"item1", "item2"}, new String[]{"item value 1", "item value 2"});

CompositeType compType = new CompositeType("compData", "compData", new String[]{"compItem1"}, new String[]{"compItem1"}, new OpenType[]{I_DONT_KNOW_WHAT_TO_PUT_HERE});
CompositeData compData = new CompositeDataSupport(compType, new String[]{"compData"}, data);

See the "I_DONT_KNOW_WHAT_TO_PUT_HERE" above, I could not find out how to pass an OpenType of CompositeData. And I have seen an examples of recursively getting the instances of nested CompositeData from CompositeData.

Some references:

http://docs.oracle.com/javase/6/docs/api/javax/management/openmbean/CompositeData.html http://docs.oracle.com/javase/6/docs/api/javax/management/openmbean/CompositeType.html http://docs.oracle.com/javase/6/docs/api/javax/management/openmbean/OpenType.html


Solution

  • The value of I_DONT_KNOW_WHAT_TO_PUT_HERE is type, but the 2nd argument in the constructor on code line 4 should have the string "compItem1", not "compData" since this represents the item name you defined in the 3rd parameter on code line 4.

    Here's the full code:

        CompositeType type = new CompositeType("My Type", "My Type", new String[]{"item1", "item2"}, new String[]{"item1", "item2"}, new OpenType[]{SimpleType.STRING, SimpleType.STRING});
        CompositeData data = new CompositeDataSupport(type, new String[]{"item1", "item2"}, new String[]{"item value 1", "item value 2"});
    
        CompositeType compType = new CompositeType("compData", "compData", new String[]{"compItem1"}, new String[]{"compItem1"}, new OpenType[]{type});
        CompositeData compData = new CompositeDataSupport(compType, new String[]{"compItem1"}, new Object[]{data});
    

    Have you considered using MXBeans ? Unless you really need all that additional meta-data, it's a much simpler (and maintainable) way to go for exposing complex attributes in JMX.