Dashboard > JJGuidelines > Home > Appendix B - Guidelines Rules > B.7. JMX rules
JJGuidelines Log In | Sign Up   View a printable version of the current page.
B.7. JMX rules
Added by Alexander Bollaert, last edited by Alexander Bollaert on Jan 15, 2007  (view change)

JMX rules

Overview

Table B.7. JMX rules

Rules
JMX_001: Name A MBean Interface Like _ImplementingClassName_MBean
JMX_002: Declare A MBean Interface public
JMX_003: Provide A public No Arg Constructor For A MBean Implementing Class
JMX_004: Make A Getter Or Setter Follow The Naming Conventions
JMX_005: Return The Property Type For A Getter
JMX_006: Declare A Getter Without Parameters
JMX_007: Declare A Setter With At Least 1 Parameter
JMX_008: Declare A Setter With At Most 1 Parameter
JMX_009: Do Not Define The Same Getter Twice
JMX_010: Match Getter And Setter

JMX_001: Name A MBean Interface Like _ImplementingClassName_MBean

The management interface must have the same name followed by MBean as the resource it provides the interface for.

WRONG

public interface CacheMBean {
    // ...
}

public class MyCache implements CacheMBean {
}

WRONG

public interface MyCacheMbean {
    // ...
}

public class MyCache implements CacheMbean {
}

RIGHT

public interface MyCacheMBean { 
    // ... 
} 

public class MyCache implements MyCacheMBean { 
}

The naming of the interface and the implementing class are case sensitive. MyCacheMBean is not the same as MyCacheMbean.

If you declare the interface as in the second wrong example the compiler will of course not complain. However when you try to register your MBean with the MBean server a NotCompliantMBeanException will be thrown.

JMX_002: Declare A MBean Interface public

The interface must be public.

WRONG

interface CacheMBean { 
    // ... 
}

RIGHT

public interface CacheMBean { 
    // ...
}

The MBean can be created and consequently registered with the MBean server. However when trying to use the management interface a javax.management.ReflectionException will be thrown.

JMX_003: Provide A public No Arg Constructor For A MBean Implementing Class

The implementing class needs a public constructor.

WRONG

public class MyCache implements MyCacheMBean { 
    // a private constructor 
    // so that programmers cannot instantiate this class 
    private MyCache() { 
        
    } 
    //There is no means for the MBeanServer to instantiate the class 
    // ... 
}

RIGHT

public class MyCache implements MyCacheMBean { 
    // a default no-argument constructor is provided by the compiler 
    // ... 
}

Also constructors with arguments can be used.

Although the system will generate a no-argument constructor for you if you not define any constructor, it is recommended that you explicitly declare the no-arg constructor.

JMX_004: Make A Getter Or Setter Follow The Naming Conventions

A getter or setter needs to follow the naming convention.

WRONG

public interface MyCacheMBean {
    public void setmaxEntities(long nbr);

    // Wrong capitalisation
    public long getMax();
    // Does not use the name of the arguments it gets
}

RIGHT

public interface MyCacheMBean {
    public void setMaxEntities(long nbr);

    public long getMaxEntities();

}

For Getters returning a boolean value the isProperty form is also accepted, e.g. isEmpty.

JMX_005: Return The Property Type For A Getter

A getter needs to return the type of the property it is getting.

WRONG

public interface MyCacheMBean {
    public void getMaxEntities();
    // Will be viewed as an operation
    // ...
}

JMX_006: Declare A Getter Without Parameters

A getter shouldn't have any parameters.

WRONG

public interface MyCacheMBean {
    public long getMaxEntities(long anArg);
    // Will be viewed as an operation
    // ...
}

JMX_007: Declare A Setter With At Least 1 Parameter

A setter should have at least 1 parameter.

WRONG

public interface MyCacheMBean {
    public void setMaxEntities();
    // Will be viewed as an operation
    // ...
}

In fact such operations will be regularly used for setting boolean values., e.g. enableCaching() in stead of setCaching(boolean arg).

JMX_008: Declare A Setter With At Most 1 Parameter

A setter may only have one parameter for an MBean.

WRONG

public interface MyCacheMBean {
    public void setMaxEntities(long nbr, boolean allowShrinkage);
    // Will be viewed as an operation
    // ...
}

JMX_009: Do Not Define The Same Getter Twice

A getter must not be defined twice or more.

WRONG

public interface MyCacheMBean {
    public boolean isEmpty();

    public boolean getEmpty();
    // ...
}

JMX_010: Match Getter And Setter

Getters and setters need to match.

WRONG

public interface MyCacheMBean {
    // Maximum entities is read/write
    public void setMaxEntities(int nbr);

    public long getMaxEntities();
    // ...
}

The setter takes precedence over the getter (although this could be different for different MBean server implementations). In other words: there is NO getter defined for maxEntities. The MBean server had to choose between long and int and has given preference to the setter. There is now an operation

JMX_011: Do Not Construct An MBean

Use the instantiate method instead.

WRONG

Object myCache = new MyCache();

RIGHT

Object myCache = mbs.instantiate("MyCache");

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Hosted by JavaLobby
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.5 Build:#520 Jun 27, 2006) - Bug/feature request - Contact Administrators