How to monitor JMS Queues using JMX

This article describes how to monitor JMS Queue statistics. You can create, collect, analyze, archive, and access diagnostic data generated by a running server and the applications deployed within its containers.
Here there are two very simple JMS examples which we can use to monitor any JMS resource deployed on our server. We will focus on JMS queue.

The old way

Below is reported the old style way.

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

import javax.management.AttributeList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import weblogic.management.MBeanHome;
import weblogic.management.WebLogicMBean;

public class JMSQueueStats {

private final static String JNDI_FACTORY = “weblogic.jndi.WLInitialContextFactory”;
private String url = “t3://localhost:7001”;
private String password = “weblogic123”;
private String username = “weblogic”;

private MBeanHome mbh;

public JMSQueueStats() throws NamingException {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
InitialContext ctx = new InitialContext(env);

mbh = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
}

public static void main(String[] args) throws Exception {
JMSQueueStats stats = new JMSQueueStats();
stats.start();
}

public void start() {
try {
Set mbeans = mbh.getMBeansByType(“JMSDestinationRuntime”);

int i = 0;
for (Iterator iter = mbeans.iterator(); iter.hasNext(); i++) {
WebLogicMBean mbean = (WebLogicMBean)iter.next();
String[] atts = new String[]{“Name”, “MessagesCurrentCount”,”MessagesPendingCount”,”MessagesHighCount”,”MessagesReceivedCount”,”MessagesThresholdTime”};
AttributeList al = mbean.getAttributes(atts);

HashMap values = new HashMap();
int i2 = 0;
for (Iterator iter1 = al.iterator(); iter1.hasNext(); i2++) {
javax.management.Attribute att = (javax.management.Attribute)iter1.next();
if (att.getValue() == null)
values.put(atts[i2], “null”);
else
values.put(atts[i2], att.getValue().toString());

}
String name = (String)values.get(“Name”);

WebLogicMBean parent = mbean.getParent();
if (parent != null)
name = parent.getName() + “.” + name;

System.out.println(name);

System.out.println(values);
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

In addition to JMSDestinationRuntime it’s possible get JMS attributes from the following runtime: JMSRuntime, JMSServerRuntime, JMSConnectionRuntime, JMSSessionRuntime, JMSProducerRuntime, JMSConsumerRuntime, JMSDurableSubscriberRuntime and JMSSessionPoolRuntime.

The new way

Since MBeanHome is deprecated below is showed a new way to monitor JMS Queue.

import java.util.Hashtable;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class JMSQueueStatsNew {

public static void main(String[] args) throws Exception {

JMXConnector jmxCon = null;

try {
JMXServiceURL serviceUrl = new JMXServiceURL(“service:jmx:t3://127.0.0.1:7001/jndi/weblogic.management.mbeanservers.runtime”);

System.out.println(“Connecting to: ” + serviceUrl);

Hashtable<String, String> env = new Hashtable<>();
env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, “weblogic.management.remote”);
env.put(javax.naming.Context.SECURITY_PRINCIPAL, “weblogic”);
env.put(javax.naming.Context.SECURITY_CREDENTIALS, “weblogic123”);

jmxCon = JMXConnectorFactory.newJMXConnector(serviceUrl, env);
jmxCon.connect();
MBeanServerConnection connection = jmxCon.getMBeanServerConnection();

ObjectName service = new ObjectName(“com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean”);
ObjectName serverRuntimeMBean = (ObjectName)connection.getAttribute(service, “ServerRuntime”);
System.out.println(“serverRuntimeMBean: ” + serverRuntimeMBean);
String serverName = (String)connection.getAttribute(serverRuntimeMBean, “Name”);
System.out.println(“Server name: ” + serverName);

ObjectName jmsRuntimeMbean = (ObjectName)connection.getAttribute(serverRuntimeMBean, “JMSRuntime”);
System.out.println(“JMSRuntime: ” + jmsRuntimeMbean);

ObjectName[] jmsServerRuntimeMbeans = (ObjectName[])connection.getAttribute(jmsRuntimeMbean, “JMSServers”);

for (ObjectName jmsServerRuntime : jmsServerRuntimeMbeans) {
String jmsServerName = (String)connection.getAttribute(jmsServerRuntime, “Name”);
System.out.println(“JMSServer name: ” + jmsServerName);

ObjectName[] jmsDestinationRuntimeMbeans = (ObjectName[])connection.getAttribute(jmsServerRuntime, “Destinations”);
for (ObjectName jmsDestinationRuntime : jmsDestinationRuntimeMbeans) {
String destinationName = (String)connection.getAttribute(jmsDestinationRuntime, “Name”);
System.out.println(destinationName);

if (destinationName.split(“@”).length == 2) {
destinationName = destinationName.split(“@”)[1];
}
if (destinationName.split(“!”).length == 2) {
destinationName = destinationName.split(“!”)[1];
}

System.out.println(destinationName);

long messagesCurrentCount = (Long)connection.getAttribute(jmsDestinationRuntime, “MessagesCurrentCount”);
System.out.println(“messagesCurrentCount ” + messagesCurrentCount);

long messagesPendingCount = (Long)connection.getAttribute(jmsDestinationRuntime, “MessagesPendingCount”);
System.out.println(“messagesPendingCount ” + messagesPendingCount);

long messagesReceivedCount = (Long)connection.getAttribute(jmsDestinationRuntime, “MessagesReceivedCount”);
System.out.println(“messagesReceivedCount ” + messagesReceivedCount);

long messagesHighCount = (Long)connection.getAttribute(jmsDestinationRuntime, “MessagesHighCount”);
System.out.println(“messagesHighCount ” + messagesHighCount);

System.out.println(“————————————————–“);
}
}
}
finally {
if (jmxCon != null)
jmxCon.close();
}
}
}

If you want to learn more retro games news, read our magazine and subscribe to our newsletter.