Get list of queue names in ActiveMQ

2019-02-20 15:15发布

问题:

I have tried below code to get list of queues in ActiveMQ. But its not working. I have 4 queues in my ActiveMQ.

try {
ActiveMQConnection.makeConnection(URL).start();
Set<ActiveMQQueue> allque= ActiveMQConnection.makeConnection().getDestinationSource().getQueues();

Iterator<ActiveMQQueue> itr= allque.iterator();
while(itr.hasNext()){
ActiveMQQueue q= itr.next();
System.out.println(q.getQueueName());
}
} catch (Exception e) {

e.printStackTrace();
}

Please let me know any correction in my code or some new code get it done.

回答1:

The Destination Source functionality is not a guaranteed way to find the destinations on the Broker. The functionality can fail to provide any results in a number of cases such as when the advisory feature on the Broker is disabled or the client has been configured not to watch for advisories. You also query for destination immediately which doesn't necessarily allow for the time needed for the advisories to be dispatched to the client from the Broker.

A more reliable mechanism is the JMX support on the Broker which provides methods for obtaining lists of destinations along with a host of other information about the running broker instance.

There are plenty of articles out there showing how to use JMX with ActiveMQ.



回答2:

you have to invoke getDestinationSource().getQueues() on the same connection

try {
    ActiveMQConnection conn = ActiveMQConnection.makeConnection(URL);
    conn.start();

    Set<ActiveMQQueue> allque= conn.getDestinationSource().getQueues();
    Iterator<ActiveMQQueue> itr= allque.iterator();
    while(itr.hasNext()){
      ActiveMQQueue q= itr.next();
      System.out.println(q.getQueueName());
    }
} catch (Exception e) {

e.printStackTrace();
}


标签: java activemq