下面列出了javax.management.ObjectInstance#getObjectName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Map<String, Object> readCPUUsage(ObjectInstance oi, MBeanServerConnection mbsc) {
Map<String, Object> m = new LinkedHashMap<String, Object>();
ObjectName on = oi.getObjectName();
Object procCPU = getMBeanAttrValue(mbsc, on, "ProcessCpuLoad");
Object systemCPU = getMBeanAttrValue(mbsc, on, "SystemCpuLoad");
if (procCPU == null) {
procCPU = new Long(-1);
systemCPU = new Long(-1);
}
else {
DecimalFormat formatter = new DecimalFormat("00.0");
procCPU = Double.valueOf(formatter.format((Double) procCPU * 100));
systemCPU = Double.valueOf(formatter.format((Double) systemCPU * 100));
}
m.put("cpu_p", procCPU);
m.put("cpu_s", systemCPU);
return m;
}
private void mappingCollctors() throws Exception {
Set<ObjectInstance> beans = mbsc.queryMBeans(Client.getObjectName(GARBAGE_COLLECTORS), null);
for (ObjectInstance collector : beans) {
ObjectName collectorObjName = collector.getObjectName();
String collectorName = getAttribute(collectorObjName, "Name");
if ("Copy".equals(collectorName) || "PS Scavenge".equals(collectorName) || "ParNew".equals(collectorName)
|| "G1 Young Generation".equals(collectorName)) {
ygcCollector = collectorObjName;
} else if ("MarkSweepCompact".equals(collectorName) || "PS MarkSweep".equals(collectorName)
|| "ConcurrentMarkSweep".equals(collectorName) || "G1 Old Generation".equals(collectorName)) {
fgcCollector = collectorObjName;
} else {
ygcCollector = collectorObjName;
}
}
}
private void mappingPools() throws Exception {
Set<ObjectInstance> beans = mbsc.queryMBeans(Client.getObjectName(MEM_POOL_PREFIX + "*"), null);
for (ObjectInstance pool : beans) {
ObjectName poolObjName = pool.getObjectName();
String poolName = getAttribute(poolObjName, "Name");
poolName = poolName.trim().toLowerCase();
if (poolName.contains("eden")) {
eden = poolObjName;
} else if (poolName.contains("survivor")) {
sur = poolObjName;
} else if (poolName.contains("old") || poolName.contains("tenured")) {
old = poolObjName;
} else if (poolName.contains("perm") || poolName.contains("metaspace")) {
perm = poolObjName;
} else if (poolName.contains("compressed class space")) {
ccs = poolObjName;
}
}
}
private void mappingPools() throws Exception {
// full gc的collector,下属的memoryPool包括所有需要GC的Pool(Code Reserve等则不在此列)
Set<ObjectInstance> beans = mbsc.queryMBeans(Client.getObjectName(MEM_POOL_PREFIX + "*"), null);
pollNameMapping = new HashMap<String, String>();
for (ObjectInstance collector : beans) {
ObjectName collectorName = collector.getObjectName();
String name = (String) getAttribute(collectorName, "Name");
name = name.trim();
String lowerCaseName = name.toLowerCase();
if (lowerCaseName.contains(SURVIVOR)) {
pollNameMapping.put(SURVIVOR, name);
} else if (lowerCaseName.contains(EDEN)) {
pollNameMapping.put(EDEN, name);
} else if (lowerCaseName.contains(OLD) || lowerCaseName.contains(TENURED)) {
pollNameMapping.put(OLD, name);
} else if (lowerCaseName.contains(PERM) || lowerCaseName.contains(METASPACE)) {
pollNameMapping.put(PERM, name);
} else if(lowerCaseName.contains(COMPRESSED_CLASS_SPACE)) {
pollNameMapping.put(COMPRESSED_CLASS_SPACE, name);
}
}
}
/**
* Queries for all MBeans from the MBean Server and only looks at the relevant MBean and gets its
* metric numbers.
*/
private void updateMetrics() {
try {
QueryExp exp = Query.match(Query.attr("SensorName"), Query.value("*" + CLUSTER_NAME + "*"));
Set<ObjectInstance> mbeans = new HashSet<>(ManagementFactory.getPlatformMBeanServer()
.queryMBeans(new ObjectName("ClusterStatus:*"), exp));
for (ObjectInstance instance : mbeans) {
ObjectName beanName = instance.getObjectName();
if (beanName.toString().equals("ClusterStatus:cluster=" + CLUSTER_NAME)) {
MBeanInfo info = _server.getMBeanInfo(beanName);
MBeanAttributeInfo[] infos = info.getAttributes();
for (MBeanAttributeInfo infoItem : infos) {
Object val = _server.getAttribute(beanName, infoItem.getName());
_beanValueMap.put(infoItem.getName(), val);
}
}
}
} catch (Exception e) {
// update failed
}
}
public void init(String host, int port) throws Exception {
int iport = 0;
String shost = null;
mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
String onStr = "*:type=ThreadPool,*";
ObjectName objectName = new ObjectName(onStr);
Set<ObjectInstance> set = mBeanServer.queryMBeans(objectName, null);
for (ObjectInstance oi : set) {
objName = oi.getObjectName();
String name = objName.getKeyProperty("name");
/* Name are:
* http-8080
* jk-10.33.144.3-8009
* jk-jfcpc%2F10.33.144.3-8009
*/
String [] elenames = name.split("-");
String sport = elenames[elenames.length-1];
iport = Integer.parseInt(sport);
String [] shosts = elenames[1].split("%2F");
shost = shosts[0];
if (port==0 && host==null)
break; /* Take the first one */
if (host==null && iport==port)
break; /* Only port done */
if (shost.compareTo(host) == 0)
break; /* Done port and host are the expected ones */
}
if (objName == null)
throw new Exception("Can't find connector for " + host + ":" + port);
this.port = iport;
this.host = shost;
}
/**
* Converts from an object name pattern to a real object name, by querying with findMBean;
* if no matching MBean can be found (or if more than one match found) then returns null.
* If the supplied object name is not a pattern then just returns that. If the
*/
public ObjectName toLiteralObjectName(ObjectName objectName) {
if (checkNotNull(objectName, "objectName").isPattern()) {
ObjectInstance bean = findMBean(objectName);
return (bean != null) ? bean.getObjectName() : null;
} else {
return objectName;
}
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
for (Class<?> c : new Class<?>[] {
Spume.class, Thing.class, XSpume.class, XThing.class
}) {
for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
System.out.println("Class " + c.getName() + " with name " + n +
"...");
ObjectName realName = new ObjectName("a:type=" + c.getName());
Constructor<?> constr = c.getConstructor(ObjectName.class);
Object mbean = constr.newInstance(realName);
ObjectInstance oi;
String what =
"Registering MBean of type " + c.getName() + " under name " +
"<" + n + ">: ";
try {
oi = mbs.registerMBean(mbean, n);
} catch (Exception e) {
e.printStackTrace();
fail(what + " got " + e);
continue;
}
ObjectName registeredName = oi.getObjectName();
if (!registeredName.equals(realName))
fail(what + " registered as " + registeredName);
if (!mbs.isRegistered(realName)) {
fail(what + " not registered as expected");
}
mbs.unregisterMBean(registeredName);
}
}
System.err.flush();
if (failures == 0)
System.out.println("TEST PASSED");
else
throw new Exception("TEST FAILED: " + failure);
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
for (Class<?> c : new Class<?>[] {
Spume.class, Thing.class, XSpume.class, XThing.class
}) {
for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
System.out.println("Class " + c.getName() + " with name " + n +
"...");
ObjectName realName = new ObjectName("a:type=" + c.getName());
Constructor<?> constr = c.getConstructor(ObjectName.class);
Object mbean = constr.newInstance(realName);
ObjectInstance oi;
String what =
"Registering MBean of type " + c.getName() + " under name " +
"<" + n + ">: ";
try {
oi = mbs.registerMBean(mbean, n);
} catch (Exception e) {
e.printStackTrace();
fail(what + " got " + e);
continue;
}
ObjectName registeredName = oi.getObjectName();
if (!registeredName.equals(realName))
fail(what + " registered as " + registeredName);
if (!mbs.isRegistered(realName)) {
fail(what + " not registered as expected");
}
mbs.unregisterMBean(registeredName);
}
}
System.err.flush();
if (failures == 0)
System.out.println("TEST PASSED");
else
throw new Exception("TEST FAILED: " + failure);
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
for (Class<?> c : new Class<?>[] {
Spume.class, Thing.class, XSpume.class, XThing.class
}) {
for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
System.out.println("Class " + c.getName() + " with name " + n +
"...");
ObjectName realName = new ObjectName("a:type=" + c.getName());
Constructor<?> constr = c.getConstructor(ObjectName.class);
Object mbean = constr.newInstance(realName);
ObjectInstance oi;
String what =
"Registering MBean of type " + c.getName() + " under name " +
"<" + n + ">: ";
try {
oi = mbs.registerMBean(mbean, n);
} catch (Exception e) {
e.printStackTrace();
fail(what + " got " + e);
continue;
}
ObjectName registeredName = oi.getObjectName();
if (!registeredName.equals(realName))
fail(what + " registered as " + registeredName);
if (!mbs.isRegistered(realName)) {
fail(what + " not registered as expected");
}
mbs.unregisterMBean(registeredName);
}
}
System.err.flush();
if (failures == 0)
System.out.println("TEST PASSED");
else
throw new Exception("TEST FAILED: " + failure);
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
for (Class<?> c : new Class<?>[] {
Spume.class, Thing.class, XSpume.class, XThing.class
}) {
for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
System.out.println("Class " + c.getName() + " with name " + n +
"...");
ObjectName realName = new ObjectName("a:type=" + c.getName());
Constructor<?> constr = c.getConstructor(ObjectName.class);
Object mbean = constr.newInstance(realName);
ObjectInstance oi;
String what =
"Registering MBean of type " + c.getName() + " under name " +
"<" + n + ">: ";
try {
oi = mbs.registerMBean(mbean, n);
} catch (Exception e) {
e.printStackTrace();
fail(what + " got " + e);
continue;
}
ObjectName registeredName = oi.getObjectName();
if (!registeredName.equals(realName))
fail(what + " registered as " + registeredName);
if (!mbs.isRegistered(realName)) {
fail(what + " not registered as expected");
}
mbs.unregisterMBean(registeredName);
}
}
System.err.flush();
if (failures == 0)
System.out.println("TEST PASSED");
else
throw new Exception("TEST FAILED: " + failure);
}
public void init(String host, int port) throws Exception {
int iport = 0;
String shost = null;
mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
String onStr = "*:type=ThreadPool,*";
ObjectName objectName = new ObjectName(onStr);
Set<ObjectInstance> set = mBeanServer.queryMBeans(objectName, null);
Iterator<ObjectInstance> iterator = set.iterator();
while (iterator.hasNext()) {
ObjectInstance oi = iterator.next();
objName = oi.getObjectName();
String name = objName.getKeyProperty("name");
/* Name are:
* http-8080
* jk-10.33.144.3-8009
* jk-jfcpc%2F10.33.144.3-8009
*/
String [] elenames = name.split("-");
String sport = elenames[elenames.length-1];
iport = Integer.parseInt(sport);
String [] shosts = elenames[1].split("%2F");
shost = shosts[0];
if (port==0 && host==null)
break; /* Take the first one */
if (host==null && iport==port)
break; /* Only port done */
if (shost.compareTo(host) == 0)
break; /* Done port and host are the expected ones */
}
if (objName == null)
throw(new Exception("Can't find connector for " + host + ":" + port));
this.port = iport;
this.host = shost;
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
for (Class<?> c : new Class<?>[] {
Spume.class, Thing.class, XSpume.class, XThing.class
}) {
for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
System.out.println("Class " + c.getName() + " with name " + n +
"...");
ObjectName realName = new ObjectName("a:type=" + c.getName());
Constructor<?> constr = c.getConstructor(ObjectName.class);
Object mbean = constr.newInstance(realName);
ObjectInstance oi;
String what =
"Registering MBean of type " + c.getName() + " under name " +
"<" + n + ">: ";
try {
oi = mbs.registerMBean(mbean, n);
} catch (Exception e) {
e.printStackTrace();
fail(what + " got " + e);
continue;
}
ObjectName registeredName = oi.getObjectName();
if (!registeredName.equals(realName))
fail(what + " registered as " + registeredName);
if (!mbs.isRegistered(realName)) {
fail(what + " not registered as expected");
}
mbs.unregisterMBean(registeredName);
}
}
System.err.flush();
if (failures == 0)
System.out.println("TEST PASSED");
else
throw new Exception("TEST FAILED: " + failure);
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
for (Class<?> c : new Class<?>[] {
Spume.class, Thing.class, XSpume.class, XThing.class
}) {
for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
System.out.println("Class " + c.getName() + " with name " + n +
"...");
ObjectName realName = new ObjectName("a:type=" + c.getName());
Constructor<?> constr = c.getConstructor(ObjectName.class);
Object mbean = constr.newInstance(realName);
ObjectInstance oi;
String what =
"Registering MBean of type " + c.getName() + " under name " +
"<" + n + ">: ";
try {
oi = mbs.registerMBean(mbean, n);
} catch (Exception e) {
e.printStackTrace();
fail(what + " got " + e);
continue;
}
ObjectName registeredName = oi.getObjectName();
if (!registeredName.equals(realName))
fail(what + " registered as " + registeredName);
if (!mbs.isRegistered(realName)) {
fail(what + " not registered as expected");
}
mbs.unregisterMBean(registeredName);
}
}
System.err.flush();
if (failures == 0)
System.out.println("TEST PASSED");
else
throw new Exception("TEST FAILED: " + failure);
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
for (Class<?> c : new Class<?>[] {
Spume.class, Thing.class, XSpume.class, XThing.class
}) {
for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
System.out.println("Class " + c.getName() + " with name " + n +
"...");
ObjectName realName = new ObjectName("a:type=" + c.getName());
Constructor<?> constr = c.getConstructor(ObjectName.class);
Object mbean = constr.newInstance(realName);
ObjectInstance oi;
String what =
"Registering MBean of type " + c.getName() + " under name " +
"<" + n + ">: ";
try {
oi = mbs.registerMBean(mbean, n);
} catch (Exception e) {
e.printStackTrace();
fail(what + " got " + e);
continue;
}
ObjectName registeredName = oi.getObjectName();
if (!registeredName.equals(realName))
fail(what + " registered as " + registeredName);
if (!mbs.isRegistered(realName)) {
fail(what + " not registered as expected");
}
mbs.unregisterMBean(registeredName);
}
}
System.err.flush();
if (failures == 0)
System.out.println("TEST PASSED");
else
throw new Exception("TEST FAILED: " + failure);
}
/**
* Actually register the MBean with the server. The behavior when encountering
* an existing MBean can be configured using the {@link #setRegistrationBehavior(int)}
* and {@link #setRegistrationBehaviorName(String)} methods.
* @param mbean the MBean instance
* @param objectName the suggested ObjectName for the MBean
* @throws JMException if the registration failed
*/
protected void doRegister(Object mbean, ObjectName objectName) throws JMException {
ObjectName actualObjectName;
synchronized (this.registeredBeans) {
ObjectInstance registeredBean = null;
try {
registeredBean = this.server.registerMBean(mbean, objectName);
}
catch (InstanceAlreadyExistsException ex) {
if (this.registrationPolicy == RegistrationPolicy.IGNORE_EXISTING) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring existing MBean at [" + objectName + "]");
}
}
else if (this.registrationPolicy == RegistrationPolicy.REPLACE_EXISTING) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Replacing existing MBean at [" + objectName + "]");
}
this.server.unregisterMBean(objectName);
registeredBean = this.server.registerMBean(mbean, objectName);
}
catch (InstanceNotFoundException ex2) {
logger.error("Unable to replace existing MBean at [" + objectName + "]", ex2);
throw ex;
}
}
else {
throw ex;
}
}
// Track registration and notify listeners.
actualObjectName = (registeredBean != null ? registeredBean.getObjectName() : null);
if (actualObjectName == null) {
actualObjectName = objectName;
}
this.registeredBeans.add(actualObjectName);
}
onRegister(actualObjectName, mbean);
}
/**
* Actually register the MBean with the server. The behavior when encountering
* an existing MBean can be configured using {@link #setRegistrationPolicy}.
* @param mbean the MBean instance
* @param objectName the suggested ObjectName for the MBean
* @throws JMException if the registration failed
*/
protected void doRegister(Object mbean, ObjectName objectName) throws JMException {
Assert.state(this.server != null, "No MBeanServer set");
ObjectName actualObjectName;
synchronized (this.registeredBeans) {
ObjectInstance registeredBean = null;
try {
registeredBean = this.server.registerMBean(mbean, objectName);
}
catch (InstanceAlreadyExistsException ex) {
if (this.registrationPolicy == RegistrationPolicy.IGNORE_EXISTING) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring existing MBean at [" + objectName + "]");
}
}
else if (this.registrationPolicy == RegistrationPolicy.REPLACE_EXISTING) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Replacing existing MBean at [" + objectName + "]");
}
this.server.unregisterMBean(objectName);
registeredBean = this.server.registerMBean(mbean, objectName);
}
catch (InstanceNotFoundException ex2) {
if (logger.isInfoEnabled()) {
logger.info("Unable to replace existing MBean at [" + objectName + "]", ex2);
}
throw ex;
}
}
else {
throw ex;
}
}
// Track registration and notify listeners.
actualObjectName = (registeredBean != null ? registeredBean.getObjectName() : null);
if (actualObjectName == null) {
actualObjectName = objectName;
}
this.registeredBeans.add(actualObjectName);
}
onRegister(actualObjectName, mbean);
}
/**
* Actually register the MBean with the server. The behavior when encountering
* an existing MBean can be configured using the {@link #setRegistrationBehavior(int)}
* and {@link #setRegistrationBehaviorName(String)} methods.
* @param mbean the MBean instance
* @param objectName the suggested ObjectName for the MBean
* @throws JMException if the registration failed
*/
protected void doRegister(Object mbean, ObjectName objectName) throws JMException {
ObjectName actualObjectName;
synchronized (this.registeredBeans) {
ObjectInstance registeredBean = null;
try {
registeredBean = this.server.registerMBean(mbean, objectName);
}
catch (InstanceAlreadyExistsException ex) {
if (this.registrationPolicy == RegistrationPolicy.IGNORE_EXISTING) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring existing MBean at [" + objectName + "]");
}
}
else if (this.registrationPolicy == RegistrationPolicy.REPLACE_EXISTING) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Replacing existing MBean at [" + objectName + "]");
}
this.server.unregisterMBean(objectName);
registeredBean = this.server.registerMBean(mbean, objectName);
}
catch (InstanceNotFoundException ex2) {
logger.error("Unable to replace existing MBean at [" + objectName + "]", ex2);
throw ex;
}
}
else {
throw ex;
}
}
// Track registration and notify listeners.
actualObjectName = (registeredBean != null ? registeredBean.getObjectName() : null);
if (actualObjectName == null) {
actualObjectName = objectName;
}
this.registeredBeans.add(actualObjectName);
}
onRegister(actualObjectName, mbean);
}
public static Map<String, Object> readHeapUsage(ObjectInstance oi, MBeanServerConnection mbsc) {
Map<String, Object> m = new LinkedHashMap<String, Object>();
ObjectName on = oi.getObjectName();
m.put("heap_use", getMBeanAttrValue(mbsc, on, "HeapMemoryUsage", "used"));
m.put("heap_commit", getMBeanAttrValue(mbsc, on, "HeapMemoryUsage", "committed"));
m.put("heap_max", getMBeanAttrValue(mbsc, on, "HeapMemoryUsage", "max"));
m.put("heap_init", getMBeanAttrValue(mbsc, on, "HeapMemoryUsage", "init"));
return m;
}
public static Map<String, Object> readClassLoadUsage(ObjectInstance oi, MBeanServerConnection mbsc) {
Map<String, Object> m = new LinkedHashMap<String, Object>();
ObjectName on = oi.getObjectName();
m.put("class_total", getMBeanAttrValue(mbsc, on, "TotalLoadedClassCount"));
m.put("class_load", getMBeanAttrValue(mbsc, on, "LoadedClassCount"));
m.put("class_unload", getMBeanAttrValue(mbsc, on, "UnloadedClassCount"));
return m;
}