MBean written using JDK's built-in JMX implementation. The implementation of JMX is not unique to SUN, and JBoss also has its own JMX implementation. If you use JBoss as a WEB server, writing mbeans Based on the JBoss implementation is a good choice. As our company uses JBoss, all mbeans are written based on JBoss. What is the difference between JBoss-based MBean and SUN-based MBean? There are some differences, but most of them are the same.
1. HelloWorld instance
1. Preparations
JBOSS implements the JMX specification. This instance is implemented based on JBoss. Please first download a JBoss, I am a JBoss-3.2.6, this instance needs the support of JBoss's two JAR packages: jboss-system-3.2.6.jar, jboss-jmx-3.2.6.jar, if you are the same with Eclipse for Development (recommended ), add the two packages to the library reference of the project (refer to the previous two chapters for the method of adding to the library reference ).
2. program code
Suppose we have a message attribute that requires frequent configuration changes, so we will write it as an MBean.
1) HelloWorldServiceMBean Interface
Before writing MBean, We need to write an MBean interface. All methods in the interface are set/get methods of attributes. This interface must inherit the ServiceMBean interface.
Java code
- Import org. jboss. system. ServiceMBean;
- Public interface HelloWorldServiceMBean extends ServiceMBean {
- String getMessage ();
- Void setMessage (String message );
- }
2) HelloWorldService implementation class
Then write the implementation class HelloWorldService of the HelloWorldServiceMBean interface, which must inherit the ServiceMBeanSupport class. This type is no longer simple, that is, the property and the corresponding set/get method. In EJB, it is called the object class, and in Hibernate it is called the 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;
- }
}
- XmlVersion = "1.0" encoding = "UTF-8"?>
- <Server>
- <MbeanCode = "example. mbean. HelloWorldService"
Name = "www.chengang.com.cn: service = HelloWorld">
- <AttributeName = "Message">Hello WorldAttribute>
- Mbean>
Server>
Note:
◆ Code points to MBean implementation class HelloWorldService
◆ Name is a name in the format of [descriptive text]: service = [class name]
◆ Attribute is the initial value for the attribute. As a result, when JBOSS loads the HelloWorldService class, the message attribute has an initial value "Hello World. Note that the first letter of the Message must be in uppercase.
2. 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: it is *. class, not *. java)
| ---- HelloWorldServiceMBean. class
| ---- META-INF
| ---- Jboss-service.xml
Other Instructions:
◆ The hello. sar directory can also be compressed into a hello. sar file with the same name in zip format, put in the jboss-3.2.6 \ server \ default \ deploy directory.
◆ JBOSS supports hot deployment, which means you do not need to restart JBOSS when setting this directory.
Iii. MBean Effect
Click "service = HelloWorld" to open the details page.
Change "HelloWorld" to "Hello World, ChenGang", and then click "Apply Changes" to modify
4. How to Use the mescript attribute for other classes
Now we can set the Message attribute through an automatically provided WEB page. The following question is: "How to get the Message attribute value in other classes ". MBean retains only one MBean instance in JBoss (Singleton mode ?), That is to say, the problem turns to how we get this unique instance. The routine is as follows:
1. Create a class that uses the Message attribute
- 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 );
- }
}
- Void callGo ();
- Ublic void callGo (){
- New Client (). go ();
- }