Tải bản đầy đủ - 0 (trang)
Figure 2-10. Operations exposed for management on the HTMLAdaptorServer

Figure 2-10. Operations exposed for management on the HTMLAdaptorServer

Tải bản đầy đủ - 0trang

2.3 Downloading and Installing the JMX Reference Implementation

Before you can build and run the application, you must first obtain the JMX RI. The

easiest way to do this is to download it from Sun Microsystems at

http://java.sun.com/products/JavaManagement/. Select either the source code or binary

RI under "JMX Deliverables" and follow the instructions.

Once you've downloaded the RI, you should unzip the downloaded file directly into your

c:\ drive for Windows or your home directory for Unix.



69



Chapter 3. Dynamic MBeans

Standard MBeans are well suited for management interfaces that are relatively static.

However, if a management interface must be defined for an existing resource, is likely to

evolve over time, or for some other reason needs to be exposed at runtime, JMX provides

an interface that allows you to do just that. In this chapter, we will start by looking at the

reasons for instrumenting application code as dynamic MBeans. Then we will look at the

metadata classes that are used to define the management interface of a dynamic MBean.

Next, we will explore ways to implement the DynamicMBean interface and show how the

MBeanInfo metadata class is critical in making dynamic MBeans work. At the end of the

chapter, we will examine the management interface inheritance patterns that are used

with dynamic MBeans.

This chapter assumes that you either are already familiar with

standard MBeans or have read Chapter 2.



3.1 Why Use Dynamic MBeans?

The main reason to use dynamic MBeans is to more easily instrument existing code that

is written in a manner that conflicts with the standard MBean design patterns we

discussed in Chapter 2. The dynamic MBean interface is determined not through

introspection, but rather through a method call on the dynamic MBean itself. This method,

called getMBeanInfo(), returns information about the management interface and is

defined on the DynamicMBean interface; it is the portal through which a management

application views what has been exposed on the management interface of a resource that

has been instrumented as a dynamic MBean.

Another reason to use dynamic MBeans is so that you can provide descriptions of the

MBean features that are visible to the management application. An MBean feature is an

attribute, constructor, operation, parameter, or notification of an MBean. The feature

description is a brief explanation of what a particular feature means when viewed from a

management application. The feature's name usually indicates what it means, but this isn't

always the case. Feature descriptions are not available to standard MBeans.

Because the dynamic MBean interface is exposed at runtime, rather than at compile time

(as a standard MBean is), the management interface is exposed through metadata classes.

If the management interface is likely to change over time, dynamic MBeans offer a more

flexible way to instrument a resource. The management interface is not statically bound

to a dynamic MBean, as it is for a standard MBean. Rather, the management interface is

exposed dynamically. As such, it is conceivable that a dynamic MBean could—without

code changes—expose a different interface from one instance to the next by reading

which attributes and operations to expose from a configuration file.



3.2 How Do Dynamic MBeans Work?

70



Like standard MBeans, dynamic MBeans must be created and registered with the MBean

server. When the MBean server is asked to register a dynamic MBean, however, no

introspection is performed. Recall that it is the strict application of the standard MBean

design patterns (discussed in Chapter 2)—enforced through introspection—that tells the

MBean server what management interface is exposed on an MBean. So, how does a

dynamic MBean expose its management interface?

Instead of using a Java interface with the name "MBean" on it, dynamic MBeans use

metadata classes to expose their management interfaces. They make that metadata

available through an interface called DynamicMBean, which must be implemented by all

dynamic MBeans. This interface is shown in Example 3-1.

Example 3-1. The DynamicMBean interface

package javax.management;

public interface DynamicMBean {

public Object getAttribute(String attribute)

throws AttributeNotFoundException, MBeanException,

ReflectionException;

public void setAttribute(Attribute attribute)

throws AttributeNotFoundException, InvalidAttributeValueException,

MBeanException, ReflectionException;

public AttributeList getAttributes(String[] attributes);

public AttributeList setAttributes(AttributeList attributes);

public Object invoke(String actionName, Object params[], String

signature[])

throws MBeanException, ReflectionException;

public MBeanInfo getMBeanInfo();

}



Essentially, the DynamicMBean interface provides a way for a management interface to do

four things:











Dynamically discover the management interface exposed by the MBean

(getMBeanInfo()).

Retrieve the value of one or more attributes on the management interface

