通过Apache .commons-modeler开发相关的JMX

                     通过Apache 的.commons-modeler组件和JMX 中MBean的描述,是JMX不必实现相关的XxxxMBean的实现,通过反射和MBean描述动态产生相关的MBean的对象和接口。

 

1.JMX的服务对象

 

package com.easyway.apache.moduler.jmx;
/**
 * 备注此处的JMX没有实现相关的XxxMBean的接口
 * @author longgangbai
 *
 */
public class EasywayService implements IEasywayService {

	private String serviceName;

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	/* (non-Javadoc)
	 * @see com.easyway.apache.moduler.jmx.IEasywayService#printHello()
	 */
	public void printHello() {
		System.out.println("Hello World, " + serviceName);
	}

	/* (non-Javadoc)
	 * @see com.easyway.apache.moduler.jmx.IEasywayService#printHello(java.lang.String)
	 */
	public void printHello(String whoName) {
		System.out.println("Hello , " + serviceName);
	}

}

 2.针对JMX的MBean的描述符文件如下:

<?xml version="1.0"?>
<mbeans-descriptors>
 <!-- 
   对<mbean>标签做一下说明:
         name mbean在xml中的唯一标识,不一定要和类同名 
         description mbean的注释说明信息 
         domain mbean所属域 
         group mbean所属组 
         type mbean的类全名(包名+类名) 
         classname 指定实现代理功能的ModelMbean的全名,如果不指定则默认为BaseModelMBean 
         
         
         备注:在这个文件中可以配置对各JMXbean对象
  -->
 <mbean name="EasywayService" description="the EasywayService bean" domain="easyway" group="helloGroup" type="com.easyway.apache.moduler.jmx.EasywayService">
  <attribute name="serviceName" description="a name attribute" type="java.lang.String" writeable="true"/>
  <operation name="printHello" description="a operation to print hello" impact="INFO" returnType="String"/>
 </mbean>
</mbeans-descriptors> 

 

 

3.针对JMX的Agent代理如下:

package com.easyway.apache.moduler.jmx;
import java.io.InputStream;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.modelmbean.ModelMBean;
import org.apache.commons.modeler.ManagedBean;
import org.apache.commons.modeler.Registry;
import com.sun.jdmk.comm.HtmlAdaptorServer;
/**
 * JMX代理测试的应用
 * @author longgangbai
 *
 */
public class EasywayServiceBeanAgent {
	

	    public static void main(String[] args) throws Exception {
	        //基于xml中的信息构建一个Registry
	        Registry registry = Registry.getRegistry(null, null);
	        InputStream stream = EasywayServiceBeanAgent.class.getResourceAsStream("/Mbeans-descriptors.xml");
	        registry.loadMetadata(stream);
	        stream.close();
	        
	        
	        //由Registry得到一个MBeanServer
	        MBeanServer server = registry.getMBeanServer();

	        //得到Hello在描述文件中的信息类,对应于xml文件<mbean>标签的name属性。
	        ManagedBean managed = registry.findManagedBean("EasywayService");
	        
	        //创建ObjectName
	        ObjectName helloName = new ObjectName(managed.getDomain() + ":name=EasywayService");
	        
	        //得到ModelMBean 动态生成相应的方法的对应的XxxxMBean的接口
	        ModelMBean hello = managed.createMBean(new EasywayService());
	        
	        //注册MBean
	        server.registerMBean(hello, helloName);

	        ObjectName adapterName = new ObjectName("EasywayServiceBeanAgent:name=htmladapter,port=8082");
	        HtmlAdaptorServer adapter = new HtmlAdaptorServer();
	        
	        server.registerMBean(adapter, adapterName);
	        
	        adapter.start();
	        
	        System.out.println("start.....");
	    }
	}

 

你可能感兴趣的:(apache,xml,bean,sun)