MBean部署到JBoss下运行

来源:互联网 发布:项目奖金 知乎 编辑:程序博客网 时间:2024/05/17 02:24

    看了好多的MBean例子,自己写了一个,可是却在部署上花了不少功夫。所以自己将MBean的部署写下来,希望以后不要再忘记!

下面是一个非常简单的MBean实例:

首先需要写一个接口:

package org.tuturial.mbean.hello;

public interface HelloWorldMBean{
 
 public void sayHello();
 public void startService();
 public void stopService();
 public String getName();
 public void setName(String name);
 public int getAge();
 public void setAge(int age) ;

}

注意:在JBoss上部署的MBean中,我们需要的属性,方法都是调用接口的。我们对外公布的是接口而不是实现接口的类。

下面是定义了该接口的实现类:

package org.tuturial.mbean.hello;

public class HelloWorld implements HelloWorldMBean {
 private String name;
 private int age;

 public HelloWorld() {

 }
 public HelloWorld(String str){
  this.name = "Hello World! I am a Standard MBean";
 }

 public void sayHello() {
  System.out.println("Hello World!");
 }

 public void startService() {
  System.out.println("start Service");
 }

 public void stopService() {
  System.out.println("Stop Service");

 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

}

在jboss-service.xml配置文件中需要配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<server>
 <mbean code="org.tuturial.mbean.hello.HelloWorld"
  name="org.tuturial.mbean.hello:service=HelloWorld">
  <depends>jboss:service=Naming</depends>
  <attribute name="Age">22</attribute>
  <attribute name="Name">Jone</attribute>
  <!--
   <attribute name="ThreadPoolSize">100</attribute>
   -->
 </mbean>
</server>

对应配置文件的解释,可以参看前面文章所描叙的MBean的配置。

使用ant进行编译:

<project basedir="../" default="build" name="CommonClass">
 <property environment="env" />
 <property name="ECLIPSE_HOME" value="../myeclipse" />
 <property name="debuglevel" value="source,lines,vars" />
 <property name="target" value="1.5" />
 <property name="source" value="1.5" />
 <path id="CommonClass.classpath">
        <pathelement location="bin"/>
        <pathelement location="../../../../../../../../software/jboss-4.2.2.GA/lib/commons-codec.jar"/>

     // ....这里是需要的包导进来

</path>

<target name="init">
  <mkdir dir="bin" />
  <copy includeemptydirs="false" todir="bin">
   <fileset dir="src" excludes="**/*.launch, **/*.java" />
  </copy>
 </target>

<target name="clean">
  <delete dir="bin" />
 </target>
 <target depends="build-project" name="build" />
 <target depends="init" name="build-project">
  <javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
   <src path="src" />
   <classpath refid="CommonClass.classpath" />
  </javac>
 </target>

<target name="ear_HelloWorldMbean" depends="build-project">
  <jar jarfile="bin/HelloWorldMBean.sar">
   <fileset dir="bin">
    <include name="org/tuturial/mbean/hello/*.class" />
   </fileset>
   <fileset dir="deploy/mbeanXml/HelloWorldMBean">
    <include name="META-INF/jboss-service.xml" />
   </fileset>
  </jar>
  <mkdir dir="bin/HelloWorldMBean" />
  <ear destfile="bin/HelloWorldMBean/HelloWorldMBean.ear" appxml="deploy/app_HelloWorldMBean.xml">
   <fileset dir="bin">
    <include name="HelloWorldMBean.sar" />
   </fileset>
  </ear>
 </target>

</project>

app_HelloWorldMBean.xml:配置

<?xml version="1.0" encoding="UTF-8"?>
<application>
 <module>
  <java>HelloWorldMBean.sar</java>
 </module>
</application>

配置文件中注意对大小写的敏感:MBean开发实例流程如下:

/**
 * MBean开发实例 <br>
 *
 * 1. 确定MBean接口
 *  写一个普通的接口, 在接口名字后面加上MBean标记(可能是MBean的规范这样约定的). <br>
 *
 * 2. 实现MBean接口
 *  实现类名字去掉接口的MBean后缀标记. <br>
 *
 * 3. 写jboss-service文件. <br>
 * 例:
 * <?xml version="1.0" encoding="UTF-8"?>
 * <server>
 *  <mbean code="com.regaltec.imon.collector.polling.mbean.IMonCollectorPolling"
 *     name="com.regaltec.imon.mbean:service=IMonCollectorPolling">
 *   <depends>jboss:service=Naming</depends>
 *   <attribute name="Collector_Id">imoncollector_1</attribute>
 *  </mbean>
 * </server>
 *
 * code项指向MBean的实现类. <br>
 * name项是一个名称,格式一般是:[说明性文字]:service=[类名]
 * attribute是为属性设置初始值,这样当JBOSS一加载MBean实现类时,属性就有了一个初始值. 注意属性名的第一个字母必须是大写。<br>
 *
 * 4. 目录文件结构<br>
 * hello.sar
 * |----example <br>
 * | +----mbean <br>
 *   | +----HelloWorldService.class(注意:是*.class,不是*.java) <br>
 *   | +----HelloWorldServiceMBean.class <br>
 * |----META-INF <br>
 * | +----jboss-service.xml <br>
 *
 * 附application.xml结构:<br>
 * <?xml version="1.0" encoding="UTF-8"?>
 * <application>
 * <display-name>IMonCollectorEar</display-name>
 * <description>IMonCollectorEar</description>
 *
 * <module> <java>lib</java> </module>
 *
 * <module> <java>IMonCollectorPolling.jar</java> </module>
 *
 * <module> <java>IMonCollectorPolling.sar</java> </module>
 *
 * <module>
 *  <web>
 *   <web-uri>IMonCollectorTest.war</web-uri>
 *   <context-root>IMonCollectorTest</context-root>
 *  </web>
 * </module>
 * </application>