Listing queues and topics in ActiveMQ

This will provide the list of queues and topics managed by an ActiveMQ broker…

import javax.jms.*;
import java.io.*;
import java.util.*;
import org.apache.activemq.*;
import org.apache.activemq.command.*;
import org.apache.activemq.advisory.*;

public class GetDestinations {
  private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
  public static void main(String[] args) throws Exception {
    ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory("admin", "password", 
                                                                              "failover:(tcp://esb01:61616,tcp://esb02:61616)");
    ActiveMQConnection connection = (ActiveMQConnection)connectionFactory.createConnection();
    Session session = connection.createSession(true,  Session.AUTO_ACKNOWLEDGE);
    connection.start();
    DestinationSource dest = connection.getDestinationSource();
    Set amqt = dest.getTopics();
    System.out.println(amqt);
    Iterator queues2 = amqt.iterator();
    while (queues2.hasNext()) {
      ActiveMQTopic queue3 = queues2.next();
      System.out.println(new Date() + "\t" + "Topic: " + queue3.getPhysicalName());
    }
    Set amqs = dest.getQueues();
    System.out.println(amqs);
    Iterator queues = amqs.iterator();
    while (queues.hasNext()) {
      ActiveMQQueue queue2 = queues.next();
      System.out.println("Queue: " + queue2.getPhysicalName());
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.