This section demonstrates how to connect JMX Managed Resource's instrumentation to the JMX Agent's MBeanServer.
The JMX Agent's MBean server is just a Java™ class that implements the MBeanServer interface. The class that actually implements the MBeanServer interface is vendor specific but it is created via JMX's MBeanServerFactory class. Because all the elements of the JMX Agent are normal Java classes you can make them part of your application and manage it from within rather than via some external agent. This is the approach we will take with our simple web server.
Creating an MBeanServer instance requires a single line of code
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
JMX management applications never use the actual object reference of an MBean. Instead MBeans are always accessed via the MBeanServer using their ObjectNames. An ObjectName consists of two parts: the domain and the key properties. The domain is instrumentation specific, i.e., it is an arbitrary string (with the exception of the characters: ':', ',', '=', '*', '?') that may have structure and meaning to a management application but is opaque to JMX. The key property list is a set of property-value pairs. A valid ObjectName must contain at least one key property but JMX does not define the keys or the values they may take. Some useful key properties include: 'type', 'vendor', 'version', 'id'.
The code fragment below creates an ObjectName for the HTTP daemon's Listener:
ObjectName listenerObjectName = new ObjectName("tutorial:type=Listener, vendor=Tivoli");
There are two ways to populate the MBeanServer's repository of MBeans. An
MBean can be instansiated programatically and then registered with the MBean
server or you can use one of the MBeanServer's createMBean()
methods to create
the instance and register it with the MBeanServer. The former approach is used
when you need access to elements of the MBean object's interface that aren't
part of its management interface.
We use both approaches in our HTTP daemon:
Object[] listenerParams = {requestQueue};
String[] listenerSig = {"java.util.SortedSet"};
mbs.createMBean("com.tivoli.jmx.tutorial.managedserver.Listener",
listenerObjectName,
listenerParams,
listenerSig);
mbs.registerMBean(handlers, handlersObjectName);
Finally, we can use the MBeanServer to invoke methods on our MBeans. This is accomplished with the MBeanServer's invoke() method as illustrated below:
mbs.invoke(listenerObjectName, "startListening", null, null)