org.jgroups.util.Metrics Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote Jakarta Enterprise Beans and Jakarta Messaging, including
all dependencies. It is intended for use by those not using maven, maven users should just import the Jakarta Enterprise Beans and
Jakarta Messaging BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
The newest version!
package org.jgroups.util;
import org.jgroups.JChannel;
import org.jgroups.annotations.ManagedAttribute;
import org.jgroups.annotations.Property;
import org.jgroups.jmx.ResourceDMBean;
import org.jgroups.stack.Protocol;
import javax.management.MBeanAttributeInfo;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Predicate;
import java.util.function.Supplier;
import static org.jgroups.conf.AttributeType.BYTES;
import static org.jgroups.conf.AttributeType.SCALAR;
/**
* Extracts all attributes and methods annotated with {@link org.jgroups.annotations.ManagedAttribute} and returns them
* as a map of names associated with [getter-method/description tuples]. E.g. for an attribute called foo, a method
* foo() or getFoo() is searched for.
* @author Bela Ban
* @since 5.4, 5.3.6
*/
public class Metrics {
protected JChannel ch;
public static final Predicate IS_NUMBER=obj -> {
if(obj instanceof Field)
return isNumberAndScalar(obj, ((Field)obj).getType());
if(obj instanceof Method) {
Method m=(Method)obj;
return isNumberAndScalar(obj, m.getReturnType());
}
return false;
};
public static class Entry {
protected final AccessibleObject type; // Field or Method
protected final String description;
protected final Supplier supplier;
protected Entry(AccessibleObject type, String description, Supplier method) {
this.type=type;
this.description=description;
this.supplier=method;
}
public AccessibleObject type() {return type;}
public String description() {return description;}
public Supplier supplier() {return supplier;}
@Override
public String toString() {
return String.format(" %s [%s]", supplier.get(), description);
}
}
public static Map>> extract(JChannel ch) {
return extract(ch, null);
}
public static Map>> extract(JChannel ch, Predicate filter) {
Map>> map=new LinkedHashMap<>();
for(Protocol p: ch.stack().getProtocols())
map.put(p.getName(), extract(p, filter));
return map;
}
public static Map> extract(Protocol p) {
return extract(p, null);
}
public static Map> extract(Protocol p, Predicate filter) {
Map> map=new TreeMap<>();
ResourceDMBean dm=new ResourceDMBean(p, filter);
dm.forAllAttributes((k,v) -> {
MBeanAttributeInfo info=v.info();
String descr=info != null? info.getDescription() : "n/a";
Supplier
© 2015 - 2025 Weber Informatics LLC | Privacy Policy