Search code examples
jbossjmxmbeans

How to dynamically add and register new attributes to mbean


Is it possible to dynamically add and register new attributes to mbean

eg :

<server>

<mbean code="org.jboss.example.MyMbean" name="jboss:service=myMbean,name=MyMbeanExample">

<attribute name="attribute1">value1</attribute>

<attribute name="attribute2">value2</attribute>

<attribute name="attribute3">value3</attribute>

<attribute name="attribute4">value5</attribute>

<attribute name="attribute5">value5</attribute>...

</mbean>
</server>

A new attribute added in jboss-service.xml should be registered in MyMbean dynamically with making any code change in Mbean, can this be done?

Thanks in Advance.


Solution

  • It's hard to answer your question without seeing the code of your DynamicMBean, but I suspect the answer to your question, with the proviso that there be no code changes in the MBean, is no. However, here's an overall approach (taking some liberal assumptions about your code):

    • A DynamicMBean like this will usually have some sort of map, keyed by the attribute name, and and containing either the value of the attribute (easy), or a value object containing some or all of the following intended to acquire/set the value of the attribute:
      • a target invocation object,
      • a method
      • an array of arguments to the method
    • When the MBeanInfo of the MBean is requested, the supplied MBeanAttributeInfos should reference the keys in this map (as well as the data type, mutability etc.) You can either generate these on the fly every time MBeanInfo is requested, or keep an updated collection of MBeanAttributeInfo which is updated whenever you add a new attribute.
    • The methods setAttribute and setAttributes should create a new attribute (by inserting the new key and value into the attribute map) if the set references an attribute that does not already exist.

    Since you're using JBoss, if you feel you might want to implement something like this, consider extending JBoss's ServiceDynamicMBeanSupport. It does some, but not all, of the legwork for you.