Professional Documents
Culture Documents
Introduction
Using JMX (Java Management eXtensions) to manage part of your application is a great java feature. The
standard Mbeans mechanisms which consist of creating a classwhich implements an MBean interface is quite
simple and already allows to manage most of your application.
In this article, I will describe how can we implements the DynamicMBean interface to do more 'complex'
things. Especially, we will look on how we can we expose and modify a set of properties, and how can we
expose methods by specifiing their names (The java code will be more clear than my poor english).
The following examples are taken from the work I have done on the PEtALS CDK (Component Development
Kit) in order to expose a set of JBI extensions and bootstrap methods for management.
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#getAttribute(java.lang.String)
*/
.getProperties().getProperty(attribute);
if (value != null
&& !value
.startsWith(ComponentPropertiesManager.MANAGED_METHOD_PREFIX)) {
return value;
} else {
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#getAttributes(java.lang.String[])
*/
.getProperties().getProperty(name);
if (value != null
&& !value
.startsWith(ComponentPropertiesManager.MANAGED_METHOD_PREFIX))
return list;
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
*/
MBeanException, ReflectionException {
if (managedBootstrap.getComponentPropertiesManager().getProperties()
.getProperty(name) == null) {
throw new AttributeNotFoundException(name);
managedBootstrap.getComponentPropertiesManager().getProperties()
try {
save();
} catch (IOException e) {
/*
* (non-Javadoc)
* @see
javax.management.DynamicMBean#setAttributes(javax.management.AttributeList)
*/
if (managedBootstrap.getComponentPropertiesManager()
.getProperties().getProperty(name) != null
managedBootstrap.getComponentPropertiesManager()
try {
save();
} catch (IOException e) {
return result;
The previous methods are the ones which are called by the JMX implementation for management. In our case
it is quite simple since a property entry is directly mapped into an attribute...
In order to expose these attributes, we will see later that we need to create a set of MBeanAttributeInfo that
will be used by the getMBeanInfo method to create the information object used by JMX to expose all of this.
/**
* Dynamically get attributes for the MBean. The attributes are the
ones
* @return
*/
.getComponentPropertiesManager().getExposedAttributeKeys();
// build the MBeanAttributeInfo
.size()];
Iterator<String> it = attributeKeys.iterator();
return attrs;
/**
* Dynamically get the operations for the MBean. The operations are defined
* @return
*/
.getExposedMethodNames();
if (methods.contains(name)) {
operations.add(oper);
operations.toArray(result);
return result;
This time again, we are are creating a set of MBeanOperationInfo from a list methods names. We retrieve the
methods from the class we want to manage from the managedBootstrap class by reflection.
This method will be then used to create the MBeanInfo that is returned by the method getMBeanInfo:
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#getMBeanInfo()
*/
Invoking a method
Doing java reflection is out of this article scope. Have a look on the implementation to see how it is done.
Complete implementation
/**
* version 2.1 of the License, or (at your option) any later version.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* -------------------------------------------------------------------------
* $Id$
* -------------------------------------------------------------------------
*/
package org.objectweb.petals.component.common;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.ReflectionException;
import javax.management.RuntimeOperationsException;
/**
* The MBean used to expose component attributes and operations via JMX.
*/
public class ExtensionMBean implements DynamicMBean {
8);
primitiveClasses.put(Boolean.TYPE.toString(), Boolean.TYPE);
primitiveClasses.put(Character.TYPE.toString(), Character.TYPE);
primitiveClasses.put(Byte.TYPE.toString(), Byte.TYPE);
primitiveClasses.put(Short.TYPE.toString(), Short.TYPE);
primitiveClasses.put(Integer.TYPE.toString(), Integer.TYPE);
primitiveClasses.put(Long.TYPE.toString(), Long.TYPE);
primitiveClasses.put(Float.TYPE.toString(), Float.TYPE);
primitiveClasses.put(Double.TYPE.toString(), Double.TYPE);
/**
* @param bootstrap
*/
this.managedBootstrap = bootstrap;
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#getAttribute(java.lang.String)
*/
.getProperties().getProperty(attribute);
if (value != null
&& !value
.startsWith(ComponentPropertiesManager.MANAGED_METHOD_PREFIX)) {
return value;
} else {
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#getAttributes(java.lang.String[])
*/
.getProperties().getProperty(name);
if (value != null
&& !value
.startsWith(ComponentPropertiesManager.MANAGED_METHOD_PREFIX))
return list;
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
*/
MBeanException, ReflectionException {
if (managedBootstrap.getComponentPropertiesManager().getProperties()
.getProperty(name) == null) {
managedBootstrap.getComponentPropertiesManager().getProperties()
try {
save();
} catch (IOException e) {
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#setAttributes(javax.management.AttributeList)
*/
if (managedBootstrap.getComponentPropertiesManager()
.getProperties().getProperty(name) != null
managedBootstrap.getComponentPropertiesManager()
try {
save();
} catch (IOException e) {
return result;
/*
* (non-Javadoc)
* @see javax.management.DynamicMBean#getMBeanInfo()
*/
/**
* Dynamically get the operations for the MBean. The operations are defined
* @return
*/
.getExposedMethodNames();
if (methods.contains(name)) {
operations.add(oper);
}
result = new MBeanOperationInfo[operations.size()];
operations.toArray(result);
return result;
/**
* Dynamically get attributes for the MBean. The attributes are the ones
* @return
*/
.getComponentPropertiesManager().getExposedAttributeKeys();
.size()];
Iterator<String> it = attributeKeys.iterator();
return attrs;
/*
* (non-Javadoc)
*
* @see javax.management.DynamicMBean#invoke(java.lang.String,
* java.lang.Object[], java.lang.String[])
*/
: findSignatureClasses(signature, loader));
//
try {
} catch (Exception e) {
if (method == null) {
new NoSuchMethodException(actionName),
"The operation with name " + actionName + " could not be found");
try {
// invoke method
} catch (IllegalAccessException e) {
} catch (RuntimeException e) {
throw new RuntimeOperationsException(e, "RuntimeException"
} catch (InvocationTargetException e) {
+ actionName);
return result;
/**
* @param signature
* @param loader
* @return
* @throws ReflectionException
*/
throws ReflectionException {
if (signature == null)
return null;
if (length == 0)
return tab;
try {
.get(signature[i]);
if (primCla != null) {
tab[i] = primCla;
} else {
} catch (ClassNotFoundException e) {
} catch (RuntimeException e) {
throw e;
return tab;
/**
* @throws IOException
*/
managedBootstrap.getComponentPropertiesManager().save();
About
I am a software engineer and community manager on the open source PEtALS ESB project at eBM
WebSourcing.
You can get this article as PDF file on my scribd space or on my work blog.