MBean是JMX API的一个基础概念,在JMX的规范里,一个MBean就是一个被管理的JAVA 对象,它可以代表任何需要被管理的设备,应用或资源,它暴露以下接口:
- 可读写的属性
- 可调用的操作
- 自我描述
此外MBean还可以释放通知。
下面我以一个部署在JBOSS上的MBean例子来简单介绍下这方面的知识.
环境和工具如下:
JRE: 1.6.0_07 http://www.java.com/en/download/manual.jsp
开发工具: Eclipse v3.5.0 http://download.eclipse.org/eclipse/downloads/
编译部署: ANT v1.7.1 http://ant.apache.org/bindownload.cgi
EJB服务器: jboss-4.2.3.GA http://www.jboss.com/
(1) 在EClipse创建一个新项目MBeanHello
(2) 创建HelloMBean接口-HelloMBean.java
package com.example;
public interface HelloMBean {
public void sayHello();
public String getName();
public int getCacheSize();
public void setCacheSize(int size);
}
(3)创建MBean类-Hello.java
package com.example;
import javax.management.*;
import org.jboss.system.ServiceMBeanSupport;
public class Hello
extends ServiceMBeanSupport implements HelloMBean {
public void sayHello() {
System.out.println("hello, world");
}
public String getName() {
return this.name;
public int getCacheSize() {
return this.cacheSize;
}
public synchronized void setCacheSize(int size) {
int oldSize = this.cacheSize;
this.cacheSize = size;
}
System.out.println("Cache size now " + this.cacheSize);
private final String name = "MBean is my name";
private int cacheSize = DEFAULT_CACHE_SIZE;
private static final int DEFAULT_CACHE_SIZE = 200;
private long sequenceNumber = 1;
}
(4)创建jboss 的MBean的部署文件-MBeanHello-service.xml
注意该文件必须是*-service.xml这样名称命名,部署时需要放在[joss安装目录]/default/deploy目录下
<?xml version="1.0" encoding="UTF-8"?>
<server>
<classpath codebase="lib" archives="MBeanHello.jar"/>
<mbean code="com.example.Hello"
name="HelloMBean:Service=HelloMBean" xmbean-dd="MBeanHello-xmbean.xml">
</mbean>
</server>
注意lib表示是[joss安装目录]/default/deploy/lib目录,MBeanHello.jar是class文件的打包文件,code="com.example.Hello"指的是MBean的类,MBeanHello-xmbean.xml指的是mbean类的描述文件
(5)创建mbean类的描述文件-MBeanHello-xmbean.xml
注意该文件部署在[joss安装目录]/default/conf目录
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mbean PUBLIC "-//JBoss//DTD JBOSS XMBEAN 1.1//EN" "http://www.jboss.org/j2ee/dtd/jboss_xmbean_1_1.dtd">
<mbean>
<description>
</description>
<descriptors>
<persistence persistPolicy="OnUpdate"/>
<persistence-manager value="org.jboss.mx.persistence.DelegatingPersistenceManager"/>
</descriptors>
<class>com.example.HelloMBean</class>
<constructor>
<description>The default constructor</description>
<name>HelloMBean</name>
</constructor>
<!-- Attributes -->
&defaultAttributes;
<attribute access="read-write"
getMethod="getCacheSize"
setMethod="setCacheSize">
<description>Config CacheSize
</description>
<name>CacheSize</name>
<type>int</type>
<descriptors>
<value value="10" />
</descriptors>
</attribute>
<operation impact="ACTION">
<description>say Hello</description>
<name>sayHello</name>
<return-type>void</return-type>
</operation>
</mbean>
(6)创建ant的build.xml文件
关于该xml文件的一些设置还可以参考我的博文
http://blog.csdn.net/omage/archive/2010/01/14/5191568.aspx
http://blog.csdn.net/omage/archive/2010/01/17/5203991.aspx
<?xml version="1.0"?>
<!-- ======================================================================= -->
<!-- JBoss build file -->
<!-- ======================================================================= -->
<project name="JBoss" default="MBeanjar" basedir=".">
<property environment="env"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="jboss.home" value="${env.JBOSS_HOME}"/>
<property name="jboss.server.config" value="default"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
<property name="build.src.dir" value="${build.dir}/src"/>
<property name="build.artifact" value="MBeanHello.jar"/>
<property name="jboss-system.jar" value="${jboss.home}/lib/jboss-system.jar"/>
<!-- Build classpath -->
<path id="classpath">
<!-- So that we can get jndi.properties for InitialContext -->
<pathelement location="${basedir}"/>
<!-- Only the jbossall-client.jar should ideally be sufficient -->
<fileset dir="${jboss.home}/client">
<include name="**/jbossall-client.jar"/>
</fileset>
<pathelement location="${jboss-system.jar}"/>
<!-- javax.persistence.* -->
<fileset dir="${jboss.home}/server/default/lib">
<include name="ejb3-persistence.jar"/>
<include name="jboss-ejb3x.jar" />
<include name="log4j.jar" />
</fileset>
<pathelement location="${build.classes.dir}"/>
</path>
<property name="build.classpath" refid="classpath"/>
<!-- Override with your XDoclet bundle dist location -->
<property name="xdoclet.home" value="E:/Program Files/java-sdk/xdoclet-1.2.3"/>
<property name="xdoclet.lib" value="${xdoclet.home}/lib"/>
<path id="xdoclet.path">
<fileset dir="${xdoclet.lib}">
<include name="*.jar"/>
</fileset>
<path refid="classpath"/>
</path>
<!-- =================================================================== -->
<!-- Prepares the build directory -->
<!-- =================================================================== -->
<target name="prepare">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes.dir}"/>
</target>
<!-- =================================================================== -->
<!-- Compiles the source code -->
<!-- =================================================================== -->
<target name="compile" depends="prepare">
<javac
destdir="${build.classes.dir}"
debug="on"
deprecation="on"
optimize="off"
includes="**">
<classpath refid="classpath"/>
<src path="${src.dir}" />
</javac>
</target>
<target name="MBeanjar" depends="compile">
<jar jarfile="build/${build.artifact}">
<fileset dir="${build.classes.dir}">
<include name="**/*.class"/>
<include name="**/*.*" />
</fileset>
</jar>
<copy file="build/${build.artifact}" todir="${jboss.home}/server/${jboss.server.config}/lib"/>
<copy file="${basedir}/MBeanHello-service.xml" todir="${jboss.home}/server/${jboss.server.config}/deploy"/>
<copy file="${basedir}/MBeanHello-xmbean.xml" todir="${jboss.home}/server/${jboss.server.config}/conf"/>
</target>
<!-- =================================================================== -->
<!-- Cleans up generated stuff -->
<!-- =================================================================== -->
<target name="clean.db">
<delete dir="${jboss.home}/server/${jboss.server.config}/data/hypersonic"/>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
<delete file="${jboss.home}/server/${jboss.server.config}/deploy/${build.artifact}"/>
</target>
</project>
注意jboss-system.jar文件是包含org.jboss.system.ServiceMBeanSupport类的库
(7)ant编译,部署
这会将MBean的jar包,部署文件都拷贝到jboss的目录下。
(8)运行jboss
(9)在浏览器中输入http://localhost:8080
点击JMX Console后进入管理页面,可以看到
HelloMBean
点击service=HelloMBean, 在弹出的页面上可以试试改下CacheSize, 还可以调用sayHello,这些都可以在jboss的运行窗口看到输出。
参考:
http://java.sun.com/docs/books/tutorial/jmx/mbeans/index.html
http://blog.csdn.net/omage/archive/2010/01/14/5191568.aspx
http://blog.csdn.net/omage/archive/2010/01/17/5203991.aspx
C:\software\jboss-5.0.1.GA\jboss-5.0.1.GA\bin>twiddle.bat -s localhost invoke He
lloMBean:Service=HelloMBean sayHello
不写类面熟文件也可以,但是必须将部署文件中的依赖去掉<mbean code="com.example.Hello"
name="HelloMBean:Service=HelloMBean" >
</mbean>