|
|
How Can I Find All Instances Of WombatMBean? |
Here is a small example that demonstrates how to retrieve all
MBeans which are wombats, including domestic wombats and feral wombats,
which are very ferocious.
The question here is to find out a way to retrieve all MBeans which
extend a base MBean class (in our example Wombat) or whose MBean interface
extends a base MBean interface (in our example WombatMBean).
The most efficient way to query for MBeans is to use an ObjectName
pattern. If you follow the recommended MBean Naming Scheme, then all
your MBeans will have a 'type' key indicating their type.
| You could therefore arrange for a clever naming scheme that
would allow you to retrieve all instances of Wombat
with a single ObjectName pattern.
However, complex class hierarchies are something that cannot
be completely modeled
by an ObjectName. If your class hierarchy is simple, you might
get away by saying that all your wombats will have a type key
whose value ends with Wombat. That way, you will be able to
say that all MBeans in your domain which have a type key assignement
of the form type=Wombat or type=DomesticWombat or
type=FeralWombat are all wombats. However this will not
lead you much farther.
|
With JDK 6, an alternative solution is to use a Query.isInstanceOf. This query was added in JDK 6, so with
JDK 5 you might need to loop as shown in the code example below.
Note that in both these cases, if your MBeans are
DynamicMBeans (for instance, if
you're using javax.management.StandardMBean) - you will
need to take some precautions with regard to class loading.
The extract of code below illustrates the three methods outlined
above. It also illustrate how to instantiate a StandardMBean
in such a way that isInstanceOf will work.
See javax.management.StandardMBean: When And Why
for more information on this baroque issue.
The last solution (not demonstrated here) would be to code a
metadata service that would know about the class hierarchy and
would be able to perform such 'educated' queries. This might be
an additional feature for JMX 2.0.
The code example below only shows the simpler alternatives that
you can implement and use today.
Cheers,
-- daniel
package instanceofpkg;
import java.lang.management.ManagementFactory;
import java.util.logging.Logger;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;
import javax.management.StandardMBean;
public class InstanceOfExample {
private static final Logger LOG =
Logger.getLogger(InstanceOfExample.class.getName());
public InstanceOfExample() {
}
public static interface WombatMBean {
public String getDescription();
}
public static class Wombat implements WombatMBean {
public String getDescription() { return "I am a wombat"; }
}
public static class TypedWombat extends Wombat {
public TypedWombat(String wombatType) {
type=wombatType;
}
final String type;
public String getDescription() {
return "I am a "+
((type==null)?"":(type+" "))+ "wombat";
}
public final String getKind() {
return type;
}
}
public static interface DomesticWombatMBean extends WombatMBean {
public String getPetName();
}
public static class DomesticWombat extends TypedWombat
implements DomesticWombatMBean {
public DomesticWombat() {
super("domestic");
}
public String getPetName() { return "Totor"; }
}
public static interface FeralWombatMBean extends WombatMBean {
public boolean isFerocious();
}
public static class FeralWombat extends TypedWombat
implements FeralWombatMBean {
public FeralWombat() {
super("feral");
}
public boolean isFerocious() { return true; }
}
public static void main(String[] args) throws Exception {
final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
final ObjectName wombatName =
new ObjectName("australia:type=Wombat,name=anonymous");
final ObjectName typedName =
new ObjectName("australia:type=Wombat,name=strange");
final ObjectName domesticName =
new ObjectName("australia:type=DomesticWombat,name=Totor");
final ObjectName feralName =
new ObjectName("australia:type=FeralWombat,name=SuperWombat");
server.registerMBean(new StandardMBean(new Wombat(),WombatMBean.class){},wombatName);
server.registerMBean(new StandardMBean(new TypedWombat("strange"),WombatMBean.class){},typedName);
server.registerMBean(new StandardMBean(new DomesticWombat(),DomesticWombatMBean.class){},domesticName);
server.registerMBean(new StandardMBean(new FeralWombat(),FeralWombatMBean.class){},feralName);
System.out.println("query with pattern: "+
server.queryNames(new ObjectName("australia:*,type=*Wombat"),
null));
final String className = WombatMBean.class.getName();
System.out.println("query with isInstanceOF: "+
server.queryNames(new ObjectName("australia:*"),
Query.isInstanceOf(Query.value(className))));
System.out.println("loop...");
for (ObjectName n:server.queryNames(new ObjectName("australia:*"), null)) {
if (server.isInstanceOf(n,className))
System.out.println(n+" is a "+className);
}
}
}
|
Tags:
java
jmx
Posted by dfuchs
( Mar 15 2007, 08:57:11 PM CET )
Permalink
|