一个基于JBOSS平台的MBean例子

   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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的EJB跨机器访问的例子: 1. 创建EJB接口 ```java import javax.ejb.Remote; @Remote public interface GreetingService { String sayHello(String name); } ``` 2. 创建EJB实现类 ```java import javax.ejb.Stateless; @Stateless public class GreetingServiceImpl implements GreetingService { public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 3. 在服务器端配置EJB 在服务器的EJB容器中配置EJB,例如,在Wildfly中,可以将EJB部署为WAR包,并在WEB-INF目录下创建一个名为jboss-ejb3.xml的文件,配置EJB的JNDI名称和访问地址,例如: ```xml <?xml version="1.0" encoding="UTF-8"?> <jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/schema/jbossas/jboss-ejb3-2_0.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_2.xsd" version="3.2"> <enterprise-beans> <session> <ejb-name>GreetingService</ejb-name> <ejb-class>com.example.GreetingServiceImpl</ejb-class> <session-type>Stateless</session-type> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>GreetingService</ejb-name> <method-name>*</method-name> </method> </container-transaction> </assembly-descriptor> </jboss:ejb-jar> ``` 4. 在客户端访问EJB 在客户端,使用JNDI API查找远程EJB,并调用它的方法: ```java import javax.naming.InitialContext; import com.example.GreetingService; public class Client { public static void main(String[] args) throws Exception { InitialContext ctx = new InitialContext(); GreetingService service = (GreetingService) ctx.lookup("ejb:/myapp/GreetingService!com.example.GreetingService"); String message = service.sayHello("World"); System.out.println(message); } } ``` 其中,"ejb:/myapp/GreetingService!com.example.GreetingService"是EJB的JNDI名称和接口类型,myapp是服务器上部署EJB的应用程序名称,可以根据具体情况进行修改。 注意,客户端需要在classpath中添加EJB接口的实现类和依赖库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值