Search |
||
Inter-MXBean referencesPosted by emcmanus on June 21, 2006 at 8:50 AM PDT
MXBeans include a way to handle inter-MBean references conveniently. You can use this to build an MBean hierarchy that is simple to navigate. In a previous blog entry, I described MXBeans. User-defined MXBeans are a new feature in the Mustang (Java SE 6) platform. They define a type mapping, so you can use arbitrary types in the Java interface that defines your management interface, but have them mapped to a fixed set of predefined types. More details in that earlier entry. One thing I didn't mention there is that MXBeans contain a facility for managing references between MXBeans very simply. (This facility was based on an idea by Charles Paclat of BEA.) This is probably easiest to explain by example. Suppose I have some products, and each product is composed of one or more modules. I have one MBean per product and one MBean per module. Given the MBean for a product, I would like to be able to see the MBeans for its modules, and vice versa. It looks something like this:
The corresponding MXBean interfaces might look like this:
public interface ProductMXBean {
public Set<ModuleMXBean> getModules();
public void addModule(ModuleMXBean module);
public void removeModule(ModuleMXBean module);
public String getName();
// ...
}
public interface ModuleMXBean {
public ProductMXBean getProduct();
public String getName();
// ...
}
The
The Creating referencesWhat might the code that created the
// Create and register the Product
ObjectName productName = new ObjectName("com.example.myapp:type=Product");
ProductMXBean product = new ProductImpl("wonderprod");
mbeanServer.registerMBean(product, productName);
// Create and register the Modules and add each one to the Product
String[] moduleIds = {"fred", "jim", "sheila"};
for (String moduleId : moduleIds) {
ModuleMXBean module = new ModuleImpl(moduleId, product);
ObjectName moduleName =
new ObjectName("com.example.myapp:type=Module,name=" + moduleId);
mbeanServer.registerMBean(module, moduleName);
product.addModule(module);
}
Here we are creating the Product and its Modules all at the same time, so we are able to give the Product a direct reference to each of the Module objects and vice versa. The code in bold shows this happening. This approach assumes an intimate relationship between all the objects in question, and is most suitable if the code for creating all the MXBeans is small and centralized. Another possibility is illustrated by rewriting the code as follows. Only the code in bold below has changed.
// Create and register the Product
ObjectName productName = new ObjectName("com.example.myapp:type=Product");
ProductMXBean product = new ProductImpl("wonderprod");
mbeanServer.registerMBean(product, productName);
ProductMXBean productProxy =
JMX.newMXBeanProxy(mbeanServer, productName, ProductMXBean.class);
// Create and register the Modules and add each one to the Product
String[] moduleIds = {"fred", "jim", "sheila"};
for (String moduleId : moduleIds) {
ModuleMXBean module = new ModuleImpl(moduleId, productProxy);
ObjectName moduleName =
new ObjectName("com.example.myapp:type=Module,name=" + moduleId);
mbeanServer.registerMBean(module, moduleName);
ModuleMXBean moduleProxy =
JMX.newMXBeanProxy(mbeanServer, moduleName, ModuleMXBean.class);
product.addModule(moduleProxy);
}
The coupling between the Navigating the MXBean hierarchyThe real power of this approach comes when you are coding a client that interacts with this model. You can construct a proxy
for an MXBean using the new method But the You might have guessed the answer. It returns another proxy, this time for the Suppose we known the
Then we can navigate from this proxy to proxies for all of the product's modules:
In code, finding the names of all the modules given just the
ModuleMXBean startModuleProxy =
JMX.newMXBeanProxy(mbeanServerConnection, startModuleName,
ModuleMXBean.class);
ProductMXBean containingProduct = startModuleProxy.getProduct();
Set<ModuleMXBean> modules = containingProduct.getModules();
for (ModuleMXBean module : modules)
System.out.println(module.getName());
The ability to navigate through the MBean model using proxies is a very powerful one, and a strong incentive to use this approach to managing references between MBeans. »
Related Topics >>
Open JDK Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|