A word about JMX

Source: Internet
Author: User

What?

The Java Management Extensions (JMX) API is a standard API for Management and monitoring of resources such as applications , devices, services, and the Java virtual machine.
Bottom line: The canonical API for managing system resources.

Where to use?

Ypical uses of the JMX technology include:

    • Consulting and changing application configuration
    • Accumulating statistics about application behavior and making them available
    • Notifying of state changes and erroneous conditions.

A sentence typical application: Change, view program configuration, collect computational program behavior data, notification, alarm.

Import notion

An MBean was a managed Java object, similar to a JAVABEANTM, which follows the design patterns set forth in the INS Trumentation level of the JMX specification.
(4 kinds MBean)
-Standard MBeans
-Dynamic MBeans
-Open MBeans
-Model MBeans.

MBeans expose a management interface:a set of readable and/or writable attributes and a set of invokable operations, Alon G with a self-description. The management interface does not a change throughout the life of an MBean instance.

In a word, Mbean is the core of JMX, and Mbean is similar to JavaBean, where we obtain various information about the program detected by Mbean through an mbean-burst interface.

Standard MBean

A standard MBean are defined by writing a Java interface called Somethingmbean and a Java class called Something that imple ments that interface.
A standard MBean are composed of the MBean interface which lists the methods for all exposed attributes and operations, and The class which implements this interface and provides the functionality of the instrumented resource.

In a word, standard MBean is an implementation of the interface: ***mbean implementation class (such as Timermbean, to see the API Docs), here to burst out the method of obtaining information, in order to activities you want to the various information.

Example:

/ * Hellombean.java-mbean interface describing the management operations and attributes for the Hello World MBean. In this case there is operations, "SayHello" and "add", and the attributes, "Name" and "CacheSize". */Package Com.example.mbeans; Public InterfaceHellombean {//Operations     Public void SayHello(); Public int Add(intXinty);//Attributes    //A read-only attribute called Name of type String     PublicStringGetName();//A read-write attribute called CacheSize of type int     Public int getcachesize(); Public void SetCacheSize(intsize);}
/ * Hello.java-mbean implementation for the Hello world MBean. This class must implement all the Java methods declared in the Hellombean interface, with the appropriate behavior for E  Ach one. */ PackageCom.example.mbeans; Public  class Hello implements Hellombean {     Public void SayHello() {System.out.println ("Hello, World"); } Public int Add(intXintY) {returnx + y; }/ * Getter for the Name attribute.  The pattern shown here is Frequent:the Getter returns a private field representing the attribute value. In we case, the attribute value never changes, and for other attributes it might change as the application Ru  Ns.  Consider an attribute representing statistics such as uptime or memory usage, for example.  Being read-only just means that it can ' t be changed through the management interface. */     PublicStringGetName() {return  This. Name; }/ * Getter for the CacheSize attribute. The pattern shown here is Frequent:the Getter returns a private field representing the attribute value, and t  He setter changes that field. */     Public int getcachesize() {return  This. cacheSize; }/ * Setter for the CacheSize attribute. To avoid problems with stale values in multithreaded situations, it's a good idea for setters to be Synchroni  Zed. */     Public synchronized void SetCacheSize(intSize) { This. cacheSize = size;/ * In a real application, changing the attribute would typically has effects beyond just modifying the Cachesiz  E field.  For example, resizing the cache might mean discarding entries or allocating new ones.  The logic for these effects would is here. */System.out.println ("Cache size Now"+ This. cacheSize); }Private FinalString name ="Reginald";Private intCacheSize = default_cache_size;Private Static Final intDefault_cache_size = $;}
/ * Main.java-main class for Hello World example.  Create the HelloWorld MBean, register it, then wait for forever (or until the program is interrupted). */ PackageCom.example.mbeans;Importjava.lang.management.*;Importjavax.management.*; Public  class Main {    / * For simplicity, we declare "throws Exception".  Real programs would usually want finer-grained exception handling. */     Public Static void Main(string[] args)throwsException {//Get the Platform MBean Server attention!Mbeanserver mbs = Managementfactory.getplatformmbeanserver ();//Construct the ObjectName for the MBean we'll registerObjectName name =NewObjectName ("Com.example.mbeans:type=hello");//Create the Hello world MBeanHello Mbean =NewHello ();//Register the Hello world MBean attention!Mbs.registermbean (Mbean, name);//Wait ForeverSystem.out.println ("Waiting forever ...");    Thread.Sleep (Long.max_value); }}

According to the JMX specification, a MBean interface consists of named and typed attributes that is readable and possib Ly writable, and named and typed operations that can is invoked by the applications that is managed by the MBean.

* * Sentence:
The interface is defined in **mbean, and this MBean interface has to specify the names of attributes (such as name, CacheSize), type, and burst out to get or set their methods as well as some operations. As an example, add () and SayHello ().
The core component of using a JMX agent to manage The MBEAN:JMX agent is the MBean server, which is registered before administration.

Mbeanserver mbs = Managementfactory.getplatformmbeanserver ();
Get the Mbean running on platform by calling the Java.lang.management.ManagementFactory.getPlatformMBeanServer () method Server. If platform does not, it will automatically get one (via Mbeanserverfactory.creatembeanserver ()).

Send Message

MBeans can generate notifications, for example to signal a state change, a detected event, or a problem.

don't forget, Mbean can monitor an event, issue, status, and therefore send a message notification .

Cond.......

A word about JMX

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.