Interact with a JMX MBean

In order to call a method on an JMX MBean you must know its ObjectName on the server. This can be identified in advance and the exact ObjectName hardcoded/generated in your application, however any evolution of the MBeans ObjectName on the server in future versions would cause such implementation to fail when the ObjectNames no longer exactly match. This will not be identified until an opertion is attempted, as the code does not check that the MBean defined by the ObjectName actually exists before using the ObjectName and would only fail when calling a method and the target is not found by the MbeanServer.

Instead, the suggested route would be to use an ObjectName Pattern along with the queryNames method from the MBeanServerConnection class. You can then define a pattern of key-value pairs that the server will use to find all matching MBean ObjectNames and return these in a Set. By using property name-value pairs that uniquely identify an MBean, it is thus possible to locate the exact ObjectName of the MBean you wish to use without knowing every property name and value in its ObjectName in advance. In doing so you also validate the MBeans existence before using them via an MBean Proxy.

To create an ObjectName pattern for a Qpid JMX MBean, you define the ObjectName with the domain org.apache.qpid, and specify any name-value pairs that identify the MBean(s) you wish to gather the ObjectNames for, followed by ",*" That would define that the server return the ObjectNames of any MBean posessing the specific properties names and values you specify as well as 0 or more other properties of any value.

For example, to identify the VirtualHostManager MBean for a given VirtualHost (VHOST-NAME), you would construct an ObjectName which uniquely identified it by including the MBean type and the containing VirtualHost as below:

ObjectName hostManagerObjectName =
new ObjectName("org.apache.qpid:type=VirtualHost.VirtualHostManager,VirtualHost=VHOST-NAME,*")

This pattern will find the MBean we are interested in, regardless whether it has additional properties or not. Using this pattern, you would query the MBeanServer for all names that match, using the queryNames method from the MBeanServerConnection instance (mbsc) for your JMX connection to the server:

Set<ObjectName> objectInstances = mbsc.queryNames(hostManagerObjectName, null);

Checking that the returned set is of size 1 will ensure you have matched the specific MBean you wished, otherwise it either did not exist (size 0) or the properties specified were not enough to uniquely identify it and multiple mbeans matched your query (this is unlikely unless actually desired, as the VirtualHost, Exchange, Queue etc properties makes it straightforward to uniquely identify Qpid JMX objects)

The ObjectName(s) returned can then be retrieved from the Set and used as the target ObjectName for invoking operations via the MBeanServerConnection. One way of doing so is to use the ObjectName and the Interface for the MBean in question to create an MBean Proxy object, which can be used as if it were a normal local java object:

Java 6+:

ManagedBroker hostManagerProxy = JMX.newMBeanProxy(mbsc, objectName, ManagedBroker.class);


ManagedBroker hostManagerProxy = (ManagedBroker)
MBeanServerInvocationHandler.newProxyInstance(mbsc, objectName, ManagedBroker.class, false);

You may then call methods on the proxy object as if it were a local object:

hostManagerProxy.createNewQueue(queueName, null, true);

Interact with a JMX MBean from Javascript

The two attached examples, designate_bdbha_primary and dump_queue_attributes,  illustrate interacting with Qpid JMX MBeans from Javascript. These can be invoked from the command line using the jrunscript program distributed as part of the JDK.

$ jrunscript dump_queue_attributes.js localhost:8999 admin myvhost myqueue
Description=My queue
  • No labels