Chapter 4. Model MBeans

Model MBeans are the most powerful type of MBean. Instrumenting your application resources as model MBeans provides you with the most features and flexibility of any of the MBean types that are fully specified by the current JMX specification. Furthermore, this power comes without a commensurate level of complexity! In this chapter, we will examine the features provided by model MBeans and why you might choose this instrumentation strategy over the others we have discussed so far. Then we will look at how model MBeans work, including a detailed look at the Descriptor class and the metadata classes that are used by resources instrumented as model MBeans. We will also take a look at RequiredModelMBean, a model MBean class that is required to be present in every JMX implementation. Finally, we will develop a working example that uses the same design as the examples of the previous chapters, so you can compare and contrast model MBeans with standard and dynamic MBeans.

An entire book could be devoted to model MBeans, as they are by far the most complex type of MBean. The objective of this chapter is simply to familiarize you sufficiently with the major issues involved in instrumenting your resources as model MBeans that you can do so.

Tip

This chapter assumes that you have read the previous chapter, or are familiar with dynamic MBeans. You should be familiar with the DynamicMBean interface and how to use metadata classes to describe an MBean’s management interface.

In this chapter, we will refer to MBean features, or simply features. A feature is a constituent of the management interface: an attribute, operation, constructor, parameter, or notification.

Why Use Model MBeans?

Model MBeans are dynamic MBeans and so use metadata to describe the features of the MBean. However, there is one significant difference: model MBeans offer the instrumentation developer a metadata class called Descriptor, which is a collection of name/value pairs in which the name is a String and the value is an Object. This allows for a much richer set of metadata to be exchanged with the agent level, other MBeans, and management applications. Model MBeans offer some significant benefits over other JMX instrumentation strategies, as described in this section.

First, instrumenting your application resources as model MBeans allows you to more quickly perform the instrumentation. You can instrument a resource as a model MBean in just a few lines of code. When a resource’s attributes are accessed or changed, or when an operation is invoked, the mechanism used by model MBeans is a callback. In other words, when the metadata for an MBean feature (such as an attribute or operation) is created, a reference to the instance of the resource is stored with the metadata, along with the name of the attribute getter/setter or operation. When a management application manages the MBean, it simply uses this information to call back into the resource.

A second benefit of model MBeans is the feature set that comes along with them. Model MBeans have a rich set of features, including support for:

  • Automatic attribute-change notifications

  • Persistence of the MBean’s state at predefined intervals

  • Logging of certain significant events in state changes of the MBean

  • Accessing MBean state from a cache to improve performance for attributes whose values have a relatively long freshness

A third benefit of model MBeans is that the resource you are instrumenting does not require any code changes. This is a significant advantage when instrumenting existing application or third-party resources that provide a well-defined API. Unlike standard or dynamic MBeans, the resource itself does not have to implement anything to be a perfectly compliant JMX resource. All that is required is that somewhere in the code execution stream there must be code that creates the necessary Descriptor and other metadata classes to instrument the resource. A logical place for this code is in the resource itself, but JMX does not require this.

So how can model MBeans offer so much ease of use, flexibility, and power without a corresponding boost in complexity over standard and dynamic MBeans? We will discuss that and much more in the next section.

Get Java Management Extensions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.