2
Tutorial Example
This chapter is a tutorial based on the Simple example provided with the Java Dynamic Management Kit. The directory containing the source files for the Simple example depends on the operating environment:
Before compiling any of the examples, copy the contents of the directory containing the example to another location. The file access permissions of the directories under the installation directory do not allow ordinary users write access.
Note - For standard JDK commands, only the command name and any required arguments are given. It is assumed that the path to the command is defined either in the command you type or in your PATH variable.
Developing an M-Bean
The simple m-bean provided in SimpleBean.java implements the following design guidelines:
CODE EXAMPLE 2-1 shows the Java class definition of a simple m-bean.
To compile the m-bean, type this command:
Developing an Agent
The SimpleAgent example shows the code required in an agent for:
The Java source code of this example is shown in CODE EXAMPLE 2-2.
Initializing the Framework
To initialize the framework, create an instance of the Java class com.sun.jaw.reference.agent.cmf.Framework. CODE EXAMPLE 2-3 shows code for initializing the framework. In this example, the default constructor of the com.sun.jaw.reference.agent.cmf.Framework is used.
Adding the Metadata Service
To add an instance of the metadata service supplied with the Java Dynamic Management Kit, create an instance of the Java class com.sun.jaw.impl.agent.services.light.MetaDataSrv and pass this instance to the framework. CODE EXAMPLE 2-4 shows code for adding the metadata service to a Java Dynamic Management agent.
Adding Adaptors
For a Java Dynamic Management agent to be manageable, it must contain at least one adaptor. CODE EXAMPLE 2-5 shows code for adding the HTTP/TCP adaptor to a Java Dynamic Management agent. In this example, the newObject method of the Framework class is called to create an instance of the adaptor.
Compiling and Running the Agent
Before running the example agent, make sure that the Java Dynamic Management base agent is not running on the same machine. You must also make sure that the CLASSPATH environment variable is set correctly, see Appendix B "Environment Variables" for further details.
|
To Compile the Agent
|
- 1. Go to the directory that contains the source files of the example agent.
- 2. Type this command:
|
To Run the Agent
|
- 1. If necessary, stop the Java Dynamic Management base agent.
- 2. Type this command:
Generating a C-Bean
A c-bean is a representation of an m-bean to a Java manager. The mogen tool provided with the Java Dynamic Management Kit enables a c-bean to be generated automatically from an m-bean.
The Java class definition of a simple m-bean is shown in CODE EXAMPLE 2-1. Use mogen to generate a c-bean from the SimpleBean.class m-bean. The m-bean class input to mogen must be in the form of a compiled Java class, not a source file. The c-bean that mogen generates is in the form of Java source code, which you have to compile.
|
To Generate the Example C-Bean
|
| Type the appropriate command for your operating environment:
|
The SimpleBeanMO interface that mogen generates when compiling this simple m-bean is shown in CODE EXAMPLE 2-6. In addition, mogen generates the file SimpleBeanMOStub.java containing an implementation of the SimpleBeanMO interface.
|
package examples.tutorial;
|
|
|
import java.lang.*;
|
import java.lang.reflect.*;
|
import com.sun.jaw.reference.common.*;
|
import com.sun.jaw.reference.client.mo.*;
|
|
/**
|
* Generated by the mogen compiler version:
|
* Generator.java 1.6 06/04/98 SMI
|
*
|
* @see com.sun.jaw.tools.MoGen
|
*/
|
public interface SimpleBeanMO extends ManagedObject {
|
|
public Integer getNbChanges()
|
throws InstanceNotFoundException, PropertyNotFoundException,
|
InvocationTargetException;
|
|
public String getState()
|
throws InstanceNotFoundException, PropertyNotFoundException,
|
InvocationTargetException;
|
|
public void setState(String value)
|
throws InstanceNotFoundException, IllegalAccessException,
|
PropertyNotFoundException, InvalidPropertyValueException,
|
ClassNotFoundException, InstantiationException,
|
InvocationTargetException;
|
|
public void SetState(String oper, String value)
|
throws InstanceNotFoundException, IllegalAccessException,
|
PropertyNotFoundException, InvalidPropertyValueException,
|
ClassNotFoundException, InstantiationException,
|
InvocationTargetException;
|
|
public void performReset()
|
throws InstanceNotFoundException, NoSuchMethodException,
|
InvocationTargetException, IllegalAccessException;
|
}
|
|
|
To Compile the Example C-Bean
|
| Type this command:
|
Developing a Manager
The SimpleClient example shows the code required in a manager for:
The Java source code of this example is shown in CODE EXAMPLE 2-7.
Instantiating an Adaptor Client
For a Java manager to be able to manage a Java Dynamic Management agent, it must contain an adaptor client. An adaptor client is an instance of a Java class that implements the com.sun.jaw.reference.client.adaptor.AdaptorMO interface. The adaptorMO interface provides a means for accessing remote m-beans through the adaptor in an agent. CODE EXAMPLE 2-8 shows code for creating and initializing an instance of the com.sun.jaw.impl.adaptor.rmi.AdaptorClient class. This class implements the AdaptorMO interface based on the RMI system.
Connecting an Adaptor Client
To connect an adaptor client to an adaptor, invoke the connect method of the adaptor client as shown in CODE EXAMPLE 2-9. In this example, the host name, port number and object name of the remote adaptor are specified.
Creating an M-Bean Remotely
To create an m-bean remotely, instantiate its object name and use this instance to create an m-bean in the agent, as shown in CODE EXAMPLE 2-10.
Performing Management Operations
CODE EXAMPLE 2-11 shows how to use a handle on an m-bean to perform management operations on the m-bean. In this example, the manager is able to change the state of the m-bean by invoking the setState method and reset the number state changes by invoking the performReset method.
Disconnecting an Adaptor Client
To disconnect an adaptor client from an adaptor, invoke the disconnect method of the adaptor client as shown in CODE EXAMPLE 2-12.
Compiling and Running the Manager
|
To Compile the Manager
|
| Type this command:
|
|
To Run the Manager
|
| Type this command:
|
Further Examples
The Java Dynamic Management Kit provides a number of examples. The directory containing the source files for the examples depends on the operating environment:
For further information on these examples, including README files, open the HTML page for your operating environment in a web browser:
A further agent example is described in Appendix D.
Copyright 1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California 94303 U.S.A.
Copyright in French