Alachisoft.NCache.MetricsAgents.JMX.Metrics.CountersMBean Maven / Gradle / Ivy
The newest version!
package Alachisoft.NCache.MetricsAgents.JMX.Metrics;
import Alachisoft.NCache.DataModel.Counter;
import Alachisoft.NCache.DataModel.CounterData;
import com.alachisoft.ncache.common.monitoring.CounterMetadata;
import com.alachisoft.ncache.common.monitoring.Publisher;
import javassist.bytecode.AttributeInfo;
import javax.management.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CountersMBean implements DynamicMBean {
Map counters = new HashMap<>();
@Override
public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
CounterData counterData = counters.get(attribute);
return formatDouble(counterData.getData());
}
@Override
public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
counters.put(attribute.getName(), (CounterData) attribute.getValue());
}
@Override
public AttributeList getAttributes(String[] attributes) {
List attributeList = new ArrayList<>();
for (String counterName:
attributes) {
CounterData counter = counters.get(counterName);
if ( counter != null ){
Attribute reqAttribute = new Attribute(counterName, formatDouble(counter.getData()));
attributeList.add(reqAttribute);
}
}
return new AttributeList(attributeList);
}
@Override
public AttributeList setAttributes(AttributeList attributes) {
for (Attribute attribute:
attributes.asList()) {
counters.put(attribute.getName(), (CounterData) attribute.getValue());
}
return attributes;
}
@Override
public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException {
return null;
}
@Override
public MBeanInfo getMBeanInfo() {
MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[counters.size()];
int index = 0;
Publisher counterCategory = null;
for (Map.Entry entry : counters.entrySet()) {
CounterData counter = entry.getValue();
String counterName = entry.getKey();
CounterMetadata counterMetadata = counter.getCounterMetadata();
counterCategory = counterMetadata.getCategory();
attributeInfos[index++] = new MBeanAttributeInfo(
counterName,
"java.lang.double",
counterMetadata.getDescription(),
true,
false,
false
);
}
String description = "";
switch (counterCategory){
case NCache:
description = "Counters for cache";
break;
case Bridge:
description = "Counters for bridge";
break;
case SystemMetrics:
description = "Counters for SystemMetrics";
break;
case NCacheClient:
description = "Counters for NCacheClient";
break;
case BridgedCache:
description = "Counters for BridgedCache";
break;
case NCachePersistence:
description = "Counters for NCachePersistence";
break;
case NCacheLucene:
description = "Counters for NCacheLucene";
break;
}
return new MBeanInfo(
counterCategory.toString(),
description,
attributeInfos,
null,
null,
null
);
}
public void setAttributeValue(String counterName, CounterData counterData){
counters.put(counterName, counterData);
}
private Object formatDouble(double input) {
// Use DecimalFormat to format the double value
DecimalFormat decimalFormat = new DecimalFormat("#.##");
String formattedValue = decimalFormat.format(input);
// Parse the formatted value back to double
return Double.parseDouble(formattedValue) == -10.0 ? "--" : Double.parseDouble(formattedValue);
}
}