TNN
TNN

Reputation: 436

How to get the list of queues from a server?

Any idea how to get a list of all queues from Artemis v2.6.x? I have tried to retrieve them from some MBeans under org.apache.karaf.jmx, but it do not work as I expected. Please share some working solution.

Upvotes: 1

Views: 2562

Answers (1)

Justin Bertram
Justin Bertram

Reputation: 35142

A few important notes:

  • Artemis MBeans are under org.apache.activemq.artemis by default not org.apache.karaf.jmx (although that is configurable using the <jmx-domain> element in broker.xml).
  • By default the MBean names include the name of the broker (since multiple brokers can run in the same JVM). This name is configurable using the <name> element in broker.xml. The name is localhost by default. The broker name can be excluded from the MBean's name by setting <jmx-use-broker-name>false</jmx-use-broker-name> in broker.xml if desired.
  • There are several methods to get queue names from the broker:
    • getQueueNames(): This method is on the ActiveMQServerControl MBean and returns a String[] of the names of all the queues defined on the broker.
    • getQueueNames(String): This method is on the ActiveMQServerControl MBean and returns a String[] of the names of all the queues defined on the broker whose routing-type matches the input. Valid input values are anycast & multicast.
    • API documentation for ActiveMQServerControl.

You can find a working example of an MBean client accessing queue metrics in the jmx example shipped with Artemis. You can view the code for that example online here.

Here's a simple example of code to get the queue names from a broker running on localhost:

import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.util.HashMap;

import org.apache.activemq.artemis.api.core.management.ActiveMQServerControl;
import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder;

public class JMXExample {

   public static void main(final String[] args) throws Exception {
      ObjectName on = ObjectNameBuilder.DEFAULT.getActiveMQServerObjectName();
      JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"), new HashMap());
      MBeanServerConnection mbsc = connector.getMBeanServerConnection();
      ActiveMQServerControl serverControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, ActiveMQServerControl.class, false);
      for (String queueName : serverControl.getQueueNames()) {
         System.out.println(queueName);
      }
      connector.close();
   }
}

See more details in the Artemis Management documentation.

Upvotes: 3

Related Questions