4.7 XML Service: Priming ModelMBeanInfo from XML Files

Creating ModelMBeanInfo instances in your code using the available ModelMBeanInfo APIs is very tedious . Using the API also means that your management instrumentation code has to be updated every time you have a slight change in your attributes or operations that you want to make available. It makes some sense to provide ModelMBeanInfo customization data in a separate file that accompanies the managed application. This file could be formatted as a properties file; however, choosing an XML format allows more tooling support and portability. This file would be used to customize ModelMBeanInfo at runtime. Support for creating MBeans from XML files and writing MBeans to XML files is not part of the JMX specification or reference implementation. Using an XML file does illustrate a more convenient and robust means to customize your model MBeans. It is also a useful illustration of how to create and use MBean services. We will illustrate one technique for providing XML services support here.

In this case we have created a service MBean: XMLService . A service MBean is an MBean that is used by other MBeans as a utility and doesn't actually represent a managed resource. The XMLService MBean can create an XML file from a model MBean, or it can read an XML file and return a model MBean, or ModelMBeanInfo instance. It is possible to choose a number of different XML DTDs (document type definitions) to guide the file's format.

One choice is to use the DTD used by CIM. [19] Reasons to use the CIM DTD include the following: It is freely available; it is a generally accepted standard for management data representation; and it defines a mechanism to express management objects, attributes, and operations, as well as metadata (qualifiers) about them. One drawback to using the CIM DTD is that you will have to modify it to add support for expressing notifications. These features make the CIM DTD an easy fit for JMX MBean representation.

An alternative representation for the XML file is the DTD used by the Apache Jakarta Project's Commons Modeler version 1.0. [20] The modeler uses a special DTD (at http://jakarta.apache.org/commons/modeler/mbeans-descriptors.dtd) that is a relatively straightforward use of XML elements for each of the MBeanInfo information types. At the time of this writing, the implementation of the Modeler did not fully support descriptors in ModelMBeanInfo objects. We were able to add that support and an updated version of the modeler with the support is available as part of the downloadable examples for this book on the book's Web site: http://www.awprofessional.com/titles/0672324083. Full support of descriptors is required to run these examples. The full source for these examples and the XMLService MBean are available on the Addison-Wesley Web site for this book. The Modeler reads XML files and creates ModelMBean instances. It does not provide support for creating the XML files. In order to create the XML Service envisioned here, we created our own interface that supports creating ModelMBean instances from an XML file, creating ModelMBeanInfo instances from an XML file, variations with and without the managed object, and writing an XML file.

Here is the API for the XMLService MBean:

  package  jnjmx.ch4;  import  javax.management.*;  import  javax.management.modelmbean.*;  public   interface  XMLServiceMBean {   Object createMBeanFromXMLFile(String fileName,                                 ObjectName mbName);   MBeanInfo createMBeanInfoFromXMLFile(String fileName,     String name);   Object createMBeanFromXMLFile(String fileName, ObjectName mbName,                                 Object managedResource);   MBeanInfo createMBeanInfoFromXMLFile(String fileName, String name,                                 Object managedResource);   Object createMBeanFromXMLFile(String fileName, String mbName,                                 Object managedResource);  void  writeXMLFile( ModelMBean mb, String fileName); } 

First let's look at how to use XMLService to create a model MBean for the Apache model MBean:

 /** Create a name for the MBean */  ObjectName apacheMBeanName =  new  ObjectName   ("apacheManager: id=ApacheServer"); /** Instantiate the ApacheServer utility to be the * managed resource */ serverURL = "http://www.apache.org/server-status?auto"; Apache myApacheServer =  new  Apache(serverURL); /** Instantiate a model MBean from the XML file */ ModelMBean apacheMMBean = (ModelMBean)   XMLPrimer.createMBeanFromXMLFile(     "jnjmx.ch4/apachembeans.xml",  // name of XML file     apacheMBeanName,               // name of MBean in XMl file     myApacheServer);               // managed resource / ** Register the model MBean that is now ready * to run with the MBeanServer */   ObjectInstance registeredApacheMMBean =         myMBS.registerMBean(apacheMMBean,         apacheMBeanName); //** Write the  XML file  writeXMLFile( apacheMMBean, "jnjmx.ch4/apachembeans.xml"); 

You can see that first we create the XMLService MBean. Then we use XMLService 's createMBeanFromXMLFile() method to read the file and instantiate a model MBean. We also use XMLService to write the MBean's ModelMBeanInfo to a file called apachembeans.xml . This would be important to do if some of the configuration of the model MBean had been changed ”that is, caching or logging policy ”that needed to be persisted for the next instantiation of this model MBean.

Now we are going to look at how to use the XML service as a service MBean. Here is how to instantiate an XMLService MBean:

 /** Find an MBeanServer for our MBean */  MBeanServer myMBS = MBeanServerFactory.createMBeanServer(); // Create a JMX object name for the XML service ObjectName sn =  new  ObjectName("todd:id=XMLPrimer"); // Instantiate the XML service XMLPrimer =  new  XMLService(); ObjectInstance registeredXMLService =   mbs.registerMBean(XMLPrimer, sn); 

We created an instance of XMLService , created an object name for it, and registered it with the MBeanServer.

Here is how another adapter or JMX manager can use the same XMLService MBean to create a model MBean. In this case we have used a variation of the API that does not require the managed resource. The managed resource is associated with it later:

 /** Find an MBeanServer for our MBean */  MBeanServer myMBS = MBeanServerFactory.createMBeanServer(); /** Instantiate the ApacheServer utility to be the managed * resource */ serverURL = "http://www.apache.org/server-status?auto"; Apache myApacheServer =  new  Apache(serverURL); /** Create the XML service's name */ XMLServiceName=("todd:id=XMLPrimer"); /** Instantiate an ModelMBean using the already registered * XMLService */ ModelMBean apacheMMBean = (ModelMBean) mbs.invoke(   xmlServiceName, "createMBeanFromXMLFile",  new  Object[] {"jnjmx.ch4/apachembeans.xml", // name of XML file                  "id=ApacheServer"); // name of MBean in XMl file  new  String[] {"java.lang.String",                  "java.lang.String"}); /** Assign the managed resource to the MBean, since we didn't pass * it in this time */ apacheMMBean.setManagedResource(myApacheServer,"ObjectReference"); /** Register the model MBean that is now ready to run with the * MBeanServer */ ObjectInstance registeredApacheMMBean =   myMBS.registerMBean(apacheMMBean,                       apacheMBeanName); 

The XML file containing the ModelMBeanInfo data using the Jakarta Modeler DTD is listed in full at the end of this chapter.



Java and JMX. Building Manageable Systems
Javaв„ў and JMX: Building Manageable Systems
ISBN: 0672324083
EAN: 2147483647
Year: 2000
Pages: 115

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net