5.1 The MBeanServerFactory Class

One of the first things a developer looking at the Javadoc for the MBeanServer will notice is that it is an interface, not a class. This observation immediately raises the question, How do I instantiate an MBeanServer? And the answer is, you use one of the methods provided by the MBeanServerFactory class. MBeanServerFactory provides four methods for creating MBeanServers:

 static MBeanServer createMBeanServer()  static MBeanServer createMBeanServer(String domain) static MBeanServer newMBeanServer() static MBeanServer newMBeanServer(String domain) 

The create versions keep a reference to the MBeanServer that the factory creates so that they can be accessed later via the findMBeanServer() method. No internal reference is kept to MBeanServers returned by the newMBeanServer() methods.

Every MBeanServer has a default domain for its object names . We'll discuss object naming in detail in a moment; for now just think of the domain as a kind of namespace. A programmer can specify the default domain string by calling the version of createMBeanServer() or newMBeanServer() that takes a String parameter; otherwise the default value specified by the JMX implementation will be used.

To get a reference to a specific MBeanServer, or to all MBeanServers, in a JVM, MBeanServerFactory provides the findMBeanServer() method. Its signature is

 static ArrayList findMBeanServer(String id) 

The id parameter may be either the MBeanServer ID of the desired MBeanServer, or null . Passing null to findMBeanServer() causes it to populate the returned ArrayList object with references to all of the MBeanServers in the JVM that were created via one of the MBeanServerFactory createMBeanServer() methods.

The MBeanServerDelegate MBean provides a management interface for the MBeanServer itself and serves as the source of notifications that emanate from the MBeanServer. MBeanServerDelegate will be discussed in detail in Section 5.4. To get an MBeanServer's ID, you call getMBeanServerId() on its associated MBeanServerDelegate MBean.

The final MBeanServerFactory method is releaseMBeanServer() :

 static void releaseMBeanServer(MBeanServer server) 

This method drops MBeanServerFactory 's internal reference to the given MBeanServer so that it can be garbage-collected by the JVM once all other references to the MBeanServer are gone. If the given MBeanServer doesn't exist ”that is, MBeanServerFactory has no internal reference to it, as would be the case for an MBeanServer created via one of MBeanServerFactory 's newMBeanServer() methods, releaseMBeanServer() throws an IllegalArgumentException .

Now that we know how to create MBeanServers, the next questions are, How many should we create, and which MBeanServerFactory method should we use to create them? In general, a managed application ”for example, an HTTP daemon ”will need only a single MBeanServer. Creating the MBeanServer via a createMBeanServer() method allows manageable components loaded by the application to locate the MBeanServer using findMBeanServer() and then register their MBeans.

When would it make sense to create an MBeanServer via one of the newMBeanServer() methods? Consider the following scenario: A developer wants to create a "self-managing" component that can be used by applications without regard to an application's particular choice, or lack thereof, of management mechanism. Such a component could be composed of a set of MBeans registered with an MBeanServer created by the component itself. The component's management policy could be specified via a .properties file or an LDAP profile. If the component uses one of the createMBeanServer() methods to generate its internal MBeanServer, then that MBeanServer will be accessible to any other JMX-aware code running in the same JVM as the component. Such accessibility may be neither useful to external management agents , nor desirable from the component developer's perspective. In this case using newMBeanServer() better meets the component developer's requirements.



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