标签:who jvm 继承 tor malformed int 自己 edm ready
JMX标准提供了四种不同的 MBean:
package test;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
/**
* @since 2019-11-16
*
*/
public class JMXMBeanTest {
/**
* @param args
* @throws MalformedObjectNameException
* @throws NotCompliantMBeanException
* @throws MBeanRegistrationException
* @throws InstanceAlreadyExistsException
* @throws IOException
*/
public static void main(String[] args) throws MalformedObjectNameException, InstanceAlreadyExistsException,
MBeanRegistrationException, NotCompliantMBeanException, IOException {
MBeanServer mBeanServer = null;
if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
} else {
mBeanServer = ManagementFactory.getPlatformMBeanServer();
}
ObjectName jmxObjectName = new ObjectName("jxmstudy:name=HelloWord");
Hello hello = new Hello();
hello.setName("Simon");
mBeanServer.registerMBean(hello, jmxObjectName);
System.in.read();
}
public static interface HelloMBean {
public String getName();
public void setName(String name);
}
public static class Hello implements HelloMBean {
private String name;
@Override
public String getName() {
return this.name;
}
@Override
public void setName(String name) {
this.name = name;
}
}
}
可以用 commons-modeler-2.0.1.jar完成,也可以参见tomcat的代码
具体做法是用配置文件描述mbean定义,然后走一个他自己开发的mbean公共机制完成注册,比如 org/apache/catalina/connector/mbeans-descriptors.xml 就是tomcat连接器jmx bean的描述。它有很多mbeans-descriptors.xml配置文件,针对不同组件各组件一个。
Tomcat7中的JMX使用(二)Dynamic MBean 这篇文章分析了。
————————————————
版权声明:本文为CSDN博主「朱小厮」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013256816/article/details/52817247
package com.test.jmx.modelBean;
import javax.management.*;
import javax.management.modelmbean.*;
/**
* Created by hidden on 2016/10/9.
*/
public class ModelMBeanUtils {
private static final boolean READABLE = true;
private static final boolean WRITABLE = true;
private static final boolean BOOLEAN = true;
private static final String STRING_CLASS = "java.lang.String";
public static RequiredModelMBean createModelerMBean() {
RequiredModelMBean model = null;
try {
model = new RequiredModelMBean();
model.setManagedResource(new Hello(), "ObjectReference");
ModelMBeanInfo info = createModelMBeanInfo();
model.setModelMBeanInfo(info);
} catch (Exception e) {
e.printStackTrace();
}
return model;
}
private static ModelMBeanInfo createModelMBeanInfo() {
//////////////////////////////////////////////////////////////////
// 属性 //
//////////////////////////////////////////////////////////////////
// 构造name属性信息
Descriptor portAttrDesc = new DescriptorSupport();
portAttrDesc.setField("name", "Name");
portAttrDesc.setField("descriptorType", "attribute");
portAttrDesc.setField("displayName", "Name");
portAttrDesc.setField("getMethod", "getName");
portAttrDesc.setField("setMethod", "setName");
ModelMBeanAttributeInfo nameAttrInfo = new ModelMBeanAttributeInfo(//
"Name", // 属性名
STRING_CLASS, //属性类型
"people name", // 描述文字
READABLE, WRITABLE, !BOOLEAN, // 读写
portAttrDesc // 属性描述
);
//////////////////////////////////////////////////////////////////
// 方法 //
//////////////////////////////////////////////////////////////////
// 构造 getName操作描述符信息
Descriptor getStateDesc = new DescriptorSupport(new String[] {
"name=getName",
"descriptorType=operation",
"class=com.test.jmx.modelBean.Hello",
"role=operation"
});
ModelMBeanOperationInfo getName = new ModelMBeanOperationInfo(//
"getName", //
"get name attribute", //
null, //
"java.lang.String", //
MBeanOperationInfo.ACTION, //
getStateDesc //
);
// 构造 setName操作描述符信息
Descriptor setStateDesc = new DescriptorSupport(new String[] {
"name=setName", "descriptorType=operation", "class=com.test.jmx.modelBean.Hello",
"role=operation" });
MBeanParameterInfo[] setStateParms = new MBeanParameterInfo[] { (new MBeanParameterInfo(
"name", "java.lang.String", "new name value")) };
ModelMBeanOperationInfo setName = new ModelMBeanOperationInfo(//
"setName", //
"set name attribute", //
setStateParms, //
"void", //
MBeanOperationInfo.ACTION, //
setStateDesc //
);
//构造 printHello()操作的信息
ModelMBeanOperationInfo print1Info = new ModelMBeanOperationInfo(//
"printHello", //
null, //
null, //
"void", //
MBeanOperationInfo.INFO, //
null //
);
// 构造printHello(String whoName)操作信息
ModelMBeanOperationInfo print2Info;
MBeanParameterInfo[] param2 = new MBeanParameterInfo[1];
param2[0] = new MBeanParameterInfo("whoName", STRING_CLASS, "say hello to who");
print2Info = new ModelMBeanOperationInfo(//
"printHello", //
null,//
param2,//
"void", //
MBeanOperationInfo.INFO, //
null//
);
//////////////////////////////////////////////////////////////////
// 最后总合 //
//////////////////////////////////////////////////////////////////
// create ModelMBeanInfo
ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport(//
RequiredModelMBean.class.getName(), // MBean类
null, // 描述文字
new ModelMBeanAttributeInfo[] { // 所有的属性信息(数组)
nameAttrInfo },//只有一个属性
null, // 所有的构造函数信息
new ModelMBeanOperationInfo[] { // 所有的操作信息(数组)
getName,
setName,
print1Info,
print2Info },//
null, // 所有的通知信息(本例无)
null//MBean描述
);
return mbeanInfo;
}
}
简答配置如:
-Dcom.sun.management.jmxremote.port=8085
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
标签:who jvm 继承 tor malformed int 自己 edm ready
原文地址:https://www.cnblogs.com/simoncook/p/11883569.html