Discuss New Concept,New Technic,New Tools, Including EAI,BPM,SOA,Tibco,IBM MQ,Tuxedo, Cloud,Hadoop,NoSQL,J2EE,Ruby,Scala,Python, Performance,Scalability,Distributed,HA, Social Network,Machine Learning.

ActiveMQ

Oct 122012
 
 [repost ]managing ActiveMQ with JMX APIs  October 12, 2012  Posted by on October 12, 2012 at 1:30 pm ActiveMQ Tagged with: , ,  No Responses »

original:http://www.consulting-notes.com/2010/08/monitoring-and-managing-activemq-with.html

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