Java Dynamic Management Kit 3.2 Programming Guide | ||||
---|---|---|---|---|
![]() | ![]() | Chapter 4. Operations on M-Beans | ![]() | ![]() |
To enable an m-bean to be managed by a Java Dynamic Management agent, register it in the framework's repository. Registering an m-bean does not necessarily require any modification of code within the m-bean itself. However, you can add code to an m-bean so that it can control its own registration, should you wish to do so. For more information, see Using the initCmf Method of the M-Bean.
An m-bean is registered by code in the agent that wishes to manage the m-bean's resources. The Java Dynamic Management Kit enables you to register an existing m-bean instance or to instantiate and register an m-bean in a single operation. When registered, an m-bean is assigned an object name, either by default or as specified by the agent.
An m-bean can also be instantiated and registered remotely by code in a Java Dynamic Management client. See Operations on an Agent in Chapter 7 for details about registering and interacting with the m-bean through an adaptor.
In either case, you may register the new object in a persistent repository if your agents uses one. For information on the repositories supplied with the Java Dynamic Management Kit, refer to Repository Service in Chapter 10.
Regardless of the registration method, the framework generates an event whenever an m-bean is registered. The information sent with the event includes the new object name of the m-bean. For more information, see Framework Events.
To register an m-bean, invoke one of the following methods of the Framework class:
addObject() to use the default storage mechanism for storing the m-bean and its object name
addDBObject() to specify that the m-bean and its object name are persistent
When you invoke either of these methods, you have to provide:
An existing m-bean
The object name (see Object Name) to be used for registering the m-bean
Example 4-1 shows how to add an m-bean to the repository by invoking the addObject() method of the Framework class.
Example 4-1. Registering a Volatile M-Bean
// Instantiate the text m-bean text = new TextSimple(); // Create the object name String domain = cmf.getDomain(); name = new ObjectName( domain + ":" + "TextSimple.serialNo=1" ); // Register the text m-bean cmf.addObject( text, name ); |
Example 4-2 shows how to add persistent information to the repository.
Example 4-2. Registering a Persistent M-Bean
// Instantiate the text m-bean text = new TextSimple(); // Create the object name String domain = cmf.getDomain(); name = new ObjectName( domain + ":" + "TextSimple.SerialNo = 1" ); // Register the text m-bean in persistent storage try { cmf.addDBObject( text, name ); } catch( ServiceNotFoundException e ) { e.printStackTrace(); } |
To instantiate and register an m-bean in a single operation, invoke one of the following methods of the Framework class:
newObject() to use the default storage mechanism for storing the m-bean
newDBObject() to specify that the m-bean is persistent
When you invoke either of these methods, you have to provide:
The Java class of the m-bean to be instantiated
The object name (see Object Name) to be used for registering the m-bean
Note - The Java class of the m-bean to be instantiated must contain a public constructor that does not take any arguments. If it does not, an exception is thrown when the newObject() or newDBObject() method is invoked.
The framework uses a class loader (see Chapter 13) to locate the Java class of the m-bean to be instantiated. It then creates an instance of the class. Once the m-bean has been instantiated, it must be registered with the framework. It is possible to register an m-bean by using:
The initCmf() method of the m-bean, or
The framework
To register an m-bean by using the initCmf() method, all you need to do is make sure that the Java class definition of the m-bean contains this method. The initCmf() method must be implemented as described in Initializing an M-Bean - initCmf Method in Chapter 3.
After the m-bean has been instantiated through the newObject() or newDBObject() methods, the framework uses the metadata service to find a method called initCmf() in the newly created m-bean. Provided such a method is present in the m-bean, the framework invokes it, giving:
A reference to itself as first parameter
The object name to use for registering the m-bean as second parameter
The m-bean then registers itself with the repository if the initCmf() method is implemented correctly. The initCmf() method can also take two additional parameters, see Initializing an M-Bean - initCmf Method in Chapter 3.
The services provided under com.sun.jaw.impl.agent.services contain an implementation of the initCmf() method.
If you do not define an initCmf() method in an m-bean, the framework registers the m-bean by invoking one of these methods:
addObject() if the m-bean was instantiated by invoking newObject()
addDBObject() if the m-bean was instantiated by invoking newDBObject()
Refer to Registering an Existing M-Bean for more information about these methods.
Example 4-3 shows how to add an m-bean, in this example the metadata service, to the framework.
Example 4-3. Instantiating an M-Bean
String domain = cmf.getDomain(); cmf.newObject( metImplName, domain + ":" + ServiceName.META, null ); |
All registered m-beans must be assigned a valid object name. To specify the object name explicitly, use the format defined in Object Name. If you do not specify the object name explicitly, the framework assigns a default object name to the m-bean. The format of a default object name is:
frameworkDomain:m-beanClass |
The variable parts of this format are:
frameworkDomain
The domain associated with the instance of the Framework class in the Java Dynamic Management agent. By default, the value of the framework domain is defaultDomain.
m-beanClass
The full Java class name, including the package name, of the m-bean. This is the class part of the object name of the m-bean, without any search key. Only one instance of this class is permitted within the domain.
Note - A default object name does not contain a search key. Therefore, any m-bean assigned a default name by the framework will be a singleton m-bean.
When the addDBObject() method is invoked, the framework invokes the isPersistent() method to check if the repository service provides persistent storage. If the repository does not provide persistent storage, invoking addDBObject() throws a ServiceNotFoundException exception. The isPersistent() method is defined in the MoRepSrvIf interface.
Two implementations of the repository service are supplied with the Java Dynamic Management Kit:
The com.sun.jaw.impl.agent.services.light.RepositorySrv repository provides only volatile storage, it is the default repository
The com.sun.jaw.impl.agent.services.persistent.PersistentRepSrv repository provides both volatile and persistent storage
Chapter 10 explains how to initialize the framework with a given repository and gives details of both implementations.
![]() | ![]() | ![]() |
Object Name | ![]() | Deleting M-Beans |