Java Dynamic Management Kit 4.0 Tutorial

The Agent Application

The base agent is a stand-alone application with a main method, but it also has a constructor so that it may be instantiated dynamically by another class.


Example 5-1 Constructor for the Base Agent

public BaseAgent() {
    // Enable the TRACE level according to the level set in system properties
    try {
        Trace.parseTraceProperties();
    }
    catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }

    println("\n\tInstantiating the MBean server of this agent...");
    myMBeanServer = MBeanServerFactory.createMBeanServer();

    // Retrieves ID of the MBean server from the delegate
    try {
        System.out.println("ID = "+ myMBeanServer.getAttribute(
            new ObjectName(ServiceName.DELEGATE), "MBeanServerId"));
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}

In the first statement of the constructor, we enable tracing messages for the agent. The tracing lets us see internal information at runtime, which is useful for debugging. See "Setting Trace Messages" for specifying tracing parameters on the command line. Then we create the MBean server through its static factory class (see "The MBean Server Implementation and Factory"). Its reference is stored in a variable with class-wide scope so that all internal methods have access to the MBean server. Finally, we retrieve the MBean server identification string, for informational purposes only.

After the MBean server is initialized, we are going to create the same three communications MBeans that we saw in the minimal agent.