Standard MBeans and JMX
In this chapter, we will discuss all the aspects of instrumentation using Standard MBeans.
First, you will look at the formal definition of an MBean, as defined in the JMX specification. You will then go through the naming conventions and properties of Standard MBeans and see how to implement the management interface using the Standard MBean mechanism. At the end of this chapter, you will look at the notification mechanism defined in the JMX specification and see how it can be used for communication between the managed component and the management application.
In this chapter, you will write example code to demonstrate the features of Standard MBeans. You will later use the same base example to implement Dynamic MBeans and compare the two different kinds of instrumentation of managed components. Also, familiarizing yourself with the JMX notification mechanism is important at this point, because it will be featured in the subsequent chapters of the book.
MBean Definition
MBeans must be concrete Java classes. That is the first requirement for all MBeans, regardless of their type. The MBean must be declared as a public, non-abstract class. Classes with package-only visibility or abstract classes are not compliant MBeans. The public, non-abstract class requirement is to ensure that the agent is able to instantiate the MBean on request.
For this same reason, an MBean must have at least one public constructor. An MBean is allowed to have any number of constructors that can be defined using the usual Java visibility rules with keywords public, private, or protected. Nevertheless, at least one of the constructors must be public. Also, the constructor is allowed to have any number of parameters of any type. However, remember that it is the developer's or the administrator's responsibility to make sure all the classes used in the constructor parameter list are available to the agent when it needs to instantiate the MBean.
In the Hello MBean example we built in Chapter 1, "Getting Started," you didn't declare any constructors at all. However, both of the agent implementations you used accepted the component as a valid MBean. In the example, a default constructor was used. A default constructor is a public constructor that takes no arguments. Therefore, the Hello class was a valid MBean. However, keep in mind that the default constructor is only guaranteed when you do not explicitly declare any other constructors, regardless of their visibility.
The MBean class must implement its own corresponding MBean interface or a DynamicMBean interface. If an MBean implements its own statically-typed Java interface, it is called a Standard MBean. Dynamic MBeans implement the DynamicMBean interface. We will cover the Dynamic MBeans in detail in the next chapter.
These are the three rules you need to remember when developing your MBeans.
An MBean must be a public, non-abstract class.
An MBean must have at least one public constructor.
An MBean must implement its own corresponding MBean interface or implement the DynamicMBean interface.