参考文章:
一、StandardMBean
定义接口
public interface HelloMBean {
public String getName();
public void setName(String name);
public String printHello();
public String printHello(String whoName);
}
创建实现类
public class Hello implements HelloMBean {
private String name;
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String printHello() {
return "Hello " + name;
}
@Override
public String printHello(String whoName) {
return "Hello " + whoName;
}
}
创建代理、JMX Server
public class HelloAgent {
public static void main(String[] args) throws Exception {
//create mbean server
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
//create object name
ObjectName objectName = new ObjectName("jmxBean:name=hello");
//create mbean and register mbean
server.registerMBean(new Hello(), objectName);
/**
* JMXConnectorServer service
*/
//这句话非常重要,不能缺少!注册一个端口,绑定url后,客户端就可以使用rmi通过url方式来连接JMXConnectorServer
Registry registry = LocateRegistry.createRegistry(1099);
/**
构造JMXServiceURL
完整为:service:jmx:rmi://localhost:0/jndi/rmi://localhost:1099/jmxrmi
service:jmx: - 固定开头
rmi: - 这个是jmx connector server的传输协议,在这个url中是使用rmi来进行传输的
localhost:0 - 这个是jmx connector server的IP和端口,也就是真正提供服务的host和端口,可以忽略,那么会在运行期间随意绑定一个端口提供服务
jndi/rmi://localhost:1099/jmxrmi - 这个是jmx connector server的路径,具体含义取决于前面的传输协议。比如该URL中这串字符串就代表着该jmx connector server的stub是使用 jndi api 绑定在 rmi://localhost:1099/jmxrmi 这个地址
*/
JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
//创建JMXConnectorServer
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, server);
//启动
cs.start();
}
}
通过RMI方式连接JMX Server,获取jmx connector
public class JMXClient {
public static void main(String[] args) throws Exception {
//connect JMX
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
ObjectName mbeanName = new ObjectName("jmxBean:name=hello");
//print domains
System.out.println("Domains:---------------");
String domains[] = mbsc.getDomains();
for (int i = 0; i < domains.length; i++) {
System.out.println("Domain[" + i + "] = " + domains[i]);
}
System.out.println();
//MBean count
System.out.println("MBean count:---------------");
System.out.println("MBean count = " + mbsc.getMBeanCount());
System.out.println();
//process attribute
System.out.println("process attribute:---------------");
mbsc.setAttribute(mbeanName, new Attribute("Name", "newName")); //set value
System.out.println("Name = " + mbsc.getAttribute(mbeanName, "Name")); //get value
System.out.println();
//invoke via proxy
System.out.println("invoke via proxy:---------------");
HelloMBean proxy = MBeanServerInvocationHandler.newProxyInstance(mbsc, mbeanName, HelloMBean.class, false);
System.out.println(proxy.printHello());
System.out.println(proxy.printHello("zhangsan"));
System.out.println();
//invoke via rmi
System.out.println("invoke via rmi:---------------");
System.out.println(mbsc.invoke(mbeanName, "printHello", null, null));
System.out.println(mbsc.invoke(mbeanName, "printHello", new Object[]{"lisi"}, new String[]{String.class.getName()}));
System.out.println();
//get mbean information
System.out.println("get mbean information:---------------");
MBeanInfo info = mbsc.getMBeanInfo(mbeanName);
System.out.println("Hello Class:" + info.getClassName());
System.out.println("Hello Attribute:" + info.getAttributes()[0].getName());
System.out.println("Hello Operation:" + info.getOperations()[0].getName());
System.out.println();
//ObjectName of MBean
System.out.println("ObjectName of MBean:---------------");
Set set = mbsc.queryMBeans(null, null);
for (Iterator it = set.iterator(); it.hasNext(); ) {
ObjectInstance oi = (ObjectInstance) it.next();
System.out.println(oi.getObjectName());
}
jmxc.close();
}
}
二、DynamicMBean
创建MBean类
public class HelloDynamic implements DynamicMBean {
//attributes
private String name;
private MBeanInfo mBeanInfo = null;
private String className;
private String description;
private MBeanAttributeInfo[] attributes;
private MBeanConstructorInfo[] constructors;
private MBeanOperationInfo[] operations;
MBeanNotificationInfo[] mBeanNotificationInfoArray;
public HelloDynamic() {
init();
buildDynamicMBean();
}
private void init() {
className = this.getClass().getName();
description = "Simple implementation of a MBean.";
//initial attributes
attributes = new MBeanAttributeInfo[1];
//initial constructors
constructors = new MBeanConstructorInfo[1];
//initial method
operations = new MBeanOperationInfo[1];
mBeanNotificationInfoArray = new MBeanNotificationInfo[0];
}
private void buildDynamicMBean() {
//create constructor
Constructor[] thisconstructors = this.getClass().getConstructors();
constructors[0] = new MBeanConstructorInfo("HelloDynamic(): Constructs a HelloDynamic object", thisconstructors[0]);
//create attribute
attributes[0] = new MBeanAttributeInfo("Name", "java.lang.String", "Name: name string.", true, true, false);
//create operate method
MBeanParameterInfo[] params = null;//no parameter
operations[0] = new MBeanOperationInfo("print", "print(): print the name", params, "void", MBeanOperationInfo.INFO);
//create mbean
mBeanInfo = new MBeanInfo(className, description, attributes, constructors, operations, mBeanNotificationInfoArray);
}
//dynamically add a print1 method
private void dynamicAddOperation() {
init();
operations = new MBeanOperationInfo[2];
buildDynamicMBean();
operations[1] = new MBeanOperationInfo("print1", "print1(): print the name", null, "void", MBeanOperationInfo.INFO);
mBeanInfo = new MBeanInfo(className, description, attributes, constructors, operations, mBeanNotificationInfoArray);
}
@Override
public Object getAttribute(String attribute_name) {
if (attribute_name == null) {
return null;
}
if (attribute_name.equals("Name")) {
return name;
}
return null;
}
@Override
public void setAttribute(Attribute attribute) {
if (attribute == null) {
return;
}
String Name = attribute.getName();
Object value = attribute.getValue();
try {
if (Name.equals("Name")) {
// if null value, try and if the setter returns any exception
if (value == null) {
name = null;
// if non null value, make sure it is assignable to the attribute
} else if ((Class.forName("java.lang.String")).isAssignableFrom(value.getClass())) {
name = (String) value;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public AttributeList getAttributes(String[] attributeNames) {
if (attributeNames == null) {
return null;
}
AttributeList resultList = new AttributeList();
// if attributeNames is empty, return anempty result list
if (attributeNames.length == 0) {
return resultList;
}
for (int i = 0; i < attributeNames.length; i++) {
try {
Object value = getAttribute(attributeNames[i]);
resultList.add(new Attribute(attributeNames[i], value));
} catch (Exception e) {
e.printStackTrace();
}
}
return resultList;
}
@Override
public AttributeList setAttributes(AttributeList attributes) {
if (attributes == null) {
return null;
}
AttributeList resultList = new AttributeList();
// if attributeNames is empty, nothing more to do
if (attributes.isEmpty()) {
return resultList;
}
// for each attribute, try to set it and add to the result list if successfull
for (Iterator i = attributes.iterator(); i.hasNext(); ) {
Attribute attr = (Attribute) i.next();
try {
setAttribute(attr);
String name = attr.getName();
Object value = getAttribute(name);
resultList.add(new Attribute(name, value));
} catch (Exception e) {
e.printStackTrace();
}
}
return resultList;
}
@Override
public Object invoke(String operationName, Object params[], String signature[]) throws MBeanException, ReflectionException {
// Check for a recognized operationname and call the corresponding operation
if (operationName.equals("print")) {
System.out.println("Hello, " + name + ", this is HelloDynamic!");
//dynamic add a method
dynamicAddOperation();
return null;
} else if (operationName.equals("print1")) {
System.out.println("dynamically add a print1 method");
return null;
} else {
// unrecognized operation name:
throw new ReflectionException(new NoSuchMethodException(operationName), "Cannot find the operation " + operationName + " in " + className);
}
}
public MBeanInfo getMBeanInfo() {
return mBeanInfo;
}
}
创建代理
public class HelloAgent2 {//as same as standard mbean
public static void main(String[] args) throws MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
//create mbean server
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
//create object name
ObjectName helloName = new ObjectName("jmx:name=dynamicBeanHello");
//create mbean and register mbean
server.registerMBean(new HelloDynamic(), helloName);
//create adaptor, adaptor is just a form as show mbean. It has no relation to specific mbean.
HtmlAdaptorServer adaptor = new HtmlAdaptorServer();
//create adaptor name
ObjectName adaptorName = new ObjectName("jmxAdaptor:name=adaptor,port=5050");
//register adaptor and adaptor name
server.registerMBean(adaptor, adaptorName);
adaptor.setPort(9999);
adaptor.start();
System.out.println("....................jmx server start....................");
}
}
public class HelloAgent2 {//as same as standard mbean
public static void main(String[] args) throws MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
//create mbean server
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
//create object name
ObjectName helloName = new ObjectName("jmx:name=dynamicBeanHello");
//create mbean and register mbean
server.registerMBean(new HelloDynamic(), helloName);
//create adaptor, adaptor is just a form as show mbean. It has no relation to specific mbean.
HtmlAdaptorServer adaptor = new HtmlAdaptorServer();
//create adaptor name
ObjectName adaptorName = new ObjectName("jmxAdaptor:name=adaptor,port=5050");
//register adaptor and adaptor name
server.registerMBean(adaptor, adaptorName);
adaptor.setPort(9999);
adaptor.start();
System.out.println("....................jmx server start....................");
}
}
HtmlAdaptorServer 在com.sun.jdmk.comm包下,需要下载jar包(下载jmx-1.2.1),把其中的jmxri.jar、jmxtools.jar放入项目中。