Java Management Extensions
by Paul Tremblett


Listing One
import java.util.Date;
public class TrivialExample implements TrivialExampleMBean {
  private Date readOnlyAttribute = new Date();
  private String readWriteAttribute = "Change me";
  private String settableAttribute;
  public Date getanAttributeYouCanOnlyRead() {
    readOnlyAttribute = new Date();
    return readOnlyAttribute;
  }
  public String getanAttributeYouCanReadAndWrite() {
    return readWriteAttribute;
  }
  public void setanAttributeYouCanReadAndWrite(String a) {
    readWriteAttribute = a;
  }
  public String getanAttributeYouCanSetByInvokingSomeMethod() {
    return settableAttribute;
  }
  public void invokeMethodWithoutArgument() {
    readWriteAttribute = "Change me";
  }
  public void invokeMethodWithArgument(String s) {
    settableAttribute = s;
  }
}

Listing Two
import java.util.Date;
public interface TrivialExampleMBean {
  public Date getanAttributeYouCanOnlyRead();
  public String getanAttributeYouCanReadAndWrite();
  public void setanAttributeYouCanReadAndWrite(String s);
  public String getanAttributeYouCanSetByInvokingSomeMethod();
  public void invokeMethodWithoutArgument();
  public void invokeMethodWithArgument(String arg);
}

Listing Three
import com.sun.jdmk.comm.HtmlAdaptorServer;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;

public class SimpleAgent {
  private static final int DEFAULT_PORT = 8787;
  private static final String ADAPTOR_NAME_BASE =
    "Adaptor:name=html,port=";
  public static void main(String[] args) {
    int port = DEFAULT_PORT;
    System.out.print("Creating server ");
    try {
      switch (args.length) {
        case 0:
          System.out.println("using default port (" + DEFAULT_PORT + ")");
          break;
        case 1:
          port = Integer.parseInt(args[0]);
          System.out.println("using port " + port);
          break;
        default:
          port = Integer.parseInt(args[0]);
          System.out.println("using port " + port);
          System.out.println("Ignoring " + args.length +
            " extraneous argument" + ((args.length == 2) ? 
            "" : "s"));
          break;
      }
    }
    catch (NumberFormatException e) {
      System.err.println("\nUnable to parse " + args[0] + " as port number");
      System.exit(1);
    }
    MBeanServer server = MBeanServerFactory.createMBeanServer();
    HtmlAdaptorServer adaptor = new HtmlAdaptorServer(port);
    ObjectName adaptorObjectName = null;
    String adaptorName = null;
    try {
      adaptorName = ADAPTOR_NAME_BASE + Integer.toString(port);
      adaptorObjectName = new ObjectName(adaptorName);
      server.registerMBean(adaptor, adaptorObjectName);
    }
    catch(Exception e) {
      System.err.println("\nUnable to create HTML adaptor named: " 
           + adaptorName);
      e.printStackTrace();
      System.exit(1);;
    }
    adaptor.start();    
    System.out.println("HTMLAdaptor started");
    System.out.println("Accepting connections on port " + port);
  }   
}




1


