Search code examples
jakarta-eejbossjmswildflymbeans

Get JMS Queue ObjectName on Wildfly


I want to get number of objects in Queue. This is the code I found:

MBeanServerConnection mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName queueName = ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queue.getQueueName());
Integer messageCount = (Integer) mbeanServer.getAttribute(queueName, "MessageCount");

There's some problem with this code. First of all there's no ObjectNameBuilder class on wildfly (Is there any module required!?).

After using HornetQ ObjectNameBuilder class source code, I've implemented the function by myself, but now getting this error:

org.hornetq:module=JMS,type=Queue,name="MyQueueName"

I'm using Wildfly v9.0.1-Final. Any suggestion?


Solution

  • I was facing a similar problem and some details made all the difference.

    If you are running Wildfly in domain mode so you must connect as follows:

    HashMap environment = new HashMap();
    environment.put(JMXConnector.CREDENTIALS, new String[] { "your_user", "your_password" });
    JMXServiceURL url = new JMXServiceURL("service:jmx:http-remoting-jmx://your_host:8080");
    
    JMXConnector jmxConnector = JMXConnectorFactory.connect(url, environment);
    MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
    

    Attention: Please note that the port for remote connection should be 8080, not 9990. Another important detail: your_user must be a user of the application type and no a mgmt type user.

    Also you should leave the setting on your JMX domain.xml as follows:

    <subsystem xmlns="urn:jboss:domain:jmx:1.3">
       <expose-resolved-model/>
       <expose-expression-model/>
       <remoting-connector use-management-endpoint="false" />
    </subsystem>
    

    If you are running Wildfly in standalone mode must use the 9990 port and authentication is not required.

    One more detail, try to recover the information as follows:

    String queueName = "YourQueue"; // use your queue name jms-queue 
    
    String mbeanObjectName = "jboss.as:subsystem=messaging,hornetq-server=default,jms-queue=" + queueName;
    
    ObjectName objectName = ObjectName.getInstance(mbeanObjectName);
    
    JMSQueueControl jmsQueueControl = (JMSQueueControl) MBeanServerInvocationHandler.newProxyInstance(connection, objectName, JMSQueueControl.class, false);
    
    assert jmsQueueControl != null;
    long msgCount = jmsQueueControl.countMessages(null);
    System.out.println(mbeanObjectName + " message count: " + msgCount);
    

    Note that instead of using the key "org.hornetq:module=JMS,type=Queue,name=" I used "jboss.as:subsystem=messaging,hornetq-server=default,jms-queue=".

    Ideally, use jconsole to verify the exact name of your key.