(getAttribute() and getAttributes(), respectively).

Set the value of one or more attributes on the management interface (setAttribute()

and setAttributes(), respectively).

Invoke an operation on the management interface (invoke()).



Instances of the appropriate metadata classes are created by the MBean (usually in the

constructor) and added to the MBeanInfo instance that is returned by getMBeanInfo().

71



Similarly, the management application, after invoking getMBeanInfo(), uses the metadata

classes to discover that interface. In the next section, we will take a look at how a

dynamic MBean uses the metadata classes to expose its management interface.

3.2.1 Describing the Management Interface

Dynamic MBeans tell the MBean server that they are dynamic MBeans by exposing the

DynamicMBean interface, but it is the use of the dynamic MBean metadata classes that

ties it all together. In this section, we will look at these classes and how a dynamic

MBean uses them to describe and expose its management interface to the world.

There are six significant metadata classes:

MBeanInfo

The top-level container of metadata; each MBean requires only one instance of

this class to completely describe its management interface.

MBeanAttributeInfo

Each instance of this class provides information about a single attribute.

MBeanParameterInfo

Each instance of this class provides information about a single parameter.

MBeanConstructorInfo

Each instance of this class provides information about a single constructor and

may contain one or more MBeanParamaterInfo instances.

MBeanOperationInfo

Each instance of this class provides information about a single operation and may

contain one or more MBeanParameterInfo instances.

MBeanNotificationInfo

Each instance of this class contains information about a group of notifications

emitted by this MBean.

Each of these classes (except MBeanInfo) is a subclass of MBeanFeatureInfo. Figure 3-1

shows a UML diagram that describes the multiplicity (or cardinality) between MBeanInfo

and the other metadata classes.



72



Figure 3-1. UML diagram showing the multiplicity between MBeanInfo and the other

metadata classes



As you can see from Figure 3-1, there is a whole lot of aggregation going on! The

aggregation mechanism used by all of the container classes is an array. Each of these

classes is discussed in detail below.

3.2.1.1 MBeanAttributeInfo



This metadata class is used to describe a single attribute that is exposed on an MBean. To

describe a single MBean attribute, six essential properties must be set:

name

The name of the attribute as it appears on the management interface

type

The string representation of the Class object that corresponds to the attribute's

data type

description

The description of the attribute as it should appear to a management application

isReadable

A boolean property that indicates whether or not the attribute's value can be

retrieved and viewed by a management application

isWritable

A boolean property that indicates whether or not the attribute's value can be set

by a management application

isIs

73



A boolean property that indicates whether or not the attribute is a boolean and if

its getter begins with is instead of get

An MBean creates an instance of this class for each attribute that it wants to expose,

using one of two constructors of MBeanAttributeInfo to set its essential properties. The

first constructor is defined as:

public class MBeanAttributeInfo extends MBeanFeatureInfo implements

java.io.Serializable, Cloneable {

// . . .

public MBeanAttributeInfo(String name,

String type,

String description,

boolean isReadable,

boolean isWritable,

boolean isIs) {

// . . .

}

// . . .

}



The name parameter is the name of the attribute as it is exposed on the management

interface and is a String. The name must match exactly what is expected by the methods

on the DynamicMBean interface implementation to retrieve and set the attribute's value.

The type parameter is the fully qualified name of the class that is the type of the

attribute's value. If type is an Integer, for example, this parameter will have the value

"java.lang.Integer". If type is one of the Java fundamental types (e.g., byte, char,

long), you may use either the literal string for that fundamental type or the name attribute

of the TYPE member variable of the corresponding fundamental type java.lang wrapper

class. The following example shows how to use this constructor for the QueueSize

attribute of Queue (whose type is int) by using the literal string explicitly for type int:

// . . .

public Queue(int queueSize) {

super();

MBeanAttributeInfo att = new MBeanAttributeInfo(

"QueueSize",

// name

"int",

// type

"Size of the Queue.", // description

true,

// isReadable

true,

// isWritable

false

// isIs

);

// . . .

}

// . . .



Here is an example that shows how to use the JDK wrapper class for the fundamental

type int to create an MBeanAttributeInfo object for the same QueueSize attribute of

Queue:

74



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Figure 2-10. Operations exposed for management on the HTMLAdaptorServer

Tải bản đầy đủ ngay(0 tr)

×