JMX rules
Overview
Table B.7. JMX rules
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
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 {
private MyCache() {
}
}
RIGHT
public class MyCache implements MyCacheMBean {
}
 |
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);
public long getMax();
}
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();
}
JMX_006: Declare A Getter Without Parameters
A getter shouldn't have any parameters.
WRONG
public interface MyCacheMBean {
public long getMaxEntities(long anArg);
}
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();
}
 |
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);
}
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 {
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");