1.标准MBean:
实现了AAAMBean或AAAMXBean接口的普通类,AAA为该类的类名。
public interface StandardMXBean {
public String getName();
public String getCity();
public int getRank();
}
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;
import javax.management.Attribute;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
public class Standard implements StandardMXBean {
private Map<String,Attribute> attrs;
public Standard(String name, String city, int rank) {
super();
attrs = new HashMap<String,Attribute>();
attrs.put("name", new Attribute("name",name));
attrs.put("city", new Attribute("city",city));
attrs.put("rank", new Attribute("rank",rank));
}
public void start() {
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = getMBeanName("my_service", "my_name");
try {
mbs.registerMBean(this, name);
} catch (InstanceAlreadyExistsException ie) {
ie.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Standard test = new Standard("马刺","圣安东尼奥",2);
test.start();
int time = 0;
while(true)
try {
System.out.println(++time+"秒... ...");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static private ObjectName getMBeanName(String serviceName, String nameName) {
ObjectName name = null;
String nameStr = "MBeanTest:service="+ serviceName +",name="+ nameName;
try {
name = new ObjectName(nameStr);
} catch (MalformedObjectNameException e) {
System.err.println("Error creating MBean object name: "+ nameStr);
}
return name;
}
@Override
public String getName() {
return attrs.get("name").getValue().toString();
}
@Override
public String getCity() {
// TODO Auto-generated method stub
return attrs.get("city").getValue().toString();
}
@Override
public int getRank() {
// TODO Auto-generated method stub
return (int)attrs.get("rank").getValue();
}
}
2.动态MBean:
实现DynamicMBean接口,方法getAttribute(),getAttributes(),getMBeanInfo()必须返回正确的内容。
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
public class MBeanTest implements DynamicMBean{
private Map<String,Attribute> attrs;
public MBeanTest(String name, String city, int rank) {
super();
attrs = new HashMap<String,Attribute>();
attrs.put("name", new Attribute("name",name));
attrs.put("city", new Attribute("city",city));
attrs.put("rank", new Attribute("rank",rank));
}
public void start() {
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = getMBeanName("my_service", "my_name");
try {
mbs.registerMBean(this, name);
} catch (InstanceAlreadyExistsException ie) {
ie.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MBeanTest test = new MBeanTest("马刺","圣安东尼奥",2);
test.start();
int time = 0;
while(true)
try {
System.out.println(++time+"秒... ...");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static private ObjectName getMBeanName(String serviceName, String nameName) {
ObjectName name = null;
String nameStr = "MBeanTest:service="+ serviceName +",name="+ nameName;
try {
name = new ObjectName(nameStr);
} catch (MalformedObjectNameException e) {
System.err.println("Error creating MBean object name: "+ nameStr);
}
return name;
}
@Override
public Object getAttribute(String attribute)
throws AttributeNotFoundException, MBeanException,
ReflectionException {
return attrs.get(attribute);
}
@Override
public void setAttribute(Attribute attribute)
throws AttributeNotFoundException, InvalidAttributeValueException,
MBeanException, ReflectionException {
// TODO Auto-generated method stub
}
@Override
public AttributeList getAttributes(String[] attributes) {
AttributeList attrList = new AttributeList();
for(String key : attrs.keySet()) {
attrList.add(attrs.get(key));
}
return attrList;
}
@Override
public AttributeList setAttributes(AttributeList attributes) {
return null;
}
@Override
public Object invoke(String actionName, Object[] params, String[] signature)
throws MBeanException, ReflectionException {
return null;
}
@Override
public MBeanInfo getMBeanInfo() {
MBeanAttributeInfo[] mbinfo = new MBeanAttributeInfo[attrs.size()];
int i = 0;
for(String key : attrs.keySet()) {
mbinfo[i ++ ] = new MBeanAttributeInfo(key, "some_type", "some_desc", true, false, false);
}
return new MBeanInfo("some_name", "some_description", mbinfo, null, null, null);
}
}