Java Dynamic Management Kit 3.2 Programming Guide
[ Previous ][ Fast Back ]Chapter 2. Tutorial Example [ Next ]

Developing an Agent

The SimpleAgent example shows the code required in an agent for:

The Java source code of this example is shown in Example 2-2.

Example 2-2. SimpleAgent.java
// Copyright (c) 03/12/99, by Sun Microsystems, Inc.
// All rights reserved.

// "@(#)SimpleAgent.java 3.3 99/03/12 SMI"

import com.sun.jaw.reference.common.* ;
import com.sun.jaw.reference.agent.cmf.* ;
import com.sun.jaw.reference.agent.services.*;
import com.sun.jaw.impl.agent.services.light.* ;

public class SimpleAgent {

    public static void main(String argv[]) {
        try {
            String domain = "defaultDomain" ;

            // Set up the Framework.
            Framework cmf = new Framework() ;

            // Set up the Metadata service.
            String mtdSrvClass = "com.sun.jaw.impl.agent.services.light.MetaDataSrv" ;
            String mtdSrvName = domain + ":" + ServiceName.META ;
            cmf.newObject(mtdSrvClass, mtdSrvName, null) ;

            // Set up the RMI Adaptor.
            String rmiSrvClass = "com.sun.jaw.impl.adaptor.rmi.AdaptorServerImpl" ;
            String rmiSrvName = domain + ":" + ServiceName.ADAPTOR + ".protocol=rmi" ;
            cmf.newObject(rmiSrvClass, rmiSrvName, null) ;

            // Set up the HTTP Adaptor.
            String httpSrvClass = "com.sun.jaw.impl.adaptor.http.AdaptorServerImpl" ;
            String httpSrvName = domain + ":" + ServiceName.ADAPTOR + ".protocol=http" ;
            cmf.newObject(httpSrvClass, httpSrvName, null) ;
          
        }
        catch(Exception e) {
            System.out.println("Got an exception !") ;
            e.printStackTrace() ;
            System.exit(1) ;
        }
    }
}

Initializing the Framework

To initialize the framework, create an instance of the Java class com.sun.jaw.reference.agent.cmf.Framework. 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.

Example 2-3. Initializing the Framework
import com.sun.jaw.reference.common.* ;
import com.sun.jaw.reference.agent.cmf.* ;
import com.sun.jaw.reference.agent.services.*;
import com.sun.jaw.impl.agent.services.light.* ;
...
    // Set up the Framework
    Framework cmf = new Framework() ;

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. Example 2-4 shows code for adding the metadata service to a Java Dynamic Management agent.

Example 2-4. Adding the Metadata Service
// Set up the Metadata service.

String mtdSrvClass = "com.sun.jaw.impl.agent.services.light.MetaDataSrv" ;
String mtdSrvName = domain + ":" + ServiceName.META ;
cmf.newObject(mtdSrvClass, mtdSrvName, null) ;

Adding Adaptors

For a Java Dynamic Management agent to be manageable, it must contain at least one adaptor. 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.

Example 2-5. Adding an Adaptor
// Set up the HTTP Adaptor.

String httpSrvClass = "com.sun.jaw.impl.adaptor.http.AdaptorServerImpl" ;
String httpSrvName = domain + ":" + ServiceName.ADAPTOR + ".protocol=http" ;
cmf.newObject(httpSrvClass, httpSrvName, null) ;

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. The class path must include the directory which contains the compiled SimpleAgent class. It must also include the directory containing Java Dynamic Management Kit classes; see Appendix B 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:
    prompt% javac SimpleAgent.java

To Run the Agent

  1. If necessary, stop the Java Dynamic Management base agent.

    • In the Solaris operating environment, type

    prompt% installDir/SUNWconn/jaw/bin/jaw stop

    • In the Windows NT operating environment, use the Task Manager, or in the window where you started the base agent, type Control+C.

  2. Type this command:
    prompt% java SimpleAgent

[ Previous ][ Home ][ Next ]
Developing an M-Bean[ Up ]Generating a C-Bean