Java Dynamic Management Kit 5.0 Tutorial

Target Object(s)

The object instance that actually embodies the behavior of the managed resource is called the target object. The last step in creating a model MBean is to give the MBean skeleton and its defined management interface a reference to the target object. Thereafter, the model MBean can handle management requests, forward them to the target object, and handle the response.

Example 3–2 implements the TestBean class that is the simple managed resource in our example. Its methods provide the implementation for two attributes and one operation.

Example 3–2 Implementing the Managed Resource

public class TestBean 

    // Constructor
    public TestBean() {
        echo("\n\tTestBean Constructor Invoked: State " + 
             state + " nbChanges: " + nbChanges +
             " nbResets: " + nbResets);


    // Getter and setter for the "State" attribute
    public String getState() {
        echo("\n\tTestBean: getState invoked: " + state);
        return state;

    public void setState(String s) {
        state = s;
        echo("\n\tTestBean: setState to " + state +
             " nbChanges: " + nbChanges);

    // Getter for the read-only "NbChanges" attribute
    public Integer getNbChanges() {
        echo("\n\tTestBean: getNbChanges invoked: " + nbChanges);
        return new Integer(nbChanges);

    // Method of the "Reset" operation
    public void reset() {
        echo("\n\tTestBean: reset invoked ");
        state = "reset initial state";
        nbChanges = 0;

    // Other public method; looks like a getter,
    // but no NbResets attribute is defined in
    // the management interface of the model MBean
    public Integer getNbResets() {
        echo("\n\tTestBean: getNbResets invoked: " + nbResets);
        return new Integer(nbResets);

    // Internals
    private void echo(String outstr) {

    private String  state = "initial state";
    private int     nbChanges = 0;
    private int     nbResets = 0;


By default, the model MBean handles a managed resource that is contained in one object instance. This target is specified through the setManagedResource method defined by the ModelMBean interface. The resource can encompass several programmatic objects because individual attributes or operations can be handled by different target objects. This behavior is configured through the optional targetObject and targetType descriptor fields of each attribute or operation.

In Example 3–3, one of the operations is handled by an instance of the TestBeanFriend class. In the definition of this operation's descriptor, we set this instance as the target object. We then create the operation's ModelMBeanOperationInfo with this descriptor and add it to the list of operations in the metadata for our model MBean.

Example 3–3 Setting Other Target Objects

MBeanParameterInfo[] params = null;

Descriptor getNbResetsDesc = new DescriptorSupport(new String[]
                                    { "name=getNbResets",
TestBeanFriend tbf = new TestBeanFriend();

dOperations[1] = new ModelMBeanOperationInfo(
                "getNbResets(): get number of resets performed",
                         params ,