79732889

Date: 2025-08-12 08:41:23
Score: 0.5
Natty:
Report link

Any answer worked for me, the simplest way to me is to connect to the jmx queue with mbean and retrieve java MBean, you can help with jconsole tool to find which method to execute.

public String getQueueSize() {
    try {
        String jmxUrl = "service:jmx:rmi://host:port/jndi/rmi://host:port/jmxrmi";
        JMXServiceURL url = new JMXServiceURL(jmxUrl);
        Map<String, Object> params = new HashMap<>();
        String[] credentials = {"user","password"
        };
        params.put(JMXConnector.CREDENTIALS, credentials);

        try (JMXConnector connector = JMXConnectorFactory.connect(url, params)) {
            MBeanServerConnection connection = connector.getMBeanServerConnection();
  
            // Use correct MBean for WSO2 Andes : see jconsole
            ObjectName queueMBean = new ObjectName("org.wso2.andes:name=QueueManagementInformation,type=QueueManagementInformation");

            // queue name is key and value is size in the object : see jconsole
            String queueName = env.getProperty("REQUEST_QUEUE");
            CompositeData allCounts = (CompositeData) connection.getAttribute(queueMBean, "MessageCountOfQueuesAsCompositeData");

            return allCounts.get(queueName).toString();
        }
    } catch (Exception e) {
        LOGGER.error("cannot connect to jms", e);
    }
    return "error";
}

The example is show with wso2 but works in another cases.

As a result you should manage all queue if you have multiple by adding name in method parameter !

Regards.

Reasons:
  • Blacklisted phrase (1): Regards
  • Whitelisted phrase (-1): worked for me
  • RegEx Blacklisted phrase (3): you can help
  • Long answer (-1):
  • Has code block (-0.5):
  • High reputation (-1):
Posted by: Zhar