JMX Learning Notes (i)-mbean

Source: Internet
Author: User
Tags visualvm jconsole

JMX is Java Management Extensions Java Management extensions
MBean is managed beans managed beans

An Mbean is a managed Java object, a bit like JavaBean, a device, an application, or any resource can be represented as a Mbean,mbean exposes an interface that can read or write to attributes in some objects. Usually an mbean needs to define an interface, ending with an mbean, for example: Echombean, formatted as Xxxmbean, which is a specification that must be obeyed. For example:

Java Code package com.haitao.jmx;           /** * JMX MBean Interface * * * @author haitao.tu * */public Interface Echombean {  Public       void print (String yourName); }

Package com.haitao.jmx;

/**
 * JMX MBean Interface
 * 
 * * @author haitao.tu * */public
Interface Echombean {

	public void print (String yourName);


Quite simply, in the Echombean interface, a print method is defined with a yourname string parameter, and only the interface seems to be useless, we implement this interface below

  Java Code package  com.haitao.jmx;      /**    *  Implements of jmx echombean    *     *  @author  haitao.tu    *    */   public   class  echo  Implements  echombean {           @Override         public   void  print (string yourname)  {           system.out.println ("hi "  +  yourname +  "!");                   }  

Package com.haitao.jmx;

/**
 * Implements of JMX echombean
 * 
 * @author haitao.tu * */Public
class Echo Implements Echombean {

	@Override public
	void print (String yourName) {
		System.out.println ("Hi" + YourName + "!");
	}
	
}


Echo realizes the Echombean interface, and it's simple. We just print Hi yourname!

According to the JMX definition, is the managed object, now we just define the object and not be managed, and then we let the instance object of this echo class be managed: Java codePackageCOM.HAITAO.JMX;ImportJava.lang.management.ManagementFactory;ImportJavax.management.MBeanServer;ImportJavax.management.ObjectName; /** * JMX App Demo * * @author haitao.tu * * PublicclassApp { PublicStaticvoidMain (string[] args)throwsException {//create Mbeanserver Mbeanserver MBS = Managementfactory.getplatformmbeanserver (); New Mbean objectname, which identifies the registered Mbean objectname name in Mbeanserver =NewObjectName ("Com.haitao.jmx:type=echo"); Create Mbean Echo Mbean =NewEcho ();                           Registers an Mbean in the mbeanserver, identified as ObjectName (Com.tenpay.jmx:type=echo) Mbs.registermbean (Mbean, name); In Mbeanserver, call the registered Echombean Print method Mbs.invoke (name, print).NewObject[] {"Haitao.tu"},New string[] {"Java.lang.String"});                         thread.sleep (Long.max_value);       &nbsp}      }  

Package com.haitao.jmx;

Import java.lang.management.ManagementFactory;

Import Javax.management.MBeanServer;
Import Javax.management.ObjectName;

/**
 * JMX app Demo
 * 
 * @author haitao.tu
/public class App {public
	
	static void Main (string[] Ar GS) throws Exception {
		//create Mbeanserver
		mbeanserver mbs = Managementfactory.getplatformmbeanserver ();
		
		Creates a new Mbean objectname that identifies the registered Mbean
		objectname name = new ObjectName ("Com.haitao.jmx:type=echo") in Mbeanserver;
		
		Create Mbean
		echo Mbean = new Echo ();
		
		Registers an Mbean in the mbeanserver, identified as ObjectName (Com.tenpay.jmx:type=echo)
		Mbs.registermbean (Mbean, name);

		
		In Mbeanserver, call the registered Echombean Print method
		mbs.invoke (name, print, new object[] {"Haitao.tu"}, new string[] {" Java.lang.String "});
		
		Thread.Sleep (Long.max_value);
	}



1. First, we applied a Mbeanserver object to Managementfactory in the App class.
2. Then we are going to have the instance object of Echo managed, and we need to give the object an identifier, which is objectname. Note the ObjectName constructor, which uses the form (Package name: Type= class name).
3. Then we registered echo through the Mbs.registermbean method and passed in the objectname to identify the Mbean in Mbeanserver.
4. We then invoked the registered Echo Print method via the Mbs.invoke method, finding the Mbean through objectname, and passing the last two parameters to the parameters executed by the Print method, and the type of the argument.
5. Finally we sleep the main thread, waiting for calls from other threads.

We can see from this example that the advantage of an mbean is that before the instance object of ECHO is managed, we can invoke the public method in the echo only through the handle of the Echo object, after being managed, We can invoke the Print method of the Echo object through the Mbeanserver handle MBS.

In more detail, we can view the Mbean via the JDK with tool jconsole or VISUALVM:

1.jconsole

Jconsole's position is in%java_home%/bin/jconsole.exe.

After opening, you will see:

Select Com.haitao.jmx.App, then click Connect and select Mbean to see

Here you can call the Echo Print method directly, and of course you'll see a lot of other table here, including overviews, memory, threads, classes, VM summaries, MBean.

2.VisualVM

VISUALVM's position is in%java_home%/bin/jvisualvm.exe.

After the connection is opened, we see a similar effect:

This article originally posts: http://tuhaitao.javaeye.com/blog/786391

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.