Is it possible to get a list of queues on a remote broker?

孤街醉人 提交于 2019-12-01 02:37:43

问题


I'm trying to figure out how to get a list of existing queues on a remote broker.

It looks like I can listen to queues as they are created/destroyed by adding an advisory message (which I don't yet have working) but I need to get all EXISTING queues on startup.

It looks like I can do this with getDestinationMap:

http://activemq.apache.org/maven/apidocs/org/apache/activemq/broker/region/Region.html#getDestinationMap()

But that seems like it can only be called from an embedded and in-process broker.

I mean... I'm willing to go that route but it seems to make more sense to just have the normal init/daemon setup for activemq and then have a remote process connect to it like a normal JMS consumer.

This documentation seems to imply that it's possible:

http://activemq.apache.org/how-can-i-see-what-destinations-are-used.html

But that's by using a Region object and that only seems possible if you're in the same JVM as activemq.


回答1:


    // Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

// Create a Connection
ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();


//Important point that was missed in the above answer
connection.start();

DestinationSource ds = connection.getDestinationSource();
Set<ActiveMQQueue> queues = ds.getQueues();

for(ActiveMQQueue queue : queues){
    try {
        System.out.println(queue.getQueueName());
    } catch (JMSException e) {
        e.printStackTrace();
    }
}



回答2:


See this answer: https://stackoverflow.com/a/14021722/3735747

If you're doing this in Java, there's the DestinationSource class that will help: http://activemq.apache.org/maven/5.7.0/activemq-core/apidocs/org/apache/activemq/advisory/DestinationSource.html

Create a connection and use the ActiveMQConnection type instead of the JMS Connection type.

// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

// Create a Connection
ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();

Once you're connected, you can create a DestinationSource object and get the queues:

DestinationSource ds = connection.getDestinationSource();
Set<ActiveMQQueue> queues = ds.getQueues();

for(ActiveMQQueue queue : queues){
    try {
        System.out.println(queue.getQueueName());
    } catch (JMSException e) {
        e.printStackTrace();
    }
}


来源:https://stackoverflow.com/questions/24212496/is-it-possible-to-get-a-list-of-queues-on-a-remote-broker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!