java测算cpu占用_如何取得jvm实例的cpu占用(转)

protected String getStringValue(MBeanServerConnection mbsc, String key) throwsException {

MonitorItem item= newMonitorItem(key);if (item.getKeyId().equals("jmx")) {if (2 !=item.getArgumentCount()) {throw newMonitorException("required key format: jmx[,]");

}

ObjectName objectName= new ObjectName(item.getArgument(1));

String attributeName= item.getArgument(2);

String realAttributeName;

String fieldNames= "";intsep;//

//Attribute name and composite data field names are separated by dots. On the other hand the//name may contain a dot too. In this case user needs to escape it with a backslash. Also the//backslash symbols in the name must be escaped. So a real separator is unescaped dot and//separatorIndex() is used to locate it.//sep=HelperFunctionChest.separatorIndex(attributeName);if (-1 !=sep) {

LOGGER.trace("'{}' contains composite data", attributeName);

realAttributeName= attributeName.substring(0, sep);

fieldNames= attributeName.substring(sep + 1);

}else{

realAttributeName=attributeName;

}//unescape possible dots or backslashes that were escaped by user

realAttributeName =HelperFunctionChest.unescapeUserInput(realAttributeName);

LOGGER.trace("attributeName:'{}'", realAttributeName);

LOGGER.trace("fieldNames:'{}'", fieldNames);returngetPrimitiveAttributeValue(mbsc.getAttribute(objectName, realAttributeName),

fieldNames);

}else if (item.getKeyId().equals("jmx.discovery")) {if (0 !=item.getArgumentCount()) {throw new MonitorException("required key format: jmx.discovery");

}

JsonArray counters= newJsonArray();for (ObjectName name : mbsc.queryNames(null, null)) {

LOGGER.trace("discovered object '{}'", name);for(MBeanAttributeInfo attrInfo : mbsc.getMBeanInfo(name).getAttributes()) {

LOGGER.trace("discovered attribute '{}'", attrInfo.getName());if (!attrInfo.isReadable()) {

LOGGER.trace("attribute not readable, skipping");continue;

}try{

LOGGER.trace("looking for attributes of primitive types");

String

descr=(attrInfo.getName().equals(attrInfo.getDescription())? null: attrInfo

.getDescription());

findPrimitiveAttributes(counters, name, descr, attrInfo.getName(),

mbsc.getAttribute(name, attrInfo.getName()));

}catch(Exception e) {

Object[] logInfo={name, attrInfo.getName(), e};

LOGGER.trace("processing '{},{}' failed", logInfo);

}

}

}

JsonObject mapping= newJsonObject();

mapping.add(MonitorConst.JSON_TAG_DATA, counters);returnmapping.toString();

}else{throw new MonitorException("key ID '%s' is not supported", item.getKeyId());

}

}private String getPrimitiveAttributeValue(Object dataObject, String fieldNames) throwsMonitorException {

LOGGER

.trace("drilling down with data object '{}' and field names '{}'", dataObject,

fieldNames);if (null ==dataObject) {throw new MonitorException("data object is null");

}if (fieldNames.equals("")) {if(isPrimitiveAttributeType(dataObject.getClass())) {returndataObject.toString();

}else{throw newMonitorException("data object type is not primitive: %s" +dataObject.getClass());

}

}if (dataObject instanceofCompositeData) {

LOGGER.trace("'{}' contains composite data", dataObject);

CompositeData comp=(CompositeData) dataObject;

String dataObjectName;

String newFieldNames= "";int sep =HelperFunctionChest.separatorIndex(fieldNames);if (-1 !=sep) {

dataObjectName= fieldNames.substring(0, sep);

newFieldNames= fieldNames.substring(sep + 1);

}else{

dataObjectName=fieldNames;

}//unescape possible dots or backslashes that were escaped by user

dataObjectName =HelperFunctionChest.unescapeUserInput(dataObjectName);returngetPrimitiveAttributeValue(comp.get(dataObjectName), newFieldNames);

}else{throw new MonitorException("unsupported data object type along the path: %s",

dataObject.getClass());

}

}private voidfindPrimitiveAttributes(JsonArray counters, ObjectName name, String descr,

String attrPath, Object attribute) {

LOGGER.trace("drilling down with attribute path '{}'", attrPath);if(isPrimitiveAttributeType(attribute.getClass())) {

LOGGER.trace("found attribute of a primitive type: {}", attribute.getClass());

JsonObject counter= newJsonObject();

counter.addProperty("{#JMXDESC}", null == descr ? name + "," +attrPath : descr);

counter.addProperty("{#JMXOBJ}", name.toString());

counter.addProperty("{#JMXATTR}", attrPath);

counter.addProperty("{#JMXTYPE}", attribute.getClass().getName());

counter.addProperty("{#JMXVALUE}", attribute.toString());

counters.add(counter);

}else if (attribute instanceofCompositeData) {

LOGGER.trace("found attribute of a composite type: {}", attribute.getClass());

CompositeData comp=(CompositeData) attribute;for(String key : comp.getCompositeType().keySet()) {

findPrimitiveAttributes(counters, name, descr, attrPath+ "." +key, comp.get(key));

}

}else if (attribute instanceof TabularDataSupport ||attribute.getClass().isArray()) {

LOGGER.trace("found attribute of a known, unsupported type: {}", attribute.getClass());

}else{

LOGGER

.trace("found attribute of an unknown, unsupported type: {}", attribute.getClass());

}

}private boolean isPrimitiveAttributeType(Class>clazz) {

Class>[]

clazzez={Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class,

Float.class, Double.class, String.class, java.math.BigDecimal.class,

java.math.BigInteger.class,

java.util.Date.class, ObjectName.class};returnHelperFunctionChest.arrayContains(clazzez, clazz);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值