JMX In Action summary (2) -- HelloWorld example

Source: Internet
Author: User

This article describes a simple HelloWorld example.
First, prepare the environment. I can use JDK1.6 and 1.5. You also need to go to oracle to download the jmx ri package. Address: Release 1.2 Reference Implementation ". After decompression, there will be two jar packages under the lib directory. jar can be added to CLASSPATH, which contains an Html adapter to be used later.
1. First, write a HelloWorld MBean, which consists of an interface and an implementation class. The Code is as follows:
Public interface HelloWorldMBean {
Public void setGreeting (String greeting );
Public String getGreeting ();
Public void printGreeting ();
}
Write implementation class HelloWorld:
Public class HelloWorld implements HelloWorldMBean {

Private String greeting = null;

Public HelloWorld (){
This. greeting = "Hello World! I am a Standard MBean ";
}

Public HelloWorld (String greeting ){
This. greeting = greeting;
}

Public void setGreeting (String greeting ){
This. greeting = greeting;
}

Public String getGreeting (){
Return greeting;
}

Public void printGreeting (){
System. out. println (greeting );
}

}
In this way, a HelloWorld MBean is complete, which is a standard MBean. The MBean must be registered with the Agent before it can be used. Next, write an Agent.

2. Define JMX Agent: HelloAgent. It has three tasks:
1) Create an MBean Server instance.
2) create an HTML adapter and an HTML client connection.
3) register a new HelloWorld MBean instance.
The Code is as follows:
Public class HelloAgent {

Private MBeanServer mbs = null;

Public HelloAgent (){
Mbs = MBeanServerFactory. createMBeanServer ("HelloAgent ");

HtmlAdaptorServer adapter = new HtmlAdaptorServer ();
HelloWorld hw = new HelloWorld ();
ObjectName adapterName = null;
ObjectName helloWorldName = null;
Try {
HelloWorldName = new ObjectName ("HelloAgent: name = helloWorld1 ");
Mbs. registerMBean (hw, helloWorldName );
AdapterName = new ObjectName ("HelloAgent: name = htmladapter, port = 9092 ");
Adapter. setPort (9092 );
Mbs. registerMBean (adapter, adapterName );
Adapter. start ();
} Catch (Exception e ){
E. printStackTrace ();
}
}

Public static void main (String args []) {
System. out. println ("HelloAgent is running ");
HelloAgent agent = new HelloAgent ();
}
}
This code first creates an MBean Server instance and an Html adapter instance. The MBean Server is created using a factory class. When the MBean Server is created, the input string is used as the domain name of the MBean Server. The domain name is the identifier of the MBean Server.
Next, instantiate HelloWorld MBean and register it in MBean Server. An ObjectName instance is used for registration. The ObjectName class provides a naming system for MBean in JMX, which is the unique identifier registered in MBean Server. It consists of two parts:
1) domain name: this domain name is usually the same as the domain name of the MBean Server. Otherwise, it means it is isolated from other mbeans.
2) zero or multiple key = value strings separated by commas (,). This string is used to differentiate MBean and provide information for MBean.
The next step is to register the Html Adapter. The Html adapter is also an MBean and start the adapter.

The above two steps are the code of our example. The basic structure is as follows:


3. Running example. The HelloAgent class has the main method and runs it directly. If it succeeds, "HelloAgent is running" appears ". Open the browser and enter: http: // localhost: 9092/, because the Html adapter port in the Code is set to 9092.

The above three steps complete the entire HelloWorld example and provide three types of pages through the browser:
1. The Agent page, that is, the first page to be viewed. An overview of MBean contained in the Agent above, it shows all mbeans registered in it, it is displayed through the ObjectName instance used during registration. You can use the top text box to filter mbeans to be displayed.
2. On the MBean page, click an MBean on the Agent Page To Go To The MBean page. Click the first MBean with name = helloWorld1 to display the following information:
A) ObjectName, HelloAgent: name = helloWorld1 provided during MBean Registration
B) class name. In this example, It is HelloWorld.
C) description. For Stand MBean, MBean Server is created.
D) attribute list, attribute list exposed by MBean. This MBean has a property Greeting which is a read/write attribute (RW, because getter and setter are available ), you can enter a string in the text box of the Value column and click Apply to dynamically set the Value of Greeting.
E) List of exposed operations. This MBean has an operation printGreeting. Click the printGreeting button to call this operation. The message "printGreeting Successful" is displayed, the value of the Greeting attribute you just entered is displayed on the console.
F) Reload Period indicates whether the MBean Server needs to re-instantiate the MBean. If so, how often.
G), Unregister button, anti-register this MBean.
In this example, an MBean is an Html adapter, because it is also registered as an MBean on the Agent.
3. On the Admin page, click the Admin button on the Agent Page to go to the Admin page. You can add or delete mbeans on this page. The following four text boxes are displayed:
A). Domain: displays the Domain of the current Agent,
B), Keys, that is, the attribute string of the ObjectName class.
C) Java Class, the complete Class Name of the MBean to be created.
D), Class Loader, this is optional, and others are required.
Under the Action option box, there is a Constructors option. If you select this option, click Send Request to display all Constructors of MBean. You can use one of them to create MBean instances.

MBean notification
Add the notification code to the MBean of HelloWorld. JMX provides two methods to enable MBean to be the listening object of other mbeans. The first method is to implement the javax. management. icationicationbroadcaster interface, and the second method inherits the javax. management. icationicationbroadcastersupport class.
The benefit of implementing interfaces is that you can inherit other classes. The benefit of inheriting classes is that you do not need to write method code that implements interfaces. We chose to use the inherited class method. The HelloWorld class code is modified as follows:
Public class HelloWorld extends NotificationBroadcasterSupport implements HelloWorldMBean {
Public HelloWorld (){
This. greeting = "Hello World! I

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.