Examples of JBoss Mbean

Source: Internet
Author: User
Tags getmessage jboss

Using the JDK's own JMX implementation to write the implementation of MBEAN,JMX not only Sun, JBoss also has its own JMX implementation. If you're using JBoss as a Web server, it's a good idea to write an mbean based on the JBoss implementation. Like our company is using JBoss, so all Mbean are based on JBoss to write. What is the difference between a JBoss-based Mbean and a sun-based Mbean? There are some differences, but most of them are the same.

First, HelloWorld instances

1. Preparatory work
JBoss implements the JMX specification, which is implemented based on JBoss. Please go to download a jboss, I am JBoss-3.2.6, this instance requires JBoss two jar package support: Jboss-system-3.2.6.jar, Jboss-jmx-3.2.6.jar, if you are developing with eclipse like Me (recommended), then add this two package to the library reference of the project (the method referenced in the library reference is referenced in chapter two above).

2. Program code
Suppose we have a property called message that needs to be configured frequently, then we'll write it as an mbean.

1), Helloworldservicembean interface
Before writing an mbean, we need to write an Mbean interface, which is the Set/get method of the attribute. This interface must inherit the interface Servicembean.
Java code

Import Org.jboss.system.ServiceMBean;           Public interface Helloworldservicembean extends Servicembean {String getMessage ();       void Setmessage (String message); }

2), HelloWorldService implementation class
Then write out the implementation class HelloWorldService of the Helloworldservicembean interface, which must also inherit the Servicembeansupport class. This kind of class is simple, is the attribute and the corresponding Set/get method, EJB called entity class, Hibernate called Pojo.
Java code Import Org.jboss.system.ServiceMBeanSupport; public class HelloWorldService extends Servicembeansupport

implements helloworldservicembean {             private string message;            public  String getmessage ()  {                 system.out.println ("getMessage () ="  + message);                 return message;             }                             public void setmessage (string message)  {               system.out.println ("Setmessage ("  +  message +  ")";                This.message = message;          }       }      

3), configuration file jboss-service.xml XML version= "1.0" encoding= "UTF-8"?> <server> <mbean code= "Example.mbean.HelloWorldService"

Name= "Www.chengang.com.cn:service=HelloWorld"> <attribute name= "Message"> Hello Worldattribute> mbean> server>

Description
The code entry points to the implementation class of the Mbean HelloWorldService
The name entry is a named format that is typically: [Descriptive text]:service=[class name]
attribute is to set the initial value for the property so that when JBoss loads the HelloWorldService class, the message property has an initial value of Hello World. Note The first letter of a message must be uppercase.

Second, deploy the instance to JBoss

Create a Hello.sar directory under the Jboss-3.2.6\server\default\deploy directory, and then create the following directory file structure:
Hello.sar
|----Example
| |----Mbean
| |----Helloworldservice.class (NOTE: *.class, not *.java)
| |----Helloworldservicembean.class
|----Meta-inf
|----Jboss-service.xml

Other Notes:
You can also compress the Hello.sar directory into a Hello.sar file with the same name in the ZIP format and place it in the Jboss-3.2.6\server\default\deploy directory.
JBoss supports hot deployment, meaning that you do not need to restart JBoss when you decorate this directory.

third, the effect of Mbean

Click on the "Service=helloworld" key to open the detail page

Change "HelloWorld" to "Hello World,chengang" and click "Apply Changes" to apply the modification

Iv. How other classes use the Messag property

Now that we can set the message property with a Web page that is automatically provided, the next question is: "How to get the property value of message in other classes". Mbean in JBoss is an instance of only one Mbean reserved (singleton mode. ), which means that the problem turns into how we get to the only instance. The routines are as follows:

1. Create a class that uses the message property   import org.jboss.mx.util.MBeanProxyExt;        import org.jboss.mx.util.objectnamefactory;       public class client  {           public void go ()  {                helloworldservicembean mbean =   (Helloworldservicembean)  mbeanproxyext.create (helloworldservicembean.class,  Objectnamefactory.create (Www.chengang.com.cn:service=HelloWorld));                string msg = mbean.getmessage ();               system.out.println ("client.go () ="  + msg);            }       }     


Note: There are three sentences in the Go method. The first sentence is long, which is to obtain an instance of this Mbean in JBoss based on the Mbean name set in the Jboss-service.xml file.

2. Add a corresponding method to call Client.go in the Mbean
Add a sentence to the Helloworldservicembean interface:

void Callgo ();       ublic void Callgo () {new Client (). Go (); }

3. Update the Deployment Department
Update the class files of three classes: Clien.class, Helloworldservicembean.class, Helloworldservice.class, to JBoss hello.sar\example\ The Mbean directory. Then restart JBoss.

4. Viewing effects
Open the Mbean provisioning page from JBoss and find a callgo.
Click the Invoke button after the Callgo item to get the DOS output

v. Other instructions
This example demonstrates only one message property, and you can certainly add more attributes to the HelloWorldService. Don't forget to add the corresponding Set/get method to the Helloworldservicembean interface.
The message property of this instance is of type string, but JMX also supports other types, even XML information. For XML information in Jboss-service.xml, the property type requirement is ORG.W3C.DOM.ELEMENT,JMX to encapsulate it as an XML DOM object.
In retrospect, JMX also writes configuration information in a file (Jboss-service.xml file), and it seems to be nothing new, relative to how the configuration file is written to the *.properties file. But with this chapter we can see some of the benefits of JMX: We don't have to write code to read the configuration file, and the types of attributes that JMX supports are various (as mentioned above Org.w3c.dom.Element). What's more, JMX also provides a set of properties that are previously accessed and called each other, a HelloWorld instance that reflects only the tip of the iceberg.

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.