`
arlord
  • 浏览: 5076 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

MX4J

 
阅读更多
由于时间关系,直接上demo

package com.arlord.demo;

import javax.management.Attribute;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import mx4j.tools.adaptor.http.HttpAdaptor;
import mx4j.tools.adaptor.http.XSLTProcessor;

public class MBeanAttributeInfo_Register {

public static void main(String[] args) throws Exception{
//这里使用的JMX的MBeanServerFactory直接返回一个MBeanServer的接口
MBeanServer server = MBeanServerFactory.createMBeanServer();
//为MBean创建对象名
ObjectName serverName = new ObjectName("http:name=HttpAdaptor");
//为MBeanServer创建HTTP的适配器
HttpAdaptor adaptor = new HttpAdaptor();
//注册MBean
server.registerMBean(adaptor, serverName);
//为serverName的MBean注入属性
server.setAttribute(serverName, new Attribute("Port",2088));
server.setAttribute(serverName, new Attribute("Host", "localhost"));

//XSLTProcessor的MBean对象名
ObjectName processorName = new ObjectName("Http:name=XSLTProcessor");
//XSLTProcessor是通过XSLT转换传递文件的
XSLTProcessor processor = new XSLTProcessor();
//注册处理器MBean
server.registerMBean(processor, processorName);
//为处理器注入属性
server.setAttribute(serverName, new Attribute("ProcessorName",processorName));

//创建属于自己的MBean
ObjectName test = new ObjectName("bzMBean:name=BeanInfo");
//注册已经写好的MBean
server.registerMBean(new MBeanAttributeInfo_Test(), test);
//在Mean上调用start操作
server.invoke(serverName,"start",null,null);
}

}


package com.arlord.demo;

import java.lang.reflect.Method;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanOperationInfo;

import mx4j.AbstractDynamicMBean;

public class MBeanAttributeInfo_Test extends AbstractDynamicMBean{

private String name = "Brad Pitt";

private int age = 68;

private long totalM = 0L;

private long leftM = 0L;


public String sayHello(String words){
return "your friend said: " + words;
}

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;
}

//查看内存使用情况
public String getMemory(){
totalM = Runtime.getRuntime().totalMemory() / 1024 /1024;
long maxM = Runtime.getRuntime().maxMemory()/1024/1024;
leftM = Runtime.getRuntime().freeMemory()/1024/1024;
long usedM = totalM - leftM;
return "总内存: " + totalM + "  使用的内存: " + usedM;

}

//重写创建MBean属性信息的的方法,该方法是创建的MBean是直接展示到页面上的
@Override
protected MBeanAttributeInfo[] createMBeanAttributeInfo() {
// TODO Auto-generated method stub
totalM = Runtime.getRuntime().totalMemory() / 1024 /1024;
return new MBeanAttributeInfo[]{
   new MBeanAttributeInfo("Name", String.class.getName(), "使用者名称", true, true, false),
   new MBeanAttributeInfo("Age", int.class.getName(), "年龄", true, true, false),
   new MBeanAttributeInfo("Memory", String.class.getName(), "内存", true, true, false)};

}

//重写创建操作信息的的MBean方法,该方法用于页面控制后台的信息,页面可以invoke对应MBean里面的方法,做出相应操作
@Override
protected MBeanOperationInfo[] createMBeanOperationInfo() {
// TODO Auto-generated method stub

Method sayHello = null;
try {
sayHello = this.getClass().getMethod("sayHello", new Class[]{String.class});
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return new MBeanOperationInfo[]{new MBeanOperationInfo("打个招呼", sayHello)};
}


}

访问:http://localhost:2088

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics