Enterprise Integration Zone is brought to you in partnership with:

Independent IT Consultant and Apache Camel Committer with a background in open source Java architecture, design and development. Currently helping companies utilize Apache ActiveMQ, Camel & Servicemix technologies. Ben is a DZone MVB and is not an employee of DZone and has posted 11 posts at DZone. You can read more from them at their website. View Full User Profile

Managing ActiveMQ with JMX APIs

06.21.2012
| 8458 views |
  • submit to reddit
Here is a quick example of how to programmatically access ActiveMQ MBeans to monitor and manipulate message queues...

First, get a connection to a JMX server (assumes localhost, port 1099, no auth)
Note, always cache the connection for subsequent requests (can cause memory utilization issues otherwise)

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url);
MBeanServerConnection conn = jmxc.getMBeanServerConnection();


Then, you can execute various operations such as addQueue, removeQueue, etc...

String operationName="addQueue";
String parameter="MyNewQueue";
ObjectName activeMQ = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");
if(parameter != null) {
    Object[] params = {parameter};
    String[] sig = {"java.lang.String"};
    conn.invoke(activeMQ, operationName, params, sig);
} else {
    conn.invoke(activeMQ, operationName,null,null);
} 


Also, you can get an ActiveMQ QueueViewMBean instance for a specified queue name...

ObjectName activeMQ = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");
BrokerViewMBean mbean = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, activeMQ,BrokerViewMBean.class, true);

for (ObjectName name : mbean.getQueues()) {
    QueueViewMBean queueMbean = (QueueViewMBean)
           MBeanServerInvocationHandler.newProxyInstance(mbsc, name, QueueViewMBean.class, true);

    if (queueMbean.getName().equals(queueName)) {
        queueViewBeanCache.put(cacheKey, queueMbean);
        return queueMbean;
    }
} 


Then, execute one of several APIs against the QueueViewMBean instance...

Queue monitoring - getEnqueueCount(), getDequeueCount(), getConsumerCount(), etc...

Queue manipulation - purge(), getMessage(String messageId), removeMessage(String messageId), moveMessageTo(String messageId, String destinationName), copyMessageTo(String messageId, String destinationName), etc...

Summary
The APIs can easily be used to build a web or command line based tool to support remote ActiveMQ management features. That being said, all of these features are available via the JMX console itself and ActiveMQ does provide a web console to support some management/monitoring tasks.

See these pages for more information...

http://activemq.apache.org/jmx-support.html
http://activemq.apache.org/web-console.html
Published at DZone with permission of Ben O' Day, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Stanislav Tyurikov replied on Fri, 2012/06/22 - 9:02am

Great post! You can also use JMX API to manage other frameworks, for example Apache Karaf, Apache Camel, Apache CXF, etc.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.