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

Developing a Manager

The SimpleClient example shows the code required in a manager for:

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

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

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

import java.net.* ;
import java.util.* ;
import com.sun.jaw.reference.common.* ;
import com.sun.jaw.impl.adaptor.rmi.* ;

public class SimpleClient {

    public static void main(String argv[]) {

        try {
            String agentHost = InetAddress.getLocalHost().getHostName() ;
            if (argv.length >= 1) 
                agentHost = argv[0] ;
      
            System.out.println(">>> Connecting to " + agentHost + "...") ;

            // Set up the RMI AdaptorClient. 
            //
            AdaptorClient adaptor = new AdaptorClient() ;

            // Initialize communication with the remote RMI managed object server.
            //
            adaptor.connect(null, agentHost, 1099, ServiceName.APT_RMI) ;

            // Create an instance of the SimpleBean m-bean in the 
            // remote object server. 
            //
            String simpleClass = "SimpleBean" ;
            ObjectName simpleName = new ObjectName("defaultDomain:SimpleBean.id=1") ;
            SimpleBeanMO simple = (SimpleBeanMO)adaptor.cb_newMO(
                                  simpleClass, simpleName, null) ;
      
            // Access and modify the properties of the SimpleBean m-bean remotely.
            //
            System.out.println("\nsimple.state     = " + simple.getState()) ;
            System.out.println("simple.nbChanges = " + simple.getNbChanges()) ;

            System.out.println("\n>>> Changing state...") ;
            simple.setState("New state") ;

            System.out.println("simple.state     = " + simple.getState()) ;
            System.out.println("simple.nbChanges = " + simple.getNbChanges()) ;

            System.out.println("\n>>> Resetting change counters...") ;
            simple.performReset() ;
      
            System.out.println("simple.state     = " + simple.getState()) ;
            System.out.println("simple.nbChanges = " + simple.getNbChanges()) ;

            // Terminates the communication with the remote managed object server.
            //
            adaptor.disconnect() ;
            System.exit(0) ;
        }
        catch(Exception e) {
            System.out.println("Got an exception !") ;
            e.printStackTrace() ;
            System.exit(1) ;
        }
    }
}

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. 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.

Example 2-8. Instantiating an Adaptor Client
// Set up the RMI AdaptorClient.
//
AdaptorClient adaptor = new AdaptorClient() ;

Connecting an Adaptor Client

To connect an adaptor client to an adaptor, invoke the connect() method of the adaptor client as shown in Example 2-9. In this example, the host name, port number and object name of the remote adaptor are specified.

Example 2-9. Connecting an Adaptor Client
// Initialize communication with the remote RMI managed object server.
//
adaptor.connect(null, agentHost, 1099, ServiceName.APT_RMI) ;

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 Example 2-10.

Example 2-10. Creating an M-Bean Remotely
// Create an instance of the SimpleBean m-bean in the
// remote object server.
//
String simpleClass = "SimpleBean" ;
ObjectName simpleName = new ObjectName("defaultDomain:SimpleBean.id=1") ;
SimpleBeanMO simple = (SimpleBeanMO)adaptor.cb_newMO(
                          simpleClass, simpleName, null) ;

Performing Management Operations

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.

Example 2-11. Performing Management Operations on an M-Bean
// Access and modify the properties of the SimpleBean m-bean remotely.
//
System.out.println("\nsimple.state     = " + simple.getState()) ;
System.out.println("simple.nbChanges = " + simple.getNbChanges()) ;

System.out.println("\n>>> Changing state...") ;
simple.setState("New state") ;

System.out.println("simple.state     = " + simple.getState()) ;
System.out.println("simple.nbChanges = " + simple.getNbChanges()) ;

System.out.println("\n>>> Resetting change counters...") ;
simple.performReset() ;

System.out.println("simple.state     = " + simple.getState()) ;
System.out.println("simple.nbChanges = " + simple.getNbChanges()) ;

Disconnecting an Adaptor Client

To disconnect an adaptor client from an adaptor, invoke the disconnect() method of the adaptor client as shown in Example 2-12.

Example 2-12. Disconnecting an Adaptor Client
// Terminates the communication with the remote managed object server.
//
adaptor.disconnect() ;

Compiling and Running the Manager

To Compile the Manager

To Run the Manager


[ Previous ][ Home ][ Next ]
Generating a C-Bean[ Up ]Further Examples