Mapping to OpenTypes: MXBean Complex Attribute Types
By daniel on May 06, 2008
In the recent weeks, I've seen several questions on the
SDN JMX forum
regarding how to export a complex data type - such as the
content of a
in order to obtain a mapping to a desired OpenType.
Although it might not be a very good idea to define your management
model based on how it will be displayed by a given GUI, such as that
provided by JConsole or
VisualVM, I believe it is nonetheless
interesting to explore the various way in which such a
Map<String,Integer> could be modeled and
exposed through an MXBean attribute.
In this post - I will expose five different ways in which such a complex type can be exposed, three using a regular mapping provided by the MXBean framework, two using a more complex custom mapping involving the use of the CompositeDataView interface. Note that the code example shown in this post is based on the APIs available today in JDK 6: for those interested, some improvements are also planned for JDK 7. Compared to what is demonstrated here, these improvements should greatly increase the flexibility of MXBean mapping customization.
To illustrate my demonstration, I have created a ProductCodes
application, exposing a "ProductsDB" MXBean which allows you to
register new products in its data base. This is just an example
which allows me to expose a
such as discussed above. I have therefore kept my example to the
bare minimum: a registered product is composed simply of a
product name - a String, used as primary key, and an integer, a
product code dynamically allocated by the "ProductsDB" MXBean
when the new product is registered.
The ProductsDBMXBean interface shown below lets me exports the same
Map<String,Integer> in five different ways, using
five different attribute types and mappings. Some are obviously uglier
than others, depending on whether you look at them through the
programmatic lens, or through the JConsole/VisualVM lens.
Remember: the purpose of this example is to let you experiment
with these various ways of exposing data through MXBean.
Personally - I would recommend using the first method exposed
(array of RegisteredProduct interface) which provides the best
compromise between programmatic access and GUI navigation...
Well, enough said for now. It's time for your to experiment: grab the class below, compile it, and run it in a shell window with the command:
You can now start JConsole or VisualVM and point it at your ProductCodes
application. Open the MBeanTab and select the MBean named
If you select its "Attributes" node you will see our five attributes, all exposing the same information in five different ways. The specifics of each mapping is explained in the comments associated with the code.