2nd Chapter JMX MBean basic Knowledge __★java Common components Chinese document ★

Source: Internet
Author: User
Tags memory usage readable jconsole

JMX MBean Basics

1 Introduction

This lesson introduces the basic concepts of the JMX API, the managed beans, or mbeans.

An Mbean is a managed Java object, somewhat like JavaBean, which follows some design patterns of the JMX specification. A device, an application, or any resource that needs to be managed can be represented as an mbean. An Mbean exposes a management interface that mainly includes the following operations: A set of properties that can be read or written, or that can read or write a set of actions that trigger read or write a self-describing

The management interface does not change within the life cycle of an Mbean instance. The Mbean can also send notifications when some predefined events occur.

The JMX API specification defines 5 types of Mbean: standard Mbean Dynamic Mbean development Mbean model Mbean MXBean

Here are a few examples to illustrate the simplest type of mbean, the standard Mbean and the Mxbean.

2 Standard Mbean

This section presents an example of a simple standard mbean.

A standard Mbean is defined: Write an interface called Somethingmbean, and then write a class called something to implement this interface. Each method in an interface defines either an attribute of an mbean or an operation of an mbean. By default, each method defines an action.

Attributes and operations are methods that follow a certain design pattern.

A standard Mbean consists of an Mbean interface and a class that implements the interface. The Mbean interface lists all the properties and operations that need to be exposed. The class that implements this interface provides the functionality of the resource.

The following subsections illustrate how to manage an Mbean through a standard Mbean and a simple jmx broker.

2.1 Mbean Interface

The following is an example of a basic Mbean interface, Hellobean:

By convention, an Mbean interface is named with the name of a class that implements it plus the suffix mbean. In the above code, the name of the interface is Hellobean. We'll introduce the Hello class that implements this interface in the next section.

According to the JMX specification, in addition to the name and type of operations (which can be triggered by an Mbean-managed application), an Mbean interface also contains properties that have names and types, which are readable and may be writable. The Hellobean interface declares two actions: the Add () method and the SayHello () method.

Hellobean declares two attributes: name (a readable string), cacheSize (a readable, writable integer). Getter and setter methods allow access to managed applications and may also change the values of these properties. As the JMX specification defines, a getter method is a public method named after "get" with a return value that is not NULL. A getter method enables a manager to read the value of a property, and the type of the property is the type of the return value. A setter method is a public method that has only one input parameter named after "set". A setter method enables a manager to write a new value to a property, and the type of the property is the type of the input parameter.

These actions and properties are implemented in the next section.

Implementation of 2.2 Mbean

The following Hello class implements the Hellombean interface:

The Hello class explicitly defines the actions and attributes declared in the Hellombean interface. The SayHello () and add () operations are extremely simple, but the actual operations can be as simple or as complex as they need to be.

The method to get the Name property, the method to get the CacheSize property, and the method to set the CacheSize property are also defined in the Hello class. In real-world scenarios, however, property values may change during the run, such as those that indicate boot time or memory usage.

Here, the value of the Name property is only "Reginald" and cannot be changed.

The default value for the CacheSize property is 200, and you can call the SetCacheSize method to change this property. In real-world scenarios, changing the value of a property requires some other action, such as discarding the item or assigning a new entry. In this example, only one message is printed to confirm that the CacheSize attribute value has changed. However, some more complex operations should be defined, rather than simply invoking the println () method.

The Hello Mbean defined above and its interfaces can be used to manage the resources they represent, as shown in the next section.

2.3 Creating JMX agents to manage resources

Once a resource is monitored by an mbean, the resource can be managed through a JMX broker.

The core component of the JMX agent is the Mbean server. An Mbean server is a server for a managed object, and an Mbean is registered to the server. The JMX agent also includes a set of services to manage the Mbean. You can view the API documentation for more information about the Mbeanserver interface and the Mbean server implementation.

The main class shown below represents a basic JMX agent:

First, the JMX proxy class main invokes the Getplatformmbeanserver () method of the Java.lang.management.ManagementFactory class to get an Mbean server from the platform, The server is created and initialized by the platform. If the platform has not yet created an Mbean server, then the Getplatformmbeanserver () method automatically calls the Mbeanserverfactory.creatembeanserver () method to create an Mbean server. The name of the Mbean server instance obtained by the main class is MBS.

Next, the main class defines the object name of an Mbean instance. Each JMX Mbean must have an object name. The object name is an instance of the Jmx class objectname and must conform to the syntax defined by the JMX specification. That is, the object name must contain a field and a set of key properties. The object name defined in the main class, the domain is com.example (that is, the package name that contains the Mbean), and the key property declares that the object's type is hello.

Then, create an mbean of the Hello instance object, together with the object name, to register with the Mbean server MBS. The method implements registration by calling the Jmx method Mbeanserver.registermbean () and passing the object and the object name as arguments.

After the Hello Mbean is registered with the Mbean server, the main class simply waits for the admin action inside the hello to be executed. In this example, these management operations are mainly the SayHello () and Add () methods, as well as the properties of get and set methods.

2.4 Examples of running this standard Mbean

If you've checked the class in the example, you can now run the example. In this example, the jconsole is used to interact with the Mbean. Save the JMX example of the compressed package Jmx_examples.zip to your working directory Work_dir. At the command line or terminal, use the following command to extract the package: Unzip Jmx_examples.zip. Compile the example in the Work_dir directory for all classes: Javac Com/example/*.java. If you are running JDK6, start the Main application using the following command line: Java com.example.Main

If you are running a previous version of JDK6, you will need to use the optional parameters specified below to start the main application and burst the application for monitoring and management: java-dcom.sun.management.jmxremote example. Main

Verify that main is waiting for something to happen. Open a new command line or terminal window on the same machine, enter the command jconsole start Jconsole in the New Connection dialog box, select Com.example.Main from the list, and click Connect. At this point, an overview of the platform's current activities will be displayed. Click on the Mbean tab and this panel will show all the Mbean that has been registered to the Mbean server. In the Mbean tree on the left, expand the Com.example node. You will see the Mbean Hello, which is created and registered in Main. If you click Hello, you will see its associated "Properties" and "operations" nodes in the Mbean tree. In the Mbean tree, expand the Properties node for the Hello Mbean. The Mbean attribute defined in Hello is displayed. Change the value of the CacheSize property to 150. You can confirm that the property has been modified by starting the command line or terminal window of main. In the Mbean tree, expand

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